使用 Google 地图 API 进行反向地理编码

发布于 2024-10-15 16:07:52 字数 280 浏览 4 评论 0原文


我正在研究 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?

Thanks you.
Camille.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澉约 2024-10-22 16:07:52

您确实需要循环并对谷歌在这些点发现的内容进行多次检查,一个小脚本来实际读取/循环返回的数据将是(在 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
}
?>

现在基于文档:

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

并且:

一般来说,地址是从
最具体到最不具体;这
更准确的地址是最
显着的结果...等

您只需要第一个结果即可获得您想要的结果(或者至少在那里搜索它$data->results[0]->
因此,请阅读类型并基于您可以检查是否出现您想要的结果:

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

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

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
}
?>
智商已欠费 2024-10-22 16:07:52

Google Map API 有一个地点查找 API 的测试版。我想这就是你所寻找的。基本上,您传递一个纬度/经度或地址,谷歌会对该地址进行反向查找到一个地名(如果它在其数据库中)。

请参阅他们的文档:
http://code.google.com/apis/maps/documentation/places/

摘自他们对 Places API 的介绍:

Places API 有两种相关的基本请求类型:地点搜索请求和地点详细信息请求。地点搜索请求发起对所提供位置周围“地点”的请求,通常通过地理位置提供(以便用户可以搜索他们当前所在的地点或附近的地点)。 Places API 返回靠近所提供位置的候选地点列表,然后用户可以对原始搜索中返回的特定实体发起地点详细信息请求。

Google Map API has a Beta release version of a place lookup API. I think this is what your looking for. Basically, you pass a lat/lng or address and google does a reverse lookup of the address to a place name (if its in its database).

See their documentation at:
http://code.google.com/apis/maps/documentation/places/

Excerpt from their intro on Places API:

The Places API has two basic types of requests, which are related: Place Search requests and Place Details requests. A Place Search request initiates a request for "places" around a provided location, often provided via geolocation (so that a user can search for the Place they are currently located or Places nearby). The Places API returns a list of candidate places that are near the provided location and the user can then initiate a Place Details request on a specific entity returned in the original search.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文