How to geocode an address using Google and php

14
Jan

Google has some amazing services, one of my favourites would have to be the ability to geocode an address and get not only the lat/long but a clean, well formatted address back. 

How you say... Easy!

Get yourself an api key from here: http://code.google.com/apis/maps/signup.html

use the following php code:

<?php
$key
//your api key
$address //address to geocode
$url = 'http://maps.google.com/maps/geo?q='.urlencode($address).'&output=json&sensor=false&key='.$key;
$data = file_get_contents($url);
$loc = json_decode($data);
$lat = $loc->Placemark[0]->Point->coordinates[1];
$long = $loc->Placemark[0]->Point->coordinates[0];
?>

Thanks Google!

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.