开发者

Geographical coordinates to street names

开发者 https://www.devze.com 2023-02-16 01:29 出处:网络
is any way (with a rest API will be awesome) to get the Street name correspon开发者_JS百科ding to a Geographical coordinate? I think the name is geocoding, do google have this API? Im PHP developer.

is any way (with a rest API will be awesome) to get the Street name correspon开发者_JS百科ding to a Geographical coordinate? I think the name is geocoding, do google have this API? Im PHP developer.

Ex.

<?php 
cor="38.115583,13.37579";
echo(geoname(cor)); // this prints: Foro Umberto I - 90133 Palermo  
?>

So the output of the function is the street name, the postal code and the city. Thanks for any help and scripts examples!


Yes, just use the "Reverse Geocoding" function in the Google Maps API: http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding

Here's some example code:

$lat="38.115583";
$long = "13.37579";

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);

$address = json_decode($curlData);
print_r($address);


Here's my PHP function I used for doing a Reverse Geocode lookup for a street address using the Google MAP API. Note, this example gets the output from Google in JSON, but I am doing a simple parse in PHP.

/*
 * Use Google Geocoding API to do a reverse address lookup from GPS coordinates
 */
function GetAddress( $lat, $lng )
{   
    // Construct the Google Geocode API call
    //
    $URL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=false";

    // Extract the location lat and lng values
    //
    $data = file( $URL );
    foreach ($data as $line_num => $line) 
    {
        if ( false != strstr( $line, "\"formatted_address\"" ) )
        {
            $addr = substr( trim( $line ), 22, -2 );
            break;
        }
    }

    return $addr;
}

Andrew Team at OpenGeoCode.Org

btw> Google does have restrictions on using their APIs for commercial purposes. Basically, you need to display the result on a google map.


You could also take a look at using Yahoo!'s PlaceFinder API, which offers reverse geocoding. A minimal example of a call to the API (asking it to return the lightweight data-interchange format du jour, JSON) might look like:

$url      = 'http://where.yahooapis.com/geocode?location=55.948496,-3.198909&gflags=R&flags=J';
$response = json_decode(file_get_contents($url));
$location = $response->ResultSet->Results[0];
print_r($location);

Which outputs the first result (hopefully there is one!) which contains properties like street, postal and city.


Another way of using the PlaceFinder API is through Yahoo!'s YQL API, which allows you to make use of SQL-like queries against "data tables" (often, other APIs).

Such a query might look like:

SELECT * FROM geo.placefinder WHERE text="55.948496,-3.198909" AND gflags="R"

(Try this in the YQL console interface)

To make a call to YQL with that query, from PHP, is very similar to the earlier example and should print the same information.

$query    = 'SELECT * FROM geo.placefinder WHERE text="55.948496,-3.198909" AND gflags="R"';
$url      = 'http://query.yahooapis.com/v1/public/yql?q='.urlencode($query).'&format=json';
$response = json_decode(file_get_contents($url));
$location = $response->query->results->Result;
print_r($location);


As @Elijah mentions, there's a lot of restrictions on the Maps API I gave in my last answer. You're only actually supposed to use it for Google Maps Applications. If that's a problem then you can also try the AdWords API. It also has a similar service but Google charges for it so there's less restriction.

0

精彩评论

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