我使用ContextMenu来达到其目的之外的其他目的,但是这个想法似乎很好,用谷歌地图上的点之间的距离标记折线。
我希望每次将鼠标悬停在显示该腿距离的折线上时都会显示标签。我确定距离没有问题,但是,标签总是显示最后一个条目的距离,而不是为该腿确定的距离。
我正在创建折线数组,这样每个折线都可以显示点之间的距离,除了距离标签外,它们似乎都可以正常工作。
以下是我试图实现的简化版本。
var flightPath = [];
var distanceLables = [];
var map;
function initMap() {
//Google Map
map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {
lat: 0,
lng: -180
},
mapTypeId: google.maps.MapTypeId.TERRAIN
});
//waypoints for the Polyline
var flightPlanCoordinates = [{
lat: 37.772,
lng: -122.214
}, {
lat: 21.291,
lng: -157.821
}, {
lat: -18.142,
lng: 178.431
}, {
lat: -27.467,
lng: 153.027
}];
//drawing each leg of the PolyLine indiviually so that mouseover/mouseout events can be customised to each leg
for (i = 0; i < flightPlanCoordinates.length - 1; i++) {
var tempCoords = [];
tempCoords.push(flightPlanCoordinates[i]);
tempCoords.push(flightPlanCoordinates[i + 1]);
flightPath.push(new google.maps.Polyline({
path: tempCoords,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
map: map
}));
//Creating the Context Menu which is just one line with the leg number
var contextMenuOptions = {};
contextMenuOptions.classNames = {
menu: 'context_menu displance_display',
menuSeparator: 'context_menu_separator'
};
var menuItems = [];
menuItems.push({
className: 'context_menu_item',
eventName: 'distance_click',
id: 'distanceItem',
label: 'Leg #' + i
}); //Label should represent the leg
contextMenuOptions.menuItems = menuItems;
var pos = distanceLables.push(new ContextMenu(map, contextMenuOptions)) - 1;
//mouseover/mouseout events to show and hide the label
google.maps.event.addListener(flightPath[i], 'mouseover', function(mouseEvent) {
distanceLables[pos].show(mouseEvent.latLng);
});
google.maps.event.addListener(flightPath[i], 'mouseout', function(mouseEvent) {
distanceLables[pos].hide();
});
}
}
&#13;
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.context_menu {
background-color: #ffff90;
border: 1px solid gray;
}
.context_menu_item {
padding: 3px 6px;
background-color: #ffff90;
}
.context_menu_item:hover {
background-color: #4b545f;
color: #fff;
}
.context_menu_separator {
background-color: gray;
height: 1px;
margin: 0;
padding: 0;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<title>Problem</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type='text/javascript' src='http://maps.googleapis.com/maps/api/js?ver=4.2.2'></script>
<script src="http://code.martinpearman.co.uk/googlemapsapi/contextmenu/1.0/src/ContextMenu.js"></script>
</head>
<body onload="initMap()">
<div id="map"></div>
</body>
</html>
&#13;
谢谢,
斯图
答案 0 :(得分:2)
问题是您要定义要在循环内的鼠标事件上显示的标签。现在它看起来有点像这样:
for (i = 0; i < flightPlanCoordinates.length - 1; i++) {
var pos = distanceLables.push(new ContextMenu(map, contextMenuOptions)) - 1;
google.maps.event.addListener(flightPath[i], 'mouseover', function(mouseEvent) {
distanceLables[pos].show(mouseEvent.latLng);
});
}
在鼠标悬停事件发生之前,匿名函数内的行不会执行。所以你真正在做的是:
var pos = 0;
google.maps.event.addListener(flightPath[i], 'mouseover', function(mouseEvent) {...});
var pos = 1;
google.maps.event.addListener(flightPath[i], 'mouseover', function(mouseEvent) {...});
var pos = 2;
google.maps.event.addListener(flightPath[i], 'mouseover', function(mouseEvent) {...});
... etc
在循环结束时,pos =数组的长度 - 1,因此当您将鼠标悬停在任何飞行路径部分上时,此行始终执行:
distanceLables[pos].show(mouseEvent.latLng);
即。它总是会:
distanceLables[3].show(mouseEvent.latLng);
另一种方法可能是:
for (i = 0; i < flightPlanCoordinates.length - 1; i++) {
var pos = distanceLables.push(new ContextMenu(map, contextMenuOptions)) - 1;
bindLabelEvents(flightPath[i], distanceLables[pos]);
}
var bindLabelEvents = function(polyline, label) {
google.maps.event.addListener(polyline, 'mouseover', function(mouseEvent) {
label.show(mouseEvent.latLng);
});
google.maps.event.addListener(polyline, 'mouseout', function(mouseEvent) {
label.hide();
});
};