首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Google Maps API:使用setDirections引用标记

Google Maps API:使用setDirections引用标记
EN

Stack Overflow用户
提问于 2018-06-08 05:12:14
回答 1查看 460关注 0票数 0

通过Google Maps Javascript API,用户可以使用DirectionsServiceDirectionsRenderer (examples)显示它们之间的标记和方向。

代码语言:javascript
复制
const directionsService = new google.maps.DirectionsService()
const directionsDisplay = new google.maps.DirectionsRenderer()

const mapOptions = {
  zoom:7,
  center: loc // some coordinates
}
const map = new google.maps.Map(document.getElementById('map'), mapOptions)
directionsDisplay.setMap(map)

const request = {
  origin: start, // some coordinates
  destination: end, // some coordinates
  travelMode: 'DRIVING'
}

directionsService.route(request, function(result, status) {
  if (status == 'OK') {
    directionsDisplay.setDirections(result)
  }
})

上面将在地图上显示两个标记(开始和结束),并在它们之间突出显示路线。我需要的是创建的每个标记的引用(设置自定义标签,注册事件等)。有没有办法在仍然使用directionsDisplay.setDirections(result)的情况下做到这一点,或者我需要手动创建所有内容?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 05:33:25

你不能(很容易/安全地)获得对标记的引用。您可以设置DirectionsRenderersuppressMarkers选项,然后根据响应中的数据创建您自己的“自定义”标记。

代码语言:javascript
复制
 directionsService.route(request, function(result, status) {
  if (status == 'OK') {
    directionsDisplay.setDirections(result);
    createMarker(result.routes[0].legs[0].start_location, "A", "start marker", map, infowindow);
    var lastLeg = result.routes[0].legs.length - 1;
    createMarker(result.routes[0].legs[lastLeg].end_location, "B", "end marker", map, infowindow);
  }
});

proof of concept fiddle

代码片段:

代码语言:javascript
复制
function initialize() {
  const directionsService = new google.maps.DirectionsService();
  const directionsDisplay = new google.maps.DirectionsRenderer({
    suppressMarkers: true
  });
  var infowindow = new google.maps.InfoWindow();
  const mapOptions = {
    zoom: 7,
    center: new google.maps.LatLng(37.4419, -122.1419) // some coordinates
  }
  const map = new google.maps.Map(document.getElementById('map'), mapOptions)
  directionsDisplay.setMap(map)

  const request = {
    origin: {
      lat: 37.4418834,
      lng: -122.1430195
    }, // some coordinates
    destination: {
      lat: 37.4529598,
      lng: -122.1817252
    }, // some coordinates
    travelMode: 'DRIVING'
  }

  directionsService.route(request, function(result, status) {
    if (status == 'OK') {
      directionsDisplay.setDirections(result);
      createMarker(result.routes[0].legs[0].start_location, "A", "start marker", map, infowindow);
      var lastLeg = result.routes[0].legs.length - 1;
      createMarker(result.routes[0].legs[lastLeg].end_location, "B", "end marker", map, infowindow);
    }
  })
}
google.maps.event.addDomListener(window, "load", initialize);
// Adds a marker to the map.
function createMarker(location, label, content, map, infowindow) {
  var marker = new google.maps.Marker({
    position: location,
    label: label,
    title: label,
    map: map
  });
  marker.addListener('click', function(e) {
    infowindow.setContent(content);
    infowindow.open(map, this);
  })
}
代码语言:javascript
复制
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
代码语言:javascript
复制
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50750226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档