我想从自动填充地点服务获得的预测中获取地点(cityName,ZipCode等)的详细信息。我的代码如下:
Places.GeoDataApi.getAutocompletePredictions(googleApiClient, query, bounds, null)
.setResultCallback(
new ResultCallback<AutocompletePredictionBuffer>() {
@Override
public void onResult(AutocompletePredictionBuffer buffer) {
if (buffer == null)
return;
if (buffer.getStatus().isSuccess()) {
for (AutocompletePrediction prediction : buffer) {
// How to get cityName here
}
}
buffer.release();
}
}, 15, TimeUnit.SECONDS);
这可能吗?我该如何实现它? 如果我通过placeId查找地方详细信息,我无法得到我不想要的地方:
Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (places.getStatus().isSuccess()) {
// How to get cityName here
}
places.release();
}
});
答案 0 :(得分:4)
您需要Geocode
地点结果才能获取该信息。
Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (!places.getStatus().isSuccess()) {
// Request did not complete successfully
return;
}
// Setup Geocoder
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses;
// Attempt to Geocode from place lat & long
try {
addresses = geocoder.getFromLocation(
place.getLatLng().latitude,
place.getLatLng().longitude,
1);
if (addresses.size() > 0) {
// Here are some results you can geocode
String ZIP;
String city;
String state;
String country;
if (addresses.get(0).getPostalCode() != null) {
ZIP = addresses.get(0).getPostalCode();
Log.d("ZIP", ZIP);
}
if (addresses.get(0).getLocality() != null) {
city = addresses.get(0).getLocality();
Log.d("city", city);
}
if (addresses.get(0).getAdminArea() != null) {
state = addresses.get(0).getAdminArea();
Log.d("state", state);
}
if (addresses.get(0).getCountryName() != null) {
country = addresses.get(0).getCountryName();
Log.d("country", country);
}
}
} catch (IOException e) {
e.printStackTrace();
}
places.release();
}
});