Created
November 27, 2015 10:17
-
-
Save microdesign/b0fcfe1badda0b7c2f69 to your computer and use it in GitHub Desktop.
PHP Server side Google Maps Geocoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function geocode(array $address) | |
{ | |
$address = urlencode(implode(', ', $address)); | |
$url = sprintf('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s', $address, 'YOUR-KEY'); | |
$data = @file_get_contents($url); | |
$data = json_decode($data, true); | |
// If the json data is invalid, return empty array | |
if ($data['status'] != 'OK') return array(); | |
return [ | |
'lat' => $data["results"][0]["geometry"]["location"]["lat"], | |
'lng' => $data["results"][0]["geometry"]["location"]["lng"], | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment