使用Google Map API进行反向地理编码 [英] Reverse Geocoding with Google Map API

查看:308
本文介绍了使用Google Map API进行反向地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在研究JavaScript Google Map API(版本3),更精确地说是反向地理定位。在官方文档的帮助下,我成功了要执行反向地理编码,我找到了与纬度和经度坐标对应的地址。

但是我无法找到如何获得绑定到地址的名称。可能吗?如何执行?


I'm working on the JavaScript Google Map API (Version3) and more precisely on the reverse geolocation. With the help of the official documentation, I successed to perform the reverse geocoding, I found the address corresponding to the latitude and longitude coordonates.
But I can't find how to reach the name bound to the address. Is it possible? How can it be performed?

谢谢。

Camille。

Thanks you.
Camille.

推荐答案

您真的需要循环,并且对这些点上发现的内容进行多重检查,一个小脚本可以实际读取/循环返回的数据(在PHP中):

You really need to loop and do multiple checks of what google found at these points a small script to actually reads/loop the returned data would be (in 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
}
?>

现在基于文档:

Now based on the documentation:


请注意,反向地理编码器
返回多个结果。 ...等

Note that the reverse geocoder returned more than one result. ...etc

和:

And:


通常,地址是从最具体到最不具体的
返回的;
更确切的地址是最多
的显着结果...等等

Generally, addresses are returned from most specific to least specific; the more exact address is the most prominent result...etc

您只需要第一个结果(或至少在那里搜索它) $ data-> results [0] - >

所以有一个阅读类型并根据您的检查你想要的结果是否存在:

You only need the first result to get what you want (or at least to search for it there $data->results[0]->.
So have a read of the types and based on that you can check if the result you want present or not:

<?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
}
?>

这篇关于使用Google Map API进行反向地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆