我有一个包含准确地址(街道、编号、城市、地区/区域、国家/地区)的数据库。但是,我想知道如果我们在纽约,是否有办法使用 Google API 获取城市的地区(例如“曼哈顿”)?
所有其他信息我已经在数据库中,所以如果有的话我只需要地区(当然这只会在大城市)...
更新:
我在 http://www.techques.com/question/1-3151450/Google-geolocation-API---Use-longitude-and-latitude-to-get-address 上找到了这个函数并尝试将 formatted_address
更改为 sublocality(甚至其他人如 short_name 等)但它不返回任何内容......任何帮助将不胜感激!谢谢!!
function reverse_geocode($lat, $lon) {
$url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
$data = json_decode(file_get_contents($url));
if (!isset($data->results[0]->formatted_address)){
return "unknown Place";
}
return $data->results[0]->formatted_address;
}
最佳答案
您可以像这样访问子区域:
function reverse_geocode($lat, $lon) {
$url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
$data = json_decode(file_get_contents($url));
if (!isset($data->results[0]->address_components)){
return "unknown Place";
}
if ($data->results[0]->address_components[2]->types[0]=="sublocality") {
$return_array['type']="sublocality";
$return_array['sublocality_long_name']=$data->results[0]->address_components[2]->long_name;
$return_array['sublocality_short_name']=$data->results[0]->address_components[2]->short_name;
return $return_array;
}
}
关于php - Geo Coding Address - 获取某个地址的分区(Google API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199152/