Google maps API v3.0 - 显示方向路径上方的距离和估计时间

时间:2015-06-10 06:17:21

标签: javascript google-maps google-maps-api-3

我希望我的指示看起来像我提供的图像中的指示。 enter image description here

以下是我的代码,几乎所有内容都来自Google网站上的示例。

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var haight = new google.maps.LatLng(37.7699298, -122.4469157);

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var mapOptions = {
    zoom: 14,
    center: haight
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);
}

function calcRoute() {
  var request = {
      origin: START, // Ill add both places later
      destination: END,
      travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
setTimeout(function(){
  calcRoute();
}, 2000);
    </script>

PS。是否可以在图片中看到方向的“点线”路径?

1 个答案:

答案 0 :(得分:1)

以下是虚线方向折线的示例:

var lineSymbol = {
    path: google.maps.SymbolPath.CIRCLE,
    fillOpacity: 1,
    scale: 3
};

var polylineDotted = new google.maps.Polyline({
    strokeColor: '#0eb7f6',
    strokeOpacity: 0,
    fillOpacity: 0,
    icons: [{
        icon: lineSymbol,
        offset: '0',
        repeat: '10px'
    }],
});

var rendererOptions = {
    map: map,
    suppressMarkers: false,
    polylineOptions: polylineDotted
};

以下小提琴演示了如何使用InfoWindow实现整个事物。

JSFiddle demo