Created
January 13, 2016 10:01
-
-
Save ashokmhrj/e312b65322c445a28312 to your computer and use it in GitHub Desktop.
Fetch Latitude and lonigitude
This file contains 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
<?php | |
/** | |
* Fetch Latitude and Longitude | |
* Need google api key with geo code enabled | |
*/ | |
function get_lat_long($address){ | |
$address = str_replace(", ", ",+", $address); | |
$address = str_replace("+", "+", $address); | |
$address = str_replace(" ", "+", $address); | |
$address = urlencode($address); | |
$api_key = 'YOUR-GOOGLE MAP APi-with GEOCODE-ENABLED'; | |
$json = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false&key=".$api_key); | |
$json = json_decode($json); | |
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}; | |
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}; | |
return array($lat,$long); | |
} | |
// Usages | |
$geo = get_lat_long('Kathmandu,Nepal'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment