使用谷歌地图绘制路线内的路线和各种站点

时间:2015-04-13 15:05:37

标签: google-maps

我有一个网络应用程序,其中我有一个路线,其中包含纬度和经度信息的停靠列表,我想在谷歌地图上绘制此路线,所有停靠点都使用某个图标或圆圈显示。 我已经完成了文档,但没有找到任何具体的例子。我应该使用折线进行这样的用例

1 个答案:

答案 0 :(得分:1)

Here是一个类似的问题,两者之间没有停顿。您可以调整示例,将waypoint对象添加到您的请求中。

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

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

google.maps.event.addDomListener(window, 'load', initialize);

function displayRoute() {

  var start = new google.maps.LatLng(28.694004, 77.110291);
  var end = new google.maps.LatLng(28.72082, 77.107241);

  var directionsDisplay = new google.maps.DirectionsRenderer(); // also, constructor can get "DirectionsRendererOptions" object
  directionsDisplay.setMap(map); // map should be already initialized.

  var request = {
    origin: start,
    destination: end,
    waypoints: [{
      location: new google.maps.LatLng(28.64, 77.1),
      stopover: true
    }, {
      location: new google.maps.LatLng(28.66, 77.41),
      stopover: true
    }],
    travelMode: google.maps.TravelMode.DRIVING
  };

  var directionsService = new google.maps.DirectionsService();

  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta charset="utf-8">
  <title>Waypoints in directions</title>
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
</head>

<body>
  <div id="map-canvas" style="float:left;width:400px;height:400px;"></div>
</body>

</html>