Sometimes in wordpress we need to get latitude and longitude of a specific location from a address. Actually sometimes it is required to show location map, to get exact position of a particular place or we can use it to get the postal code for that location. As we know the coordinates are the basic information for a location.

To get the lat long for a location we need to follow the following steps :

Step 1 : Put the below mentioned code in functions.php of your current theme :

function get_lat_long($address){

		$address = str_replace(" ", "+", $address);
	
		$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
		$json = json_decode($json);
	
		$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
		$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
		return $lat.','.$long;
}

Step 2. Just copy and paste the below mentioned code at the place where you want to get the lat long information for a given address :

$address = "Jawahar Circle, Jaipur, Rajasthan, India"; //put your address here
$latlong    =   get_lat_long($address);
$latlong =  explode(',' ,$latlong);
$lat = $latlong[0]; //latitude value for mentioned address
$long = $latlong[1]; //longitude value for mentioned address
echo "Latitude is : ".$lat." and Longitude is : ".$long." for given Address : ".$address; 

Follow the above two steps and you will get the lat long information for a given address in few minutes. Modify the code as per your use and stay in touch with us to get best wordpress tricks.