And*_*oes 4 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;
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
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;
}
Run Code Online (Sandbox Code Playgroud)
而不是directionsPath我尝试过result,result.routes [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!");
});*
}
Run Code Online (Sandbox Code Playgroud)
如果你已经解决了问题,请使用类似的方法google.maps.DirectionsRenderer().setDirections(result)吗?
| 归档时间: |
|
| 查看次数: |
4443 次 |
| 最近记录: |