Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geographical coordinates to street names

is any way (with a rest API will be awesome) to get the Street name corresponding 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!

like image 504
DomingoSL Avatar asked Mar 09 '11 22:03

DomingoSL


2 Answers

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);
like image 185
Ewan Heming Avatar answered Oct 11 '22 12:10

Ewan Heming


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.

like image 43
Andrew - OpenGeoCode Avatar answered Oct 11 '22 10:10

Andrew - OpenGeoCode