【发布时间】:2016-08-16 23:12:23
【问题描述】:
我有一张谷歌地图,我正在上面绘制路线并将我的点与折线连接起来。我在地图上右键单击在线上创建一个新点。我还可以沿着我的路径创建一个兴趣点,我为这些点使用不同的标记图标,并显示一个带有 POI 文本的信息窗口。
google.maps.event.addListener(Map, "rightclick", function (event) {
var markerIndex = polyLine.getPath().length;
polyLine.setMap(Map);
var isFirstMarker = markerIndex === 0;
var marker;
var poiText = "";
if(!isAddingPOI) {
marker = new google.maps.Marker({
map: Map,
position: event.latLng,
draggable: true,
icon: '/images/waypoint.png'
});
} else {
poiText = prompt("Enter a short description for this point of interest", "");
if (poiText != null) {
marker = new google.maps.Marker({
map: Map,
position: event.latLng,
draggable: true,
icon: '/images/point_of_interest.png',
info: new google.maps.InfoWindow({
content:poiText
})
});
}
}
if (isAddingPOI) {
predefinedPositions.push(new predefinedPosition(event.latLng.lat(), event.latLng.lng(), poiText));
google.maps.event.addListener(marker, 'click', function () {
marker.info.open(Map, marker);
});
isAddingPOI = false;
} else {
predefinedPositions.push(new predefinedPosition(event.latLng.lat(), event.latLng.lng()));
}
polyLine.getPath().push(event.latLng);
outlineMarkers.push(marker);
google.maps.event.addListener(marker, 'drag', function (dragEvent) {
polyLine.getPath().setAt(markerIndex, dragEvent.latLng);
predefinedPositions[markerIndex].Latitude = dragEvent.latLng.lat();
predefinedPositions[markerIndex].Longitude = dragEvent.latLng.lng();
updateDistance(outlineMarkers);
});
updateDistance(outlineMarkers);
});
我现在需要的是用户能够为新标记指定 LatLng,该标记将被放置在地图上作为可拖动的,然后能够将该标记合并(通过拖动)到我创建的路径。理想情况下,我希望在标记位于其上方时拖动标记以更改颜色的折线段。然后我需要将该标记添加到最终输出的路径数组中。
我发现这是我能找到的最接近我想要的东西,但它不是我要找的东西:Google Maps API v3 - Draggable Marker Along a Polyline
我还尝试将“鼠标悬停”事件添加到我的折线以更新在我拖动标记时不会触发的颜色。当我将鼠标悬停在它上面时,它还会更新整个折线的颜色(不仅仅是线段)。我确信我可以自己解决段颜色变化,但我坚持将标记拖到线上并让它改变颜色并最终将其合并到我的路径中。
任何建议将不胜感激。
TIA
【问题讨论】:
-
您不能更新折线段的颜色(每条折线只有一种颜色)。您需要为每个线段创建单独的折线,然后在鼠标悬停时更改折线/线段的颜色。请提供一个 minimal reproducible example 来证明您的问题。
标签: google-maps google-maps-api-3 google-maps-markers google-polyline