Reverse geocoding with Google Maps API

2019-08-18 06:01发布

问题:

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.

回答1:

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



回答2:

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
});