我的镇子上有1000多盏路灯,它们都保存在MYSQL表中。对于一个人记录一个特定的错误路灯,我需要让他们显示,并允许用户点击故障之一,以获得街灯的坐标。
有人能帮我如何听鼠标点击谷歌地图上的一个特定点,以便既获得经度和纬度的点?
在此之前,非常感谢您。
发布于 2017-12-11 07:04:07
为了在地图上显示它们并响应对特定路灯的点击,我们可以循环遍历存储的位置,并向地图中添加标记,在这些标记中添加一个单击事件监听器。
下面是一个示例,它有一个由两个latlon对象组成的数组来演示如何遍历您的位置并将事件处理程序附加到所有标记。
<div id="map" style="height:200px;"></div>
<script>
var map;
var lightLocations = [
{lat: -34.397, lng: 150.644},
{lat: -34.350, lng: 150.704}
];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 10
});
var markerClicked = function(event) {
// here's where you do what you need to do with the info
alert(this.position);
}
lightLocations.forEach( function(element) {
var marker = new google.maps.Marker({
position: element,
map: map
});
marker.addListener( 'click', markerClicked );
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
https://stackoverflow.com/questions/47747355
复制相似问题