how to reverse geocode thru php

2,434 views
Skip to first unread message

MT

unread,
Oct 22, 2010, 11:57:12 AM10/22/10
to Google Maps JavaScript API v3
I have cell phones sending in lats and longs to a php script to store
the lats and longs into the db. But i would also like to reverse
geocode the lats and longs into addresses before storing all data into
the db so that i have lats, longs, address, city,state,zipcode.

Is this possible?

- Minh

blob

unread,
Oct 22, 2010, 12:26:41 PM10/22/10
to Google Maps JavaScript API v3
Yes! You can use the geocoding api for this. Take a look at
http://code.google.com/intl/en/apis/maps/documentation/geocoding/#ReverseGeocoding.

For example you could do the following:

// call geoencoding api with param json for output
$geoCodeURL = "http://maps.googleapis.com/maps/api/geocode/json?
latlng=$lat,$lng&sensor=false";

$result = json_decode(file_get_contents($geoCodeURL), true);

print_r($result);

Now you will find all data received by the api call in the $address
array and you can simple select the relevant data you want to store in
your database.

Instead of JSON you can also retrieve the results in XML (replace json
by xml in the URL shown above).

Rossko

unread,
Oct 22, 2010, 3:08:41 PM10/22/10
to Google Maps JavaScript API v3
> Is this possible?

You need also to consider "is it legal?"
Google's geocoders are provided for use in conjunction with maps.
Reverse geocoding points and then just storing the results is not
allowed.
There are other geocoders:
http://groups.google.com/group/google-maps-api/web/resources-non-google-geocoders

blob

unread,
Oct 22, 2010, 5:07:27 PM10/22/10
to Google Maps JavaScript API v3
Of course I've assumed you're using the google geocoder only with a
corresponding Google map. If this is not the case you have to use
another geocoder..

The above shown example code can be easily adapted to the
OpenStreetMap geocoder for example. Just replace the URL by
http://nominatim.openstreetmap.org/reverse?format=json&lat=$lat&lon=$lon

If you want to get your results in XML replace "format=json" by
"format=xml".

Cheers
Reply all
Reply to author
Forward
0 new messages