views:

30

answers:

1

If a user inputs an address, I want to convert to the equivalent LatLng.

I've read the documentation, and I think I can use the Geocoder class to do this, but can't figure out how to implement it.

Thanks for any help!

+1  A: 

There is a pretty good example on http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding

To shorten it up a little:

   geocoder = new google.maps.Geocoder();

  function codeAddress() {
    //In this case it gets the address from an element on the page, but obviously you  could just pass it to the method instead
    var address = document.getElementById("address").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //In this case it creates a marker, but you can get the lat and lng from the location.LatLng
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
Amasuriel