views:

827

answers:

5

I have sets of co-ordinates in the following format

(-33.9,18.6)

How do I go about getting the name of the nearest town or Country for those co-ords? I'm guessing it will involve Javascript, but am happy to also use PHP if appropriate?

EDIT: Am trying the Google Reverse Geocoder but having trouble with it. The following code is pretty identical to one of their examples but doesn't seem to be running at all... any ideas why?

<script type="text/javascript" charset="utf-8">
            function reverseGeocode(lat,lon){

                var geocoder = new GClientGeocoder();
                var latlng = new GLatLng(lat, lon);
                geocoder.getLocations(latlng, function(addresses) {
                    alert(addresses);
                });

            }
        </script>
A: 

Google maps API has a javascript example.

http://code.google.com/apis/maps/documentation/services.html#ReverseGeocoding

ghoppe
have you any experience with it? I'm trying the following but to no avail<script type="text/javascript" charset="utf-8"> function reverseGeocode(lat,lon){ var geocoder = new GClientGeocoder(); var latlng = new GLatLng(lat, lon); geocoder.getLocations(latlng, function(addresses) { alert(addresses); }); } </script>
Chris Armstrong
A: 

Have you registered for their API key?

Also are there any specific error messages?

noko
Yes, I'm using the google api successfully to get the user's current location, but for some reason can't get reverse geolocation
Chris Armstrong
A: 

Ok managed to get it working, so here's what I did:

<script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=INSERTAPIKEYHERE" type="text/javascript"></script>
function reverseGeocode(latitude,longitude){

            var geocoder = new GClientGeocoder();
            var latlng = new GLatLng(latitude, longitude);

            geocoder.getLocations(latlng, function(addresses) {
                var address = addresses.Placemark[0].address;
                var country = addresses.Placemark[0].AddressDetails.Country.CountryName;
                var locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;

                alert(address);
            });
        }
Chris Armstrong
+1  A: 

google has a webservice you can call to do (reverse) geocoding, which is much more lightweight then using the full javascript api.

an example with v2: http://maps.google.com/maps/geo?q=51,4&amp;sensor=false&amp;output=json&amp;key=insert_your_api_key_here&amp;callback=parseme

and with v3: http://maps.google.com/maps/api/geocode/json?latlng=51,4&amp;sensor=false&amp;callback=parseme

in both cases you get a json response (javascript object), the difference being that in the now deprecated v2 you need to provide you api key and you can use a callback (for cross site ajax goodness) which for crying out loud isn't supported in v3 any more (but you don't need an api key any more).

futtta
Thanks, cold you maybe show me some sample code for using the webservice? I'm not sure how to go about it
Chris Armstrong
my still ongoing experiment lives at http://futtta.be/checkRoam/ It tries to find your coordinates (geolocation browser, html5) and uses those to get country code. Feel free to view source and copy/ paste.
futtta
A: 

Hi Chris,

I have started learning Javascript and want to know how to send lat and long via javascript to reverse geocode to get the address back an display on the page.

I copied and pasted the code but it did not work even with my key.

Can someone post a copy and paste version with complete code.

Thanks

Paul

Paul