javascript 客户端反向地理编码(Google Maps V3 API)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6558661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse Geocode on Client Side (Google Maps V3 API)
提问by Nyxynyxx
How do you do a Reverse Geocode on the clientside using Google Maps V3 API? The forward geocode from address to LatLng is straight forward (code below), but how do you do the same for reverse geocode?
您如何使用 Google Maps V3 API 在客户端进行反向地理编码?从地址到 LatLng 的正向地理编码是直截了当的(下面的代码),但是如何对反向地理编码做同样的事情?
Normal Geocode Code:
普通地理编码:
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
回答by Khepri
The process is exactly the same, with the minor difference that instead of supplying an address object to the geocode function you supply a LatLng object
该过程完全相同,只是有一点不同,即不是向地理编码函数提供地址对象,而是提供 LatLng 对象
Reverse Geocode Code:
反向地理编码:
var input = document.getElementById("latlng").value;
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
Hope that helps.
希望有帮助。