猿问

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

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 的正确方法吗?


哆啦的时光机
浏览 153回答 1
1回答

噜噜哒

我测试了我的数据库并发现了发生更新的情况。下面的代码包含更新的 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 ) {&nbsp; &nbsp; // Construct a request object, passing the place ID and fields array.&nbsp; &nbsp; FetchPlaceRequest request = FetchPlaceRequest.builder(plc, placeFields).build();&nbsp; &nbsp; Log.e(TAG,"request for place with ID = " + plc);&nbsp; &nbsp; placesClient.fetchPlace(request).addOnSuccessListener((response) -> {&nbsp; &nbsp; &nbsp; &nbsp; Place place = response.getPlace();&nbsp; &nbsp; &nbsp; &nbsp; if (!plc.equals(place.getId())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG,"placeID renewed");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG,"placeID old = " + plc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG,"placeID new = " + place.getId());&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG, "Place found: " + place.getId());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }).addOnFailureListener((exception) -> {&nbsp; &nbsp; &nbsp; &nbsp; if (exception instanceof ApiException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ApiException apiException = (ApiException) exception;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int statusCode = apiException.getStatusCode();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Handle error with given status code.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG, "Place with ID "+plc+" not found");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG, "Exception message is :" + exception.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG, "Status code = " + statusCode);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}输出是E/MapsActivity: request for place with ID = ChIJTaKjynxesBQREvi1CU5QUFgE/MapsActivity: request for place with ID = jhDdW1odXJpeWV0IE1haGFsbGVzaSwgVXp1biBTay4sIEV6aW5lL8OHYW5ha2thbGUsIFR1cmtleSIuKiwKFAoSCakQkmN8XrAUEVkLpNK_F4IJEhQKEgmFzKyYe16wFBGSjU7F2ooIIgE/MapsActivity: request for place with ID = hIJy9YVxdxpsBQRq0-xUVJdZQ8E/MapsActivity: placeID renewedE/MapsActivity: placeID old = EjhDdW1odXJpeWV0IE1haGFsbGVzaSwgVXp1biBTay4sIEV6aW5lL8OHYW5ha2thbGUsIFR1cmtleSIuKiwKFAoSCakQkmN8XrAUEVkLpNK_F4IJEhQKEgmFzKyYe16wFBGSjU7F2ooIIgE/MapsActivity: placeID new = Ei5DdW1odXJpeWV0LCBVenVuIFNrLiwgRXppbmUvw4dhbmFra2FsZSwgVHVya2V5Ii4qLAoUChIJqRCSY3xesBQRWQuk0r8XggkSFAoSCYXMrJh7XrAUEZKNTsXaiggiE/MapsActivity: Place with ID hIJy9YVxdxpsBQRq0-xUVJdZQ8 not foundE/MapsActivity: Exception message is :9012: INVALID_REQUESTE/MapsActivity: Status code = 9012E/MapsActivity: Place found: ChIJTaKjynxesBQREvi1CU5QUFg似乎是status code 9102找不到地方。
随时随地看视频慕课网APP

相关分类

Java
我要回答