Google Place API 是 Google 提供的一套服务接口,允许开发者访问 Google 庞大的地点数据库,获取地点详情、搜索附近地点、获取自动完成建议等功能。
在中国大陆无法从 Google Place API Android 获取数据的主要原因包括:
可以考虑以下替代方案:
// 添加依赖
implementation 'com.amap.api:search:latest-version'
// 地点搜索示例
GeocodeQuery query = new GeocodeQuery("北京市", "北京");
GeocodeSearch geocodeSearch = new GeocodeSearch(this);
geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 1000) {
if (result != null && result.getRegeocodeAddress() != null) {
String city = result.getRegeocodeAddress().getCity();
String province = result.getRegeocodeAddress().getProvince();
// 处理获取到的城市和省份信息
}
}
}
@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
// 地理编码结果回调
}
});
geocodeSearch.getFromLocationNameAsyn(query);
虽然技术上可以通过代理服务器访问 Google Place API,但这种方法:
建议完全迁移到国内地图服务API,以获得更稳定的服务和更好的用户体验。