在谷歌地图中没有获得纬度和长度的地址

not geting address with help of lat and long in google map

本文关键字:地址 纬度 谷歌地图      更新时间:2023-09-26

我正在尝试使用 reverse geocoding 获取地址。
但是geocoder.geocode({...})行存在一些问题。甚至正常的警报消息也不会显示。

 function initialize() { 
 var mapOptions = { 
    zoom: 11,
    center: new google.maps.LatLng(23.0171240, 72.5330533),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
  google.maps.event.addListener(map, 'click', function(e) {
    placeMarker(e.latLng, map);
    var input=e.latLng;
    var lat = parseFloat(input.lat());
    var lng = parseFloat(input.lng());
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
           alert(results[1].formatted_address);
        }
      });
    });
}
function placeMarker(position, map) {
  var marker = new google.maps.Marker({
    position: position,
    map: map
  });
  map.panTo(position);
}
google.maps.event.addDomListener(window, 'load', initialize);

initialize() 函数中添加以下行。

var geocoder = new google.maps.Geocoder();