我正在研究 JavaScript Google Map API(版本 3),更准确地说,正在研究反向地理定位。在the official documentation的帮助下,我成功进行了反向地理编码,找到了经纬度坐标对应的地址。
但我找不到如何到达与该地址绑定(bind)的名称。是否可以?怎样才能实现呢?
谢谢你。
卡米尔。
最佳答案
您确实需要循环并对谷歌在这些点发现的内容进行多次检查,一个小脚本来实际读取/循环返回的数据将是(在PHP中):
<?php
$data = json_decode(file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=46.1124,1.245&sensor=true"));
if($data->status == "OK") {
if(count($data->results)) {
foreach($data->results as $result) {
echo $result->formatted_address . "<br />";
}
}
} else {
// error
}
?>
现在基于文档:
Note that the reverse geocoder returned more than one result. ...etc
并且:
Generally, addresses are returned from most specific to least specific; the more exact address is the most prominent result...etc
您只需要第一个结果即可获得您想要的结果(或者至少在那里搜索它$data->results[0]->
。
因此,请阅读 types并在此基础上您可以检查是否出现您想要的结果:
<?php
$data = json_decode(file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=46.1124,1.245&sensor=true"));
if($data->status == "OK") {
if(count($data->results)) {
foreach($data->results[0]->address_components as $component) {
if(in_array("premise",$component->types) || in_array("route",$component->types) || in_array("park",$component->types)) {
echo $component->long_name . "<br />";
}
}
}
} else {
// error
}
?>
关于javascript - 使用 Google map API 进行反向地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4874556/