简体   繁体   中英

Reverse geocoding with Google Maps API

I have found this code for reverse geocoding:

var point = new GLatLng (lat[1],long[1]);
var geocoder = new GClientGeocoder();
geocoder.getLocations (point, function(result) { alert (lat[1]+' '+long[1]+' '+result.address); });

But it pops the alert, saying that result.address is 'undefined'. Any ideas, what could be the problem?


EDIT: Got it working, thanks.

can you include the definitions of 'lat' and 'long'? also, 'long' is a reserved keyword, so that is likely a typo / bug.

also, the results that come back, at least in gmaps v2 json format, have a more complex structure, and 'result.address' won't have anything. when I tested it out, I needed to access one of the addresses with something like:

result.Placemark[0].address

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

From this I can only tell that result is not being passed to the function or it is not an object.

You need to see what parameters the callback function receives. Here's what the documentation says:

This response will contain a Status code, and if successful, one or more Placemark objects.

If you're using Firebug, you can see what's being passed to the callback this way:

var point = new GLatLng (lat[1],long[1]);
var geocoder = new GClientGeocoder();
geocoder.getLocations (point, function(result) { 
    window.console.log(arguments);
    // Here you will see what arguments are passed and
    // decide what to do about them
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM