Google地图 - 从连续结果中获取结果[0] .formatted_address [1] .formatted_address

时间:2012-07-11 02:19:08

标签: javascript google-maps-api-3 geocode

我所拥有的地址是从包含6个数字(例如530456)的数据库中检索到的连接results[1].formatted_address。然后我对其进行了地理编码并在谷歌地图上放置了一个标记。这部分是成功的。

我现在想要在标记的infowindow中显示完整地址results[0].formatted_address,所以我所做的是反转地理编码标记的latLng以获得完整地址但导致整个地图消失。我这样做是这样的:

var address;
var eventwindow;
function codeAddress(postal) {
    geocoder.geocode( {'address': postal + ", Singapore"}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        eventwindow = new google.maps.InfoWindow();
        var markerE = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
        });

        address = getMarkerAddress(results[0].geometry.location);

        google.maps.event.addListener(marker, 'click', function(){
            if(eventwindow)
                eventwindow.close();
            eventwindow = new google.maps.InfoWindow({
                content: address,
            });
            eventwindow.open(map,marker);
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

function getMarkerAddress(location) {
    geocoder.geocode({'latLng': location}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
          if(results[0])
          {
            address = results[1].formatted_address.substring(10);
          }
        }
       else {
        alert("Geocoder failed due to: " + status);
      }
    });

我不知道我是否做得对,而且不确定我哪里出错了。还有另一种解决这个问题的方法吗?如果是这样的话?

1 个答案:

答案 0 :(得分:0)

  

我不知道我是否做得对,而且不确定我哪里出错了。还有另一种解决这个问题的方法吗?如果是这样的话?

如果您知道“正确”地址,请使用该地址。您没有提供足够的应用程序详细信息来提供有关如何执行此操作的建议。您可以通过存储数据库中地理编码器返回的坐标以及地址来避免地理编码器的不确定性,然后如果发现错误,则可以修复它。

This article on geocoding strategies可能有所帮助。