Google maps api 3 谷歌地图Api v3地图上的拖动事件
我正在使用谷歌地图v3api。我需要检测地图上的拖动事件。无论是在地图上拖动移动到附近的地理位置,还是在标记上拖动。我需要一些javascript函数来在任何一个事件发生时运行。地图对象和标记对象都有Google maps api 3 谷歌地图Api v3地图上的拖动事件,google-maps-api-3,drag,Google Maps Api 3,Drag,我正在使用谷歌地图v3api。我需要检测地图上的拖动事件。无论是在地图上拖动移动到附近的地理位置,还是在标记上拖动。我需要一些javascript函数来在任何一个事件发生时运行。地图对象和标记对象都有drag事件,尽管您可能需要dragend,以便在用户完成拖动时可以执行某些操作,而不是在用户拖动时执行某些操作 所以你可以这样做: google.maps.event.addListener(map'dragend',function(){alert('map draugd');}); googl
drag
事件,尽管您可能需要dragend
,以便在用户完成拖动时可以执行某些操作,而不是在用户拖动时执行某些操作
所以你可以这样做:
google.maps.event.addListener(map'dragend',function(){alert('map draugd');});
google.maps.event.addListener(标记'dragend',函数(){alert('marker draugd');});
event.latLng.lat()和event.latLng.lng()将为您提供坐标(不是像素,而是GPS)
那么,如何获得以像素为单位的新坐标呢?获取坐标:虽然这个代码片段可能是解决方案,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。为什么设置超时?
google.maps.event.addListener(map, "mouseover", function (e) {
google.maps.event.addListener(map, "dragend", function () {
setTimeout(() => {
console.log(e); // this contains the information about the coordinates
});
});
});
function initMapModalAdd() {
function handleMarkerDrag(event) {
$('#formAddWell input[name=coordinates]').val(`${event.latLng.lat()}, ${event.latLng.lng()}`);
}
const mapCenterPos = {lat: -22.232916, lng: -43.109969};
window.googleMapAdd = new google.maps.Map(document.getElementById('modal-map-add'), {
zoom: 8,
streetViewControl: false,
center: mapCenterPos
});
const marker = new google.maps.Marker({draggable: true, position: mapCenterPos, map: window.googleMapAdd});
marker.addListener('drag', (event) => handleMarkerDrag(event));
marker.addListener('dragend', (event) => handleMarkerDrag(event));
}