Skip to content

Instantly share code, notes, and snippets.

@maxyc
Forked from thinkerytim/google_geocoder.php
Last active August 29, 2015 14:06
Show Gist options
  • Save maxyc/e36791086e0356f85c12 to your computer and use it in GitHub Desktop.
Save maxyc/e36791086e0356f85c12 to your computer and use it in GitHub Desktop.
<?php
// Google Maps API key
$key = key; // Google API key
$mapaddress = urlencode($listing['street_number'] . " " . $listing['street_direction'] . " " . $listing['street'] . " " . $listing['street_suffix'] . " " . $listing['street_suffix2'] . ", " . $listing['city'] . ", " . $listing['state'] . " " . $listing['zip']);
function geocoder($mapaddress){
global $key;
// Desired address
$url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";
// Retrieve the URL contents
$page = file_get_contents($url);
// Parse the returned XML file
$xml = new SimpleXMLElement($page);
// Parse the coordinate string
list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
$g_address = addslashes($xml->Response->Placemark->address);
$status = $xml->Response->Status->code;
$google_coordinates = $xml->Response->Placemark->Point->coordinates;
if ($status == 200) {
// got legit code back, so save the lat/lon
} elseif ($status == 602) {
// invalid address so post error message and let them manually add marker
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment