Skip to content

Instantly share code, notes, and snippets.

@CodeBrauer
Last active March 26, 2019 12:42
Show Gist options
  • Save CodeBrauer/984ae92d4c75ff14f087 to your computer and use it in GitHub Desktop.
Save CodeBrauer/984ae92d4c75ff14f087 to your computer and use it in GitHub Desktop.
get coordinates from address with google maps api
<?php
function GMGetCoordinates($address) {
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$ch = curl_init();
$options = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
if (!$response) {
return false;
}
$response = json_decode($response);
if ($response->status !== 'OK') {
return false;
}
$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng;
return "$lat, $long";
}
@debcal
Copy link

debcal commented Mar 26, 2019

Its showing following error, Please inform how to fix it:-

{
"error_message" : "You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account",
"results" : [],
"status" : "REQUEST_DENIED"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment