Geocoder – find your latitude and longitude

Here is a quick way to find your latitude and longitude. It is a PHP script using the Google geocoding service. The Google Store Locator demo on this site utilizes the latitude and longitude from such a request to locate nearby Cyberstarweb locations. With this script as a start, you could build an entire site like geocoder.us


Here is the PHP source code of the geocoder.php

 

php

   set_error_handler('errorHandler');

   function errorHandler ($errno, $errstr, $errfile, $errline, $errcontext)
{
	//suppress all errors
}

   if ($_POST['address'] != "") {
   		doGeo(urlencode($_POST['address']));
   		}

   function doGeo($marker) {
   // Your Google Maps API key
   $key = "Your key here";

   // Desired address
   $address = "http://maps.google.com/maps/geo?q=$marker&output=xml&key=$key";

   // Retrieve the URL contents
   $page = file_get_contents($address);

   // Parse the returned XML file
   $xml = new SimpleXMLElement($page);

// Retrieve the desired XML node
//   echo $xml->Response->Placemark->Point->coordinates;

   // Parse the coordinate string
   list($longitude, $latitude, $altitude) = explode(",",
        $xml->Response->Placemark->Point->coordinates);

   // Output the coordinates
	echo "

".urldecode($marker)."


Longitude: $longitude, Latitude: $latitude

";
}
?>

One Response to “Geocoder – find your latitude and longitude”

Leave a Reply