views:

711

answers:

3

Hello All...

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...

+2  A: 

The Google Geocoding API does that, although if I remember correctly, their terms of service forbid local storage of the geocoding results.

Pekka
I did an application once based on that and I don't remember having this limitation. I think is against logic not to save the results as they will not change in time and thus save a future ping to google.
Elzo Valugi
@Elzo you're right, it's allowed to "temporarily" cache the results if used in a Google maps app: 10.3 http://code.google.com/intl/en-EN/apis/maps/terms.html
Pekka
+4  A: 

The technical term for the process you describe is called reverse geocoding. Google offers the The Google Geocoding Web Service, 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.

Daniel Vassallo
@Daniel... Thanks a lot for your feedback.. I will work out with your given solutions and will get back to you...
Nirmal
+3  A: 

The Ordnance Survey have released the postcode locations on a Creative Commons licence (CC-BY v3, IIRC). It would be a lot less hassle (and a lot clearer legally) to use that instead.

There's even a version with WGS84 (a.k.a. GPS) coordinates mirrored by mySociety

Rowland Shaw
+1 a link to the ordnance survey data dump would also be nice if you had it handy.
Pekka