Javascript – Google Geocode via HTTP callback function

ajaxcallbackgeocodingjavascript

I want to use the google geocode via HTTP functionality to translate a city name into longitude and latitude in for my AJAX web application.

However, it appears that no callback function exists for the HTTP geocoder functionality

http://code.google.com/apis/maps/documentation/geocoding/index.html

Is that true, no callback function exists?

Because if that is true, it essentially means that the Google geocode via HTTP api is useless when used with AJAX because JavaScript will throw a crossdomain exception error.

Any ideas on how I can use the geocode via HTTP api in my AJAX web application in JavaScript?

Note: I do not want to use the full blown Google Maps API which is approx 200kb download (i.e. GClientGeocoder). I want to use the HTTP api b/c of it's super quick responsiveness and lack of needing my web users from having to download the huge full blown interactive google maps api.

E.g.
http://maps.google.com/maps/geo?output=json&sensor=false&key={API_KEY}&q={CITY,STATE}&CALLBACK=????

Thanks

Best Solution

Here is an example that uses the Google Maps Geocoder. The geocoder function getLocation takes a callback function as the second argument.

function findAddress(street, city, state, zip) {
  var address = [
    street,
    city.toLowerCase(),
    state.toLowerCase(),
    zip
  ].join(', ');

  if (!geocoder) {
    geocoder = new GClientGeocoder();
  }

  if (geocoder) {
    geocoder.getLocations(
      address,
      function(result) {
        var dialog, len, point;
        if (result.Status.code != G_GEO_SUCCESS) {
          alert("Error: "+result.Status.code)
        } else {
          len = result.Placemark.length;
          if (len > 1) {
            alert("Multiple matches were found.  I'll leave it as an exercise to handle this condition");
          } else {
            point = new GLatLng(
              result.Placemark[0].Point.coordinates[1],
              result.Placemark[0].Point.coordinates[0]
            );
          }
        }
      }
    );
  }
}