将 mouseever 事件添加到谷歌地图方向服务路线

走开

如何向我使用谷歌地图路线服务创建的路线添加工具提示

你可以在这里看到一个例子:

在此处输入图片说明

https://developers.google.com/maps/documentation/javascript/examples/directions-complex

我想要的是,当用户将鼠标悬停在路线(蓝线)上时,工具提示将显示该路线的信息,有没有办法可以绑定一个事件来侦听方向上的“鼠标悬停”?

喜欢

var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({
  draggable: false,
  map: map,
  panel: document.getElementById('right-panel')
});

directionsDisplay.addListener('mouseover', function() {
  console.log("something")
});
地理编码

相关问题:

的DirectionsRenderer没有鼠标悬停事件,折线一样。

从 DirectionsResult 中的数据绘制折线,并将鼠标悬停事件侦听器添加到折线。

function renderDirectionsPolylines(response) {
  for (var i = 0; i < polylines.length; i++) {
    polylines[i].setMap(null);
  }
  var bounds = new google.maps.LatLngBounds();
  var legs = response.routes[0].legs;
  for (i = 0; i < legs.length; i++) {
    var steps = legs[i].steps;
    for (j = 0; j < steps.length; j++) {
      var nextSegment = steps[j].path;
      var stepPolyline = new google.maps.Polyline(polylineOptions);
      for (k = 0; k < nextSegment.length; k++) {
        stepPolyline.getPath().push(nextSegment[k]);
        bounds.extend(nextSegment[k]);
      }
      // add mouseover listener to each segment polyline
      google.maps.event.addListener(stepPolyline, 'mouseover', function(evt) {
        console.log("route mouse over event @" + evt.latLng.toUrlValue(6));
      });
      polylines.push(stepPolyline);
      stepPolyline.setMap(map);
      // route click listeners, different one on each step
      google.maps.event.addListener(stepPolyline, 'click', function(evt) {
        stepDisplay.setContent("you clicked on the route<br>" + evt.latLng.toUrlValue(6));
        stepDisplay.setPosition(evt.latLng);
        stepDisplay.open(map);
      })
    }
  }
  map.fitBounds(bounds);
}

概念证明小提琴

代码片段:

var map;
var stepDisplay

function initMap() {
  var markerArray = [];

  // Instantiate a directions service.
  var directionsService = new google.maps.DirectionsService;

  // Create a map and center it on Manhattan.
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 13,
    center: {
      lat: 40.771,
      lng: -73.974
    }
  });

  // Create a renderer for directions and bind it to the map.
  var directionsDisplay = new google.maps.DirectionsRenderer({
    map: map
  });

  // Instantiate an info window to hold step text.
  stepDisplay = new google.maps.InfoWindow;

  // Display the route between the initial start and end selections.
  calculateAndDisplayRoute(
    directionsDisplay, directionsService, markerArray, stepDisplay, map);
  // Listen to change events from the start and end lists.
  var onChangeHandler = function() {
    calculateAndDisplayRoute(
      directionsDisplay, directionsService, markerArray, stepDisplay, map);
  };
  document.getElementById('start').addEventListener('change', onChangeHandler);
  document.getElementById('end').addEventListener('change', onChangeHandler);
}

function calculateAndDisplayRoute(directionsDisplay, directionsService,
  markerArray, stepDisplay, map) {
  // First, remove any existing markers from the map.
  for (var i = 0; i < markerArray.length; i++) {
    markerArray[i].setMap(null);
  }

  // Retrieve the start and end locations and create a DirectionsRequest using
  // WALKING directions.
  directionsService.route({
    origin: document.getElementById('start').value,
    destination: document.getElementById('end').value,
    travelMode: 'WALKING'
  }, function(response, status) {
    // Route the directions and pass the response to a function to create
    // markers for each step.
    if (status === 'OK') {
      document.getElementById('warnings-panel').innerHTML =
        '<b>' + response.routes[0].warnings + '</b>';
      directionsDisplay.setDirections(response);
      showSteps(response, markerArray, stepDisplay, map);
      directionsDisplay.setMap(null);
      renderDirectionsPolylines(response);
      console.log(directionsDisplay.getDirections());
    } else {
      window.alert('Directions request failed due to ' + status);
    }
  });
}

function showSteps(directionResult, markerArray, stepDisplay, map) {
  // For each step, place a marker, and add the text to the marker's infowindow.
  // Also attach the marker to an array so we can keep track of it and remove it
  // when calculating new routes.
  var myRoute = directionResult.routes[0].legs[0];
  for (var i = 0; i < myRoute.steps.length; i++) {
    var marker = markerArray[i] = markerArray[i] || new google.maps.Marker;
    marker.setMap(map);
    marker.setPosition(myRoute.steps[i].start_location);
    attachInstructionText(
      stepDisplay, marker, myRoute.steps[i].instructions, map);
  }
}

function attachInstructionText(stepDisplay, marker, text, map) {
  google.maps.event.addListener(marker, 'click', function() {
    // Open an info window when the marker is clicked on, containing the text
    // of the step.
    stepDisplay.setContent(text);
    stepDisplay.open(map, marker);
  });
}
var polylineOptions = {
  strokeColor: '#C83939',
  strokeOpacity: 1,
  strokeWeight: 4
};
var polylines = [];

function renderDirectionsPolylines(response) {
  for (var i = 0; i < polylines.length; i++) {
    polylines[i].setMap(null);
  }
  var bounds = new google.maps.LatLngBounds();
  var legs = response.routes[0].legs;
  for (i = 0; i < legs.length; i++) {
    var steps = legs[i].steps;
    for (j = 0; j < steps.length; j++) {
      var nextSegment = steps[j].path;
      var stepPolyline = new google.maps.Polyline(polylineOptions);
      for (k = 0; k < nextSegment.length; k++) {
        stepPolyline.getPath().push(nextSegment[k]);
        bounds.extend(nextSegment[k]);
      }
      google.maps.event.addListener(stepPolyline, 'mouseover', function(evt) {
        console.log("route mouse over event @" + evt.latLng.toUrlValue(6));
      });
      polylines.push(stepPolyline);
      stepPolyline.setMap(map);
      // route click listeners, different one on each step
      google.maps.event.addListener(stepPolyline, 'click', function(evt) {
        stepDisplay.setContent("you clicked on the route<br>" + evt.latLng.toUrlValue(6));
        stepDisplay.setPosition(evt.latLng);
        stepDisplay.open(map);
      })
    }
  }
  map.fitBounds(bounds);
}
/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */

#map {
  height: 100%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#floating-panel {
  position: absolute;
  top: 10px;
  left: 25%;
  z-index: 5;
  background-color: #fff;
  padding: 5px;
  border: 1px solid #999;
  text-align: center;
  font-family: 'Roboto', 'sans-serif';
  line-height: 30px;
  padding-left: 10px;
}

#warnings-panel {
  width: 100%;
  height: 10%;
  text-align: center;
}
<div id="floating-panel">
  <b>Start: </b>
  <input id="start" value="penn station, new york, ny" />
  <b>End: </b>
  <input id="end" value="260 Broadway New York NY 10007" />
</div>
<div id="map"></div>
&nbsp;
<div id="warnings-panel"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

谷歌地图 API 将信息窗口添加到点击事件的标记数组

来自分类Dev

谷歌地图; 将地点卡添加到标记

来自分类Dev

如何将谷歌地图添加到照明元素?

来自分类Dev

将弹出窗口添加到谷歌地图标记

来自分类Dev

目标 c:将事件添加到谷歌日历

来自分类Dev

如何将PreShutdown事件添加到ATL服务?

来自分类Dev

将路线添加到Ember Addon

来自分类Dev

将路线添加到NavigationContainer

来自分类Dev

单击 Angular 2 时将标记添加到谷歌地图

来自分类Dev

尝试将 firebase 库添加到使用谷歌地图 API 的应用程序时出错

来自分类Dev

将搜索框添加到多边形绘图谷歌地图

来自分类Dev

当我尝试将谷歌地图添加到 Xamarin 时找不到错误 API 密钥

来自分类Dev

将事件添加到谷歌日历时出错 - 权限不足

来自分类Dev

将事件添加到CalendarView

来自分类Dev

将Postbuild事件添加到项目

来自分类Dev

将onClick事件添加到append()

来自分类Dev

将onclick事件添加到模板

来自分类Dev

将点击事件添加到矩形?

来自分类Dev

jQuery将事件添加到星级

来自分类Dev

将事件添加到html元素

来自分类Dev

将触摸事件添加到GroupObject

来自分类Dev

C ++-使用lambda将地图添加到地图

来自分类Dev

将表单添加到Web服务

来自分类Dev

将动态参数添加到angular2 +路线

来自分类Dev

将信息窗口添加到路线中的目标标记

来自分类Dev

将孩子添加到父级的Rails路线

来自分类Dev

使用选定的jQuery将事件添加到按钮后,将事件添加到按钮

来自分类Dev

使用PHP将事件添加到贝加尔湖CalDAV服务器

来自分类Dev

尝试将事件动态添加到jQuery事件日历