将mouseover事件添加到directionsRenderer Google Maps API v3

时间:2012-02-07 18:21:10

标签: javascript google-maps google-maps-api-3

使用DirectionsService时,如何向directionsRenderer添加mouseover事件侦听器?

我知道如何将侦听器添加到直线但似乎无法在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;
    }

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

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

2 个答案:

答案 0 :(得分:5)

您是否会使用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)

等方法

答案 1 :(得分:0)

第二个示例不起作用的原因是因为没有与DirectionsRenderer()类生成的对象关联的事件。它会生成一个DirectionsResult对象。

http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRenderer

基于文档:

http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsResult

DirectionsResult对象包含DirectionsRoutes数组。使用上面的代码,我将使用directionsPath对象来获取路由:directionsPath.routes然后获取第一条路线directionsPath.routes[0]

从那里开始,您需要使用LatLngs中的directionsPath.routes[0]数组来构建折线,然后您可以使用鼠标悬停事件。