-2

当我将鼠标悬停在行驶路线上时没有显示警报,任何人都可以帮助我吗?
我尝试了所有方法,但在驾驶路线鼠标悬停时不起作用。

注意:我是在行驶路线上执行此操作,而不是在折线上。

    <!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>
4

1 回答 1

0

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

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>
于 2013-07-19T13:41:42.107 回答