简体   繁体   English

使用Google Maps API进行反向地理编码

[英]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'. 但是它弹出警报,说result.address是'undefined'。 Any ideas, what could be the problem? 有什么想法,可能是什么问题?


EDIT: Got it working, thanks. 编辑:可以了,谢谢。

can you include the definitions of 'lat' and 'long'? 您可以包括“ lat”和“ long”的定义吗? also, 'long' is a reserved keyword, so that is likely a typo / bug. 另外,“ long”是保留关键字,因此很可能是拼写错误/错误。

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. 同样,返回的结果(至少以gmaps v2 json格式)具有更复杂的结构,并且“ result.address”将没有任何结果。 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 请参阅http://code.google.com/apis/maps/documentation/geocoding/index.html#Geocoding响应

From this I can only tell that result is not being passed to the function or it is not an object. 由此,我只能说result没有传递给函数或它不是对象。

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: 如果您使用的是Firebug,则可以通过以下方式查看传递给回调的内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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