javascript - 如何通过给定的纬度/经度获取地址

标签 javascript google-maps reverse-geocoding

如何通过给定的纬度/经度而不是位置获取地址?我尝试过 “反向地理编码”,但它只显示位置名称。

我的代码是这样的:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
var marker;
var marker2;

function initialize() {
geocoder = new google.maps.Geocoder();
var
latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
    zoom: 5,
    center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
google.maps.event.addListener(map, 'click', function (event) {
    //alert(event.latLng);      
    geocoder.geocode({
        'latLng': event.latLng
    }, function (results, status) {
        if (status ==
            google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                alert(results[1].formatted_address);
            } else {
                alert('No results found');
            }
        } else {
            alert('Geocoder failed due to: ' + status);
        }
    });
}); <!--click event--> }
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map-canvas"></div>

最佳答案

检查这两个引用文献:

How to get address location from latitude and longitude in Google Map.?

Get location address from Latitude and Longitude in google maps

这些答案会对您有所帮助。

使用此 URL 获取地址:

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true

关于javascript - 如何通过给定的纬度/经度获取地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20743351/

相关文章:

javascript - 在 JavaScript 中打印时间段

javascript - 多次触发谷歌地图 get_bounds 事件

javascript - 在新窗口中打开谷歌地图反向地理编码

iphone - 当键值观察者仍然注册时释放(反向地理编码器)

javascript - 在谷歌地图事件函数中发送附加参数

api - 从 Google Maps API 构建路口

javascript - 如何使用 Beautiful Soup 从 python 代码中获取 javascript 函数的结果?

javascript - 为什么我应该使用backbone.js或spine.js

javascript - 如何用JS制作一个计算模块(数学)?

javascript - 在 Google map 中打开生成的带有标记的 map