views:

71

answers:

3

I'm trying to get longitude and latitude values from the google maps api with an address, however it doesn't seem to be returning any value. Is there a problem with my syntax?

var point = GClientGeocoder.getLatLng('1600 Pennsylvania Avenue NW Washington, DC 20500');
alert(point);
+2  A: 

This works (provided you have the correct API key, subject to the 2500 requests/day rate limit):

address_string = '1600 Pennsylvania Avenue NW Washington, DC 20500';
geocoder = new GClientGeocoder();
geocoder.getLatLng(
    address_string,
    function(point) {
        if (point !== null) {
            alert(point); // or do whatever else with it
        } else {
            // error - not found, over limit, etc.
        }
    }
);

You seem to be calling a function of GClientGeocoder, where you need to create a new GClientGeocoder() and call its function.

Piskvor
Thanks, That works. I'm still learning javascript.
Queueball
@Queueball: You're welcome; I'm still learning, too :)
Piskvor
A: 

Try it like:

geocoder = new GClientGeocoder();
point = geocoder.getLatLng(address)
anand
+1  A: 

It seems like you're using v2 of Google Maps; if you're interested in using v3 (since Google has officially deprecated version 2), you could do something like this:

var mygc = new google.maps.Geocoder();
mygc.geocode({'address' : '1600 Pennsylvania Avenue NW Washington, DC 20500'}, function(results, status){
    alert( "latitude : " + results[0].geometry.location.lat() );
    alert( "longitude : " + results[0].geometry.location.lng() );
});

It's similar to the other examples except:

  • You call new google.maps.Geocoder() instead of GClientGeocoder()
  • You pass in an object with an 'address' property, instead of the string by itself
  • The results is a JSON object, which means you have to access lat/lng a little differently

The Google API docs have more detail for v3. Cheers!

Christopher Scott