如何在地图拖动后很长时间内获得纬度

How to get lat, long after map dragend

<script>
  function initialize() {
    var crosshairShape = {coords:[0,0,0,0],type:'rect'};

    var mapCanvas = document.getElementById('map');
    var mapOptions = {
      center: new google.maps.LatLng(23.634501, -102.552784),
      zoom: 5,
      scrollwheel: false,
      panControl: false,
      zoomControl: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(mapCanvas, mapOptions);

    var marker = new google.maps.Marker({
        map: map,
    });

    marker.bindTo('position', map, 'center');
    //marker.bindTo('position', map, 'center');

    map.addListener('dragend', function() {
        var Newlat = position.coords.latitude;
       console.log(Newlat);
      });

  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

这是我用来制作原点带有标记的可拖动地图的代码。我似乎无法获得标记所在位置的经纬度。 有人可以帮帮我吗?

map dragend event 回调函数没有任何参数。在回调函数中,获取 map 中心(这是你想要的):

map.addListener('dragend', function() {
  var Newlat = map.getCenter().lat();
  console.log(Newlat);
});

proof of concept fiddle

代码片段:

function initialize() {
  var mapCanvas = document.getElementById('map');
  var mapOptions = {
    center: new google.maps.LatLng(23.634501, -102.552784),
    zoom: 5,
  }
  var map = new google.maps.Map(mapCanvas, mapOptions);

  var marker = new google.maps.Marker({
    map: map,
  });

  marker.bindTo('position', map, 'center');

  map.addListener('dragend', function() {
    var Newlat = map.getCenter().lat();
    console.log(Newlat);
  });

}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>