开发者

Reverse Geocoding with Google Map API

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 corre开发者_开发知识库sponding 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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜