如何通过js获取用户在地图上点击的位置?
答案 0 :(得分:1)
这是我在地图画布上用于放置事件的回调函数:
function placeDroppedMarker()
{
var locale = new google.maps.LatLng(mouseLat, mouseLng);
document.getElementById('map-lat').value = mouseLat;
document.getElementById('map-lng').value = mouseLng;
document.getElementById('map-icon-input').value = SelectedIcon;
var marker = new google.maps.Marker({
position: locale,
map: map,
draggable: true
});
var image = new google.maps.MarkerImage(
"http://www.twulogin.org/ourride/wp-content/themes/spectrum/images/" + SelectedIcon + ".png",
new google.maps.Size(37,37),
new google.maps.Point(0,0),
new google.maps.Point(16,37),
new google.maps.Size(37,37)
);
marker.setIcon(image);
map.setCenter(locale);
}
你必须像这样设置一个事件监听器(在你的map init函数中)来获取mouseLat和mouseLng:
google.maps.event.addListener(map, 'mousemove', function(event) {
mouseLat = event.latLng.lat();
mouseLng = event.latLng.lng();
});
地图是你的谷歌地图。
完整的工作示例是: http://www.ourride.org/?page_id=238