我有两个文本框供用户在数据库中输入新记录时使用。
我有一个Google Maps的实时预览,效果很好,现在我想要做的是在地图上添加一个右键单击事件,该事件会用单击的坐标填充经度/经度文本框。
这有可能吗?
我知道如何添加事件侦听器,并查看了API文档,但没有看到任何这样做的东西。我知道你可以在他们网站的谷歌地图上做到这一点。
发布于 2011-10-27 02:04:54
google.maps.event.addListener(map, "rightclick", function(event) {
var lat = event.latLng.lat();
var lng = event.latLng.lng();
// populate yor box/field with lat, lng
alert("Lat=" + lat + "; Lng=" + lng);
});
发布于 2017-01-30 19:09:17
您可以创建一个InfoWindow对象(class documentation here),并将一个rightclick
事件处理程序附加到该对象上,该事件处理程序将使用地图上所单击位置的纬度和经度填充该对象。
function initMap() {
var myOptions = {
zoom: 6,
center: new google.maps.LatLng(-33.8688, 151.2093)
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
marker = new google.maps.Marker({
map: map,
}),
infowindow = new google.maps.InfoWindow;
map.addListener('rightclick', function(e) {
map.setCenter(e.latLng);
marker.setPosition(e.latLng);
infowindow.setContent("Latitude: " + e.latLng.lat() +
"<br>" + "Longitude: " + e.latLng.lng());
infowindow.open(map, marker);
});
}
html,
body {
height: 100%;
}
#map-canvas {
height: 100%;
width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAIPPUQ0PSWMjTsgvIWRRcJv3LGfRzGmnA&callback=initMap" async defer></script>
<div id="map-canvas"></div>
https://stackoverflow.com/questions/7905733
复制相似问题