【问题标题】:Google maps event listener dragend谷歌地图事件监听器 dragend
【发布时间】:2023-04-08 09:34:02
【问题描述】:

我正在为我的标记添加一个事件监听器,标记的功能是:

function setupMarkerWaypoint(){
console.log('setting waypoint marker');
waypointMarker = new google.maps.Marker({
    position: default_latlng,
    map: mapBooking,
    icon:"http://maps.google.com/mapfiles/ms/micons/blue.png"
});

waypointMarker.setVisible(true);

function geocodePosition(pos) {
geocoder.geocode({
'latLng': pos
}, function(responses) {
if (responses && responses.length > 0) {
  updateMarkerAddress(responses[0].formatted_address);
} else {
  updateMarkerAddress('Cannot determine address at this location.');
}
});
}

function updateMarkerAddress(str) {
document.getElementById('AddWaypoint').innerHTML = str;
 }

// Update current position info.

 geocodePosition(latLng);

 // Add dragging event listeners.

google.maps.event.addListener(waypointMarker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(waypointMarker.getPosition());
});

}

但我无法在谷歌地图上拖动我的标记。我应该做哪些必要的改变?

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 google-maps-markers dom-events


    【解决方案1】:

    要使标记可拖动,请将其draggable 属性设置为true

    Google Maps Javascript API Reference: MarkerOptions

    waypointMarker = new google.maps.Marker({
        position: default_latlng,
        map: mapBooking,
        draggable: true,
        icon:"http://maps.google.com/mapfiles/ms/micons/blue.png"
    });
    

    working fiddle

    代码sn-p:

    var geocoder;
    var mapBooking;
    var default_latlng
    
    function initialize() {
      mapBooking = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(37.4419, -122.1419),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      geocoder = new google.maps.Geocoder();
      default_latlng = mapBooking.getCenter();
      setupMarkerWaypoint();
    
    }
    google.maps.event.addDomListener(window, "load", initialize);
    
    function setupMarkerWaypoint() {
      console.log('setting waypoint marker');
      waypointMarker = new google.maps.Marker({
        position: default_latlng,
        map: mapBooking,
        draggable: true,
        icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
      });
    
      waypointMarker.setVisible(true);
    
      function geocodePosition(pos) {
        geocoder.geocode({
          'latLng': pos
        }, function(responses) {
          if (responses && responses.length > 0) {
            updateMarkerAddress(responses[0].formatted_address);
          } else {
            updateMarkerAddress('Cannot determine address at this location.');
          }
        });
      }
    
      function updateMarkerAddress(str) {
        document.getElementById('AddWaypoint').innerHTML = str;
      }
    
      // Update current position info.
    
      geocodePosition(waypointMarker.getPosition());
    
      // Add dragging event listeners.
    
      google.maps.event.addListener(waypointMarker, 'dragend', function() {
        // updateMarkerStatus('Drag ended');
        geocodePosition(waypointMarker.getPosition());
      });
    
    }
    html,
    body,
    #map_canvas {
      height: 500px;
      width: 500px;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
    <div id="map_canvas" style="border: 2px solid #3872ac;"></div>
    <div id="AddWaypoint"></div>

    【讨论】:

    • 我想为 dragend(google 事件监听器)进行地理编码,类似于此示例 developers.google.com/maps/documentation/javascript/examples/…。但我的地址键状态未定义。任何输入?
    • 听起来是个新问题。我过去曾这样做过。其实,什么地址?您应该使用反向地理编码器,它采用坐标。
    • 这里是示例链接geocodezip.com/…。我已经处理了事件侦听器部分 -geocoder.geocode({'latLng': latlng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { console.log(results); if ( results[0]) { var formatted_address = results[0].formatted_address; infowindow.setContent(results[0].formatted_address); infowindow.open(map, marker); 我得到一个错误 'latlng' : undefined
    • 请再问一个问题。评论不适用于新问题,并且在 cmets 中发布的代码真的很难阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多