Skip to content

Instantly share code, notes, and snippets.

@gaffling
Created April 3, 2021 11:56
Show Gist options
  • Save gaffling/2db726d73c1938b0fff2c58a33bdde5b to your computer and use it in GitHub Desktop.
Save gaffling/2db726d73c1938b0fff2c58a33bdde5b to your computer and use it in GitHub Desktop.
[getLatLong] Get lat and long coordinatesby address from Google Maps #php #function #geo
/* ---------------------------------------------------------------------------------------- */
/* [getLatLong] Get lat and long coordinatesby address from Google Maps #php #function #geo */
/* ---------------------------------------------------------------------------------------- */
function getLatLong($address){
if (!is_string($address)) die('ERROR - Address must be a string!');
$url = sprintf('https://www.google.com/maps/place/%s', rawurlencode($address));
$result = file_get_contents($url);
preg_match('!center=(-?\d+\.\d+)%2C(-?\d+\.\d+)&zoom=!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