将鼠标悬停事件添加到directionsRenderer Google Maps API v3
Add mouseover event to directionsRenderer Google Maps API v3
使用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)
的方法?
第二个示例不起作用的原因是没有与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]
。
从那里开始,您需要使用directionsPath.routes[0]
中的LatLngs
数组来构造多段线,然后可以使用鼠标悬停事件。
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 使用Google Maps API向标记添加多个字符
- Google Maps API OverlayView()在AngularJS指令中不起作用
- 使用Google Maps API驱动时间多边形
- 使用Google Maps JavaScript API v3和Geocoding API映射多个位置
- 在for循环中使用多维数组设置google.maps.Marker图标
- 可扩展的画布作为Google Maps Javascript API中的覆盖
- 在自动完成中使用Google Maps Places API;API不工作
- google.maps.event.addDomListener(window, 'load', fun
- 根据PHP中数组的长度在Google Maps API中循环遍历标记
- 加载Google MAPS API v3的JSON数据
- 如何使用Dojo引用Google Maps事件中的包含类
- Javascript:Google Maps API:如果地图加载在隐藏容器中,则为空白地图
- 如何在Ionic/Codova应用程序中保护我的Google Maps Javascript V3 API密钥
- 来自JSON数组的Google Maps API生成器
- Google Maps JavaScript API v3:getDetails(请求,回调),用于多个位置
- "现代的“;在Google Maps JS API v3上使用字母标记
- Google Maps Javascript v3 自定义控件子类化标准控件
- Google Maps panTo conflicts with addListener
- 来自plugin.google.maps(不是com.google.maps)的标记不会改变其位置