Getting LAT & LONG values of an address with Google Maps API in PHP

Getting LAT & LONG values of an address with Google Maps API in PHP

Always wanted to get the LATITUDE & LONGITUDE coordinates of a adress with PHP?

This script returns the correct LAT and LON points according to the Google Maps API.
Can be interested when you want to do a inverted Geo-location look-up in PHP.

function getLatLong($address){
   if (!is_string($address))die("ERROR! - Invalid Address!");
   $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
   $_result = false;
   if($_result = file_get_contents($_url)) {
      if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
      preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
      $_coords['lat'] = $_match[1];
      $_coords['long'] = $_match[2];
   }
   return $_coords; // returns an array $_coords['lat'], $_coords['long']
}
credits : http://phpsnips.com/535/Get-Lat-&-Long-values-of-an-address#.UVA_mByyCzQ

About the Author

Daan is a Creative-Geek who loves learning and sharing new techniques! Follow him on Twitter to keep up to date with the Creative-Geeks blog and other subjects. Contact him on e-mail : info[at]creative-geeks.com.