Skip to content

Instantly share code, notes, and snippets.

@ichadhr
Last active October 7, 2019 15:37
Show Gist options
  • Save ichadhr/3a4d99da4c1af629f31611535b20d7e7 to your computer and use it in GitHub Desktop.
Save ichadhr/3a4d99da4c1af629f31611535b20d7e7 to your computer and use it in GitHub Desktop.
PHP Get lat, long from address
// ------------------------------------------
// converts a string with a stret address
// into a couple of lat, long coordinates.
// ------------------------------------------
public function getLatLong($address){
if (!is_string($address))die("All Addresses must be passed as a string");
$_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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment