【问题标题】:Plotting a route and various stops inside a route using google map使用谷歌地图绘制路线和路线内的各个站点
【发布时间】:2015-04-13 15:05:37
【问题描述】:

我有一个 Web 应用程序,其中我有一条路线,其中包含带有纬度和经度信息的站点列表,我想在谷歌地图上绘制这条路线,所有站点都使用某个图标或圆圈显示。 我浏览了文档,但没有找到任何具体的例子。我应该在这样的用例中使用折线

【问题讨论】:

  • 多少站?你有路线折线以及停靠点吗?你见过the documentation on the directions service吗?
  • 如果你想要一条真实的路线覆盖街道,折线不是你想要的,因为它们会直接连接站点。

标签: google-maps


【解决方案1】:

Here 是一个类似的问题,中间没有停止。您可以调整示例,将航点对象添加到您的请求中。

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>

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多