为了账号安全,请及时绑定邮箱和手机立即绑定

Google Places API:使用 Java 刷新地点 ID

Google Places API:使用 Java 刷新地点 ID

哆啦的时光机 2023-04-26 17:08:56
Google警告我们有关 PlaceID 的更改并提出建议;由于 Google 地图数据库的大规模更新,地点 ID 可能会发生变化。在这种情况下,地点可能会收到新的地点 ID,而旧 ID 会返回 NOT_FOUND 响应。您可以免费刷新地点 ID,方法是发出地点详细信息请求,仅在字段参数中指定 ID 字段。不幸的是,他们的网站上没有 Java/Kotlin 的示例代码,除了指向他们的Web 服务的链接String placeID ="Some place ID";    List<Place.Field> placeFields = Arrays.asList(            Place.Field.ID,    );    // Construct a request object, passing the place ID and fields array.    FetchPlaceRequest request = FetchPlaceRequest.builder(placeID, placeFields).build();    placesClient.fetchPlace(request).addOnSuccessListener((response) -> {        Place place = response.getPlace();        if (!placeID.equals(place.getId())) {            //update your old placeID        }   }).addOnFailureListener((exception) -> {        if (exception instanceof ApiException) {            //Place removed.        }   });这是用 Java 更新 placeID 的正确方法吗?
查看完整描述

1 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

我测试了我的数据库并发现了发生更新的情况。下面的代码包含更新的 placeID、删除的 placeID 和仍然有效的 palceID,以便进行测试。


ArrayList<String> placeIDsList = new ArrayList<>();

placeIDsList.add("ChIJTaKjynxesBQREvi1CU5QUFg");

placeIDsList.add("EjhDdW1odXJpeWV0IE1haGFsbGVzaSwgVXp1biBTay4sIEV6aW5lL8OHYW5ha2thbGUsIFR1cmtleSIuKiwKFAoSCakQkmN8XrAUEVkLpNK_F4IJEhQKEgmFzKyYe16wFBGSjU7F2ooIIg");

placeIDsList.add("hIJy9YVxdxpsBQRq0-xUVJdZQ8");


// Specify the fields to return (in this example all fields are returned).

List<Place.Field> placeFields = Arrays.asList(Place.Field.ID);


for (String plc : placeIDsList ) {

    // Construct a request object, passing the place ID and fields array.

    FetchPlaceRequest request = FetchPlaceRequest.builder(plc, placeFields).build();


    Log.e(TAG,"request for place with ID = " + plc);


    placesClient.fetchPlace(request).addOnSuccessListener((response) -> {

        Place place = response.getPlace();



        if (!plc.equals(place.getId())) {

            Log.e(TAG,"placeID renewed");

            Log.e(TAG,"placeID old = " + plc);

            Log.e(TAG,"placeID new = " + place.getId());


        } else {

            Log.e(TAG, "Place found: " + place.getId());

        }



    }).addOnFailureListener((exception) -> {

        if (exception instanceof ApiException) {

            ApiException apiException = (ApiException) exception;

            int statusCode = apiException.getStatusCode();

            // Handle error with given status code.

            Log.e(TAG, "Place with ID "+plc+" not found");

            Log.e(TAG, "Exception message is :" + exception.getMessage());

            Log.e(TAG, "Status code = " + statusCode);

        }

    });

}

输出是


E/MapsActivity: request for place with ID = ChIJTaKjynxesBQREvi1CU5QUFg

E/MapsActivity: request for place with ID = jhDdW1odXJpeWV0IE1haGFsbGVzaSwgVXp1biBTay4sIEV6aW5lL8OHYW5ha2thbGUsIFR1cmtleSIuKiwKFAoSCakQkmN8XrAUEVkLpNK_F4IJEhQKEgmFzKyYe16wFBGSjU7F2ooIIg

E/MapsActivity: request for place with ID = hIJy9YVxdxpsBQRq0-xUVJdZQ8

E/MapsActivity: placeID renewed

E/MapsActivity: placeID old = EjhDdW1odXJpeWV0IE1haGFsbGVzaSwgVXp1biBTay4sIEV6aW5lL8OHYW5ha2thbGUsIFR1cmtleSIuKiwKFAoSCakQkmN8XrAUEVkLpNK_F4IJEhQKEgmFzKyYe16wFBGSjU7F2ooIIg

E/MapsActivity: placeID new = Ei5DdW1odXJpeWV0LCBVenVuIFNrLiwgRXppbmUvw4dhbmFra2FsZSwgVHVya2V5Ii4qLAoUChIJqRCSY3xesBQRWQuk0r8XggkSFAoSCYXMrJh7XrAUEZKNTsXaiggi

E/MapsActivity: Place with ID hIJy9YVxdxpsBQRq0-xUVJdZQ8 not found

E/MapsActivity: Exception message is :9012: INVALID_REQUEST

E/MapsActivity: Status code = 9012

E/MapsActivity: Place found: ChIJTaKjynxesBQREvi1CU5QUFg

似乎是status code 9102找不到地方。


查看完整回答
反对 回复 2023-04-26
  • 1 回答
  • 0 关注
  • 152 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信