Reverse geocoding with Google Maps API

岁酱吖の 提交于 2020-01-06 06:49:28

问题


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


来源:https://stackoverflow.com/questions/2667636/reverse-geocoding-with-google-maps-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!