gpt4 book ai didi

javascript - 将鼠标悬停事件添加到 directionsRenderer Google Maps API v3

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:43 25 4
gpt4 key购买 nike

如何在使用 DirectionsService 时向 directionsRenderer 添加鼠标悬停事件监听器?

我知道如何给直线添加监听器,但似乎无法在 directionsRenderer 中找到对象。

例如这个有效:

function getStraightLine(coordinates) {
if (progress.length == 0)
progress = coordinates;
else
progress.push(coordinates[1]);
updateDistance();
var line = new google.maps.Polyline({
path: coordinates,
strokeColor: "#FF0000",
strokeOpacity: .5,
strokeWeight: 2,
map: map
});
google.maps.event.addListener(line, 'mouseover', function(){
alert("moused over straight line!");
});
return line;
}

但这不是:

function getDirectionsPath(coordinates) {
var directionsPath = new google.maps.DirectionsRenderer();
directionsPath.setMap(map);

var request = {
origin: coordinates[0],
destination: coordinates[1],
travelMode: google.maps.TravelMode.WALKING
};

directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
var coordinates = result.routes[0].overview_path;
if (progress.length == 0)
progress = coordinates;
else
progress = progress.concat(coordinates);
directionsPath.setDirections(result);
google.maps.event.addListener(directionsPath, 'mouseover', function(){
alert("moused over straight line!");
});
}
});

return directionsPath;
}

我尝试了 result、result.routes[0] 和其他几个,而不是 directionsPath。

那么我应该使用什么对象呢?

最佳答案

您会在 setDirections(directionsResult) 方法生成的“折线”上使用“拖动”事件吗?

如果你不这样做,我想你可以像这样自己创建一个“折线”:

directionsService.route(request, function (result, status) 
{
var myRoute = result.routes[0].legs[0];
if (status == google.maps.DirectionsStatus.OK)
{
for (var i = 0; i < myRoute.steps.length; i++) {
for (var j = 0; j < myRoute.steps[i].lat_lngs.length; j++) {
points.push(myRoute.steps[i].lat_lngs[j]);
}
}
}
drawRoute();
}

function drawRoute()
{
var routLine = new google.maps.Polyline(
{
path: points,
strokeColor: "Red",
strokeOpacity: 0.5,
strokeWeight: 10
}
);
routLine.setMap(mapCanvas);

// Add a listener for the rightclick event on the routLine
*google.maps.event.addListener(routLine, 'mouseover', function(){
alert("moused over straight line!");
});*
}

如果您已经解决了问题,请使用类似 google.maps.DirectionsRenderer().setDirections(result) 的方法?

关于javascript - 将鼠标悬停事件添加到 directionsRenderer Google Maps API v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9181636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com