本文介绍了当鼠标悬停在行驶路线上时显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将鼠标悬停在行驶路线上时,警报不会显示,有人可以帮我吗?
我尝试了所有事情,但无法将鼠标移到路线上.

Alert is not showing when I mouse over on driving route,can any body help me?
I tried every thing but not works on driving route mouse over.

注意:我是在将鼠标移到折线上而不是在折线上的时候这样做的.

Note: I am doing this on driving route mouse over not on polyline.

    <!DOCTYPE html>
   <html>
      <head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Travel modes in directions</title>
   <link       href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet">
 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
 var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
 var haight = new google.maps.LatLng(37.7699298, -122.4469157);
 var oceanBeach = new google.maps.LatLng(37.7683909618184, -122.51089453697205);

     function initialize() {
       directionsDisplay = new google.maps.DirectionsRenderer();
   var mapOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: haight
    }
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
       directionsDisplay.setMap(map);
   }
         var points=[];
       function calcRoute() {
      var selectedMode = document.getElementById('mode').value;
     var request = {
  origin: haight,
  destination: oceanBeach,
  // Note that Javascript allows us to access the constant
  // using square brackets and a string value as its
  // "property."
  travelMode: google.maps.TravelMode[selectedMode]
     };
         directionsService.route(request, function(response, status) {
        var myRoute = response.routes[0].legs[0];    
       if (status == google.maps.DirectionsStatus.OK) {
           directionsDisplay.setDirections(response);
        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]);
            }
        }
    }
         });
         alert("points "+points);
        var eventLine = new google.maps.Polyline({
    path: points,
    visible: true,
    strokeOpacity: 0,
    zIndex: 1000
           }); 
        eventLine.setMap(map);
       google.maps.event.addListener(eventLine, "mouseover",  function() { 
                                                 alert("hi")
                                        });
       }

    google.maps.event.addDomListener(window, 'load', initialize);

      </script>
    </head>
     <body>
<div id="panel">
<b>Mode of Travel: </b>
<select id="mode" onchange="calcRoute();">
  <option value="DRIVING">Driving</option>
  <option value="WALKING">Walking</option>
  <option value="BICYCLING">Bicycling</option>
  <option value="TRANSIT">Transit</option>
</select>
</div>
<div id="map-canvas"></div>
  </body>
     </html>

推荐答案

DirectionsService 没有鼠标悬停事件, Polyline 有.如果将折线创建移动到DirectionsService回调中,那么您的代码对我有用:

The DirectionsService doesn't have a mouseover event, a Polyline does. Your code works for me if I move the polyline creation into the DirectionsService callback:

http://www.geocodezip.com/v3_SO_directionsMouseOver.html

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Travel modes in directions</title>
   <link       href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet">
 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
 var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
 var haight = new google.maps.LatLng(37.7699298, -122.4469157);
 var oceanBeach = new google.maps.LatLng(37.7683909618184, -122.51089453697205);

     function initialize() {
       directionsDisplay = new google.maps.DirectionsRenderer();
   var mapOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: haight
    }
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
       directionsDisplay.setMap(map);
     calcRoute();
   }
         var points=[];
       function calcRoute() {
      var selectedMode = document.getElementById('mode').value;
     var request = {
  origin: haight,
  destination: oceanBeach,
  // Note that Javascript allows us to access the constant
  // using square brackets and a string value as its
  // "property."
  travelMode: google.maps.TravelMode[selectedMode]
     };
         directionsService.route(request, function(response, status) {
        var myRoute = response.routes[0].legs[0];    
       if (status == google.maps.DirectionsStatus.OK) {
           directionsDisplay.setDirections(response);
        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]);
            }
        }
//         alert("points "+points);
        var eventLine = new google.maps.Polyline({
    path: points,
    visible: true,
    strokeOpacity: 0,
    zIndex: 1000
           }); 
        eventLine.setMap(map);
       google.maps.event.addListener(eventLine, "mouseover",  function() { 
                                                 alert("hi")
                                        });
    } else { alert("Directions request failed: "+status); } 
         });
       }

    google.maps.event.addDomListener(window, 'load', initialize);

      </script>
    </head>
     <body>
<div id="panel">
<b>Mode of Travel: </b>
<select id="mode" onchange="calcRoute();">
  <option value="DRIVING">Driving</option>
  <option value="WALKING">Walking</option>
  <option value="BICYCLING">Bicycling</option>
  <option value="TRANSIT">Transit</option>
</select>
</div>
<div id="map-canvas"></div>
  </body>
</html>

这篇关于当鼠标悬停在行驶路线上时显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 00:08