Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Created April 19, 2012 17:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongilbert/2422306 to your computer and use it in GitHub Desktop.
Save dongilbert/2422306 to your computer and use it in GitHub Desktop.
Geolocate an Address Using Google Maps via PHP
<?php
// URL Encode the address
$address = urlencode($data['storelocator']['address']);
// Get the JSON response from google. You can also get XML by changing geocode/json to geocode/xml
$geo = file_get_contents('http://maps.google.com/maps/api/geocode/json?sensor=false&address='.$address);
// Decode the results into an Object
$result = json_decode($geo);
// If it came back ok, retrieve the latitude and longitude from the results.
if($result->status === 'OK')
{
$lat = $result->results[0]->geometry->location->lat;
$lng = $result->results[0]->geometry->location->lng;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment