Google Maps中的反向地理编码 [英] Reverse Geocoding in Google Maps

查看:383
本文介绍了Google Maps中的反向地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在自己的Web应用程序中嵌入了Google Maps.

我想实现反向地理编码的功能(从纬度和经度中获取地址).我已经在地图点击"上完成了此操作.您可以检查以下链接...

在地图上反向地理编码单击 [

I have embedded Google Maps within my web application.

I want to implement a feature for Reverse geocoding (getting the address from the latitude and longitude). I have already done it on Map click. You may check the following link...

Reverse Geocode on Map click[^]

Whenever you click on a map, it creates a maker and shows the address of the locaton..

Now I am trying to get the address on Marker click..

Can you help me? How should I code the marker?

推荐答案

这是您刚创建的标记吗?

如果是这样,则可以将地址详细信息存储在数组中,并使用它们在标记click事件中填充信息窗口.

如果没有,您可以做这样的事情:-

Is this for the marker that you just created?

If so you can store the address details in an array and use them to populate the infowindow in the markers click event.

If not you could do some thing like this :-

google.maps.event.addListener(Marker, "click",function() {
    geocoder.geocode({''latLng'': this.getPostion()}, function(results, status) {           
   if (status == google.maps.GeocoderStatus.OK) {
      wincont = ''''


.each(results, function(){
.each(results, function(){


.each(this.address_components, function(){ switch (this.types.join(", ")) { case ''street_number'': wincont += this.long_name break; case ''route'': wincont += this.long_name break; case ''locality, political'': wincont += this.long_name break; case ''postal_code'': wincont += this.long_name break; } }); }); gInfoWindow = new google.maps.infowindow() gInfoWindow.setContent(wincont); gInfoWindow.open(gMap,marker); } else { alert("An Error Occurred with Google''s Geo-location Look up, Please Try again"); } });
.each(this.address_components, function(){ switch (this.types.join(", ")) { case ''street_number'': wincont += this.long_name break; case ''route'': wincont += this.long_name break; case ''locality, political'': wincont += this.long_name break; case ''postal_code'': wincont += this.long_name break; } }); }); gInfoWindow = new google.maps.infowindow() gInfoWindow.setContent(wincont); gInfoWindow.open(gMap,marker); } else { alert("An Error Occurred with Google''s Geo-location Look up, Please Try again"); } });



请注意,这是使用jQuery的,您可以为每个循环轻松地使用常规,结果集可以包含多个结果,如果需要的话,可以只使用第一个,目前它将用每个结果填充信息窗口(可能不是您想要的!),但仅使用第一个结果应该很容易.

还请注意,这只是在寻找特定的地址字段,这是从一些我只需要这些字段的旧代码中复制而来的,邮政编码值通常只返回通用区号,因此您可能需要对其进行长度检查首先.

如果您在地图上有多个标记,则需要将click事件绑定到每个标记事件,我通常会存储一个标记数组并将该事件添加到数组引用中.



note that this is using jQuery, you can just as easily use a normal for each loop, the results set can contain multiple results, you can just use the first if you wish, at the moment it will populate the infowindow with EVERY result (probably not what you want!) but it should be easy to only use the first result.

also note that this is only looking for specific address fields, this is copied from some old code where I only needed those fields, the post code value often comes back with only the general area code so you may want to do a length check on it first.

If you have multiple markers on the map you will want to bind a click event to each of them, I usually store an array of markers and add the event to the array reference.


这篇关于Google Maps中的反向地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆