Google maps 将鼠标悬停在驾驶路线上时显示警告

Google maps 将鼠标悬停在驾驶路线上时显示警告,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,当我将鼠标悬停在驾驶路线上时,警报未显示, 有人能帮我吗? 我什么都试过了,但在驾驶路线上都不行 注意:我是在驾驶路线上执行此操作的,而不是在多段线上 <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <title

当我将鼠标悬停在驾驶路线上时,警报未显示, 有人能帮我吗? 我什么都试过了,但在驾驶路线上都不行

注意:我是在驾驶路线上执行此操作的,而不是在多段线上

    <!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回调中,则您的代码适用于我:


但我希望它用于驾驶路线,而不是Polylinet DirectionsRenderer没有任何记录在案的鼠标悬停事件。唯一记录在案的方法是使用从DirectionsResult创建的多段线。你可以。
<!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>