Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch Latitude Longitude by passing postcodes to maps.google.com using Javascript

I have Postcode in my large database, which contains values like SL5 9JH, LU1 3TQ etc.

Now when I am pasting above postcode to maps.google.com it's pointing to a perfect location..

My requirement is like I want to pass post codes to maps.google.com and it should return a related latitude and longitude of that pointed location, that I want to store in my database.

So, most probably there should be some javascript for that... If anybody have another idea regarding that please provide it..

Thanks in advance...

like image 875
Nirmal Avatar asked Apr 12 '10 12:04

Nirmal


2 Answers

A quick note for those finding this SO answer. The answer by Daniel Vassallo uses the Google Geocoding API V2 which has now been deprecated. The new v3 API uses a request format like this:

http://maps.googleapis.com/maps/api/geocode/output?parameters

An example for a postcode lookup, returning the data in JSON format is:

http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false

This returns a JSON array that includes the lat and long in results->geometry->location->lat and results->geometry->location->lng

Example response:

{
 "results" : [
  {
     "address_components" : [
        {
           "long_name" : "SL5 9JH",
           "short_name" : "SL5 9JH",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Windsor and Maidenhead",
           "short_name" : "Windsor and Maidenhead",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "Ascot",
           "short_name" : "Ascot",
           "types" : [ "postal_town" ]
        }
     ],
     "formatted_address" : "Ascot, Windsor and Maidenhead SL5 9JH, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 51.39655490000001,
              "lng" : -0.66024660
           },
           "southwest" : {
              "lat" : 51.39457330,
              "lng" : -0.6624574999999999
           }
        },
        "location" : {
           "lat" : 51.39539040,
           "lng" : -0.66096740
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 51.39691308029150,
              "lng" : -0.6600030697084980
           },
           "southwest" : {
              "lat" : 51.39421511970851,
              "lng" : -0.6627010302915021
           }
        }
     },
     "types" : [ "postal_code" ]
  }
],
"status" : "OK"
}

The API spec is available here: https://developers.google.com/maps/documentation/geocoding/

like image 91
CharlesA Avatar answered Oct 19 '22 00:10

CharlesA


The technical term for the process you describe is called reverse geocoding. Google offers the The Google Geocoding Web Service New working Google Geocoding Link, where you can do reverse geocoding on the server side, instead of in JavaScript on the client-side.

For example, if you try the following URLs in your browser, you would get back the latitude and longitude of the postcode passed in the q parameter, in CSV format:

http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false

http://maps.google.com/maps/geo?q=LU13TQ,+UK&output=csv&sensor=false

This is how you would be able to reverse geocode your postcodes in php, for example:

$url = 'http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false';

$data = @file_get_contents($url);

$result = explode(",", $data);

echo $result[0]; // status code
echo $result[1]; // accuracy
echo $result[2]; // latitude
echo $result[3]; // longitude

Note that as Pekka suggested in another answer, the Google Maps API Terms of Use seem to prohibit the storage of the results, unless the store acts as a cache for data that will used in Google Maps. You may want to get in touch with Google and enquire on the Google Maps API Premier to have more flexible terms of use for your geocoding requirements.

like image 22
Daniel Vassallo Avatar answered Oct 19 '22 02:10

Daniel Vassallo