繁体   English   中英

谷歌地图事件监听器 dragend

[英]Google maps event listener dragend

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

 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()); }); }

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

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

Google Maps Javascript API 参考:MarkerOptions

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

工作小提琴

代码片段:

 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM