gpt4 book ai didi

java - 谷歌地方 API : refresh Place IDs with Java

转载 作者:行者123 更新时间:2023-11-30 10:01:50 25 4
gpt4 key购买 nike

谷歌 warns us关于PlaceID的变化和建议;

Place IDs may change due to large-scale updates on the Google Maps database. In such cases, a place may receive a new place ID, and the old ID returns a NOT_FOUND response.

You can refresh Place IDs free of charge, by making a Place Details request, specifying only the ID field in the fields parameter.

不幸的是,除了指向其 web services 的链接外,他们的网站上没有 Java/Kotlin 的示例代码。

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

最佳答案

我测试了我的数据库并发现了发生更新的情况。下面的代码包含更新的 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

状态代码 9102 似乎是未找到的地方。

关于java - 谷歌地方 API : refresh Place IDs with Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com