Tuesday 17 December 2013

Get longitude & latitude values with Google Maps API using PHP


Get longitude & latitude with Google Maps API get the latitude and longitude values from Google Maps API, based on the town, city or country location.
<?php
  $url = "http://maps.google.com/maps/api/geocode/json?address=hyderabad&sensor=false&region=IN";
  $response = file_get_contents($url);
  $response = json_decode($response, true);
 
  //print_r($response);
 
  $lat = $response['results'][0]['geometry']['location']['lat'];
  $long = $response['results'][0]['geometry']['location']['lng'];
 
  echo "latitude: " . $lat . " longitude: " . $long;
?>
The http://maps.google.com/maps/api/geocode/json URL takes in 3 parameters: address (your main location), region (optional but can help with ambiguity) and sensor (indicates whether or not the request will come from a device with a locational sensor).
The print_r command commented out above is a useful way to work out the array structure that you need to drill down to. To get the longitude and latitude values, we need to drill down through 5 levels.

More Info vist: developers.google.com

No comments:

Post a Comment