Login

Email
Password
Submit
Cancel

Look Up Geolocation From Address

(Working Tool and Tutorial With Code)

It is often needed to look up geolocation from a simple address. I have often used to try to use Google maps for this, but surprisingly, it is not that easy to get the longitude and latitude of a location from just an address.

I had to figure out how to do this on my own and it was a little bit of a pain. Now I figured it out (mostly) and want to share this step by step turorial for doing that.

Form For Input

Full Address (We will convert this to lon/lat coordinates)

Longitude and Latitude Results


PHP Code To Get Latitude and Longitude

For the HTML code, you can just view source of this page. Here is the PHP code which processes the address you give it and spits out the latitide and longitude :) ...the only problem is the spacing of the indentation of the code.

function nameYourFunction( $place )
{
$address = utf8_encode($place); // Encode the argument
// call geo-encoding api with param json for output
$geoCodeURL = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false";

$result = json_decode(file_get_contents($geoCodeURL), true);

return ($result);
}

Calling the PHP Lat/Lng Function

// Get the lat/lng
$result = getLatLngFromAddress ( $full_address );
$lat = $result["results"][0]["geometry"]["location"]["lat"];
$lng = $result["results"][0]["geometry"]["location"]["lng"];