我正在使用带有自动完成功能的Google Place API for Android
一切正常,但当我得到如here所示的结果时,我没有城市和邮政编码信息 .
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
= new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (!places.getStatus().isSuccess()) {
// Request did not complete successfully
Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
return;
}
// Get the Place object from the buffer.
final Place place = places.get(0);
// Format details of the place for display and show it in a TextView.
mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(),
place.getId(), place.getAddress(), place.getPhoneNumber(),
place.getWebsiteUri()));
Log.i(TAG, "Place details received: " + place.getName());
}
};
Place类不包含该信息 . 我可以获得完整的人类可读地址,lat和long等 .
如何从自动填充结果中获取城市和邮政编码?
5 回答
您通常无法从地方检索城市名称,
但你可以通过这种方式轻松获得它:
1)从你的地方获取坐标(或者你得到它们);
2)使用Geocoder按坐标检索城市 .
它可以这样做:
可以分两步检索城市名称和邮政编码
1)对https://maps.googleapis.com/maps/api/place/autocomplete/json?key=API_KEY&input=your_inpur_char进行Web服务调用 . JSON包含
place_id
字段,可在步骤2中使用 .2)对https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1进行另一次Web服务调用 . 这将返回包含
address_components
的JSON . 循环通过types
找到locality
和postal_code
可以给你城市名称和邮政编码 .实现它的代码
很遗憾,目前无法通过Android API获取此信息 .
它可以使用Places API Web服务(https://developers.google.com/places/webservice/) .
// ......
不是最好的方法,但以下内容可能很有用: