我正在使用 Google Maps API 来反向查找地址,特别是国家/地区、城市和邮政编码。我遇到的问题是,使用地理编码器,当您输入特定的经纬度时,您会根据地址详细信息的准确性获得一系列结果。例如,如果您单击街道,您会获得地址详细信息精度为 8 的地址,地址如下所示; “街道、城市、国家/地区代码” 或 “街道、城市邮政编码、国家/地区代码”。如果您点击另一个位置,您可以获得返回的地址详细信息精度 5;城市邮政编码、国家/地区代码。
我要查找的只是城市、邮政编码和国家/地区。有没有办法强制 Google map 始终返回 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&v=2&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 结果可以产生我正在寻找的字段。
谢谢。
最佳答案
您需要在应用程序中处理不同的准确度级别...API 无法为您完成所有工作;)。我同意,如果您可以向地理编码器指定您想要的精度级别,那就太好了,但目前还没有。也许谷歌将来会实现它,但是在那之前,您必须在应用程序中解析结果。这篇文章对不同的精度值进行了分割:Picking the most accurate geocode
关于javascript - Google Maps API 反向查找地址详细信息准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1585935/