8

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);
1
  • Do you get any error messages? Commented Sep 10, 2010 at 12:58

3 Answers 3

38

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){
    console.log( "latitude : " + results[0].geometry.location.lat() );
    console.log( "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!

Sign up to request clarification or add additional context in comments.

Comments

8

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.

Comments

1

Try it like:

geocoder = new GClientGeocoder();
point = geocoder.getLatLng(address)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.