Skip to content

Instantly share code, notes, and snippets.

@ashokmhrj
Created January 13, 2016 10:01
Show Gist options
  • Save ashokmhrj/e312b65322c445a28312 to your computer and use it in GitHub Desktop.
Save ashokmhrj/e312b65322c445a28312 to your computer and use it in GitHub Desktop.
Fetch Latitude and lonigitude
<?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