Google Maps API反向搜索地址详细信息准确性

时间:2009-10-18 20:22:01

标签: javascript google-maps

我使用Google Maps API反向查找地址,特别是国家/地区,城市和邮政编码。我遇到的问题是,当您输入特定的lat lng时,使用地理编码器可以根据地址详细信息的准确度获得一系列结果。例如,如果您点击街道,您的地址详细信息准确度为8,地址如下所示; “街道,城市,国家/地区代码”“街道,城市邮政编码,国家/地区代码”。如果您点击另一个位置,您可以获得返回5的地址详细信息准确度;城市邮政编码,国家代码。

我正在寻找的是城市,邮政编码和国家。有没有办法强制Google地图始终返回5的地址详细信息准确度,或者将分解的元素返回到单个部分,即城市,邮政编码和国家/地区。

这是我用来获取信息的代码:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"> 
  <head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps JavaScript API Example:  Reverse Geocoder</title> 
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAMkjr3Dq_jK6GSFYDFzaHTBRoJ0TBOI_XK4bTNi9dL2l04KlxphRNL_k0peOib9IHF6T2KwlVmOb6uQ" type="text/javascript"></script> 
    <script type="text/javascript">

        var map;
        var geocoder;
        var address;

        function initialize() {

            var zoom = 10;

            map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(40.730885, -73.997383), zoom);
            map.setUIToDefault();
            GEvent.addListener(map, "click", getAddress);
            geocoder = new GClientGeocoder();
        }

        function getAddress(overlay, latlng) {
            if (latlng != null) {
                address = latlng;
                geocoder.getLocations(latlng, showAddress);
            }
        }

        function showAddress(response) {
            map.clearOverlays();
            if (!response || response.Status.code != 200) {
                alert("Status Code:" + response.Status.code);
            } else {
                place = response.Placemark[0];
                point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                marker = new GMarker(point);
                map.addOverlay(marker);

                var message = '<b>orig latlng:</b>' + response.name + '<br/>' +
                                '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
                                '<b>Status Code:</b>' + response.Status.code + '<br>' +
                                '<b>Status Request:</b>' + response.Status.request + '<br>' +
                                '<b>Address:</b>' + place.address + '<br>' +
                                '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
                                '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode;

                marker.openInfoWindowHtml(message);
            }
        }


    </script> 
  </head> 

  <body onload="initialize()"> 
    <div id="map_canvas" style="width: 800px; height: 600px"></div> 
  </body> 
</html>

非常感谢任何帮助。我的最后一招是解析地址字符串,但感觉就像是一种矫枉过正。必须有一个更清洁的方式。

我目前正在查看getLocations()方法,也许解析JSON结果可能会产生我正在寻找的字段。

感谢名单。

1 个答案:

答案 0 :(得分:1)

您需要在应用中处理不同的准确度... API无法为您做任何事情;)。我同意,如果你可以向地理编码器发出你想要的准确度级别,那就好了,现在就不存在了。也许谷歌将来会实现它,但在此之前,你必须在你的应用程序中解析结果。这篇文章分析了不同的准确度值:Picking the most accurate geocode

相关问题