Google 地图 - 绘制多条单独的多段线

Google maps - draw multiple separate polylines

所以我试图在 google 地图上绘制多条分离的多段线。 到目前为止我没有那么多:

<script>
var center = new google.maps.LatLng(51.97559, 4.12565);
var map = new google.maps.Map(document.getElementById('map'), {
   center: center,
   zoom: 12,
   mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow({
    map: map
});  

var bounds = new google.maps.LatLngBounds();

// start coordinates
var start = ['51.97559, 4.12565', 
             '55.46242, 8.43872', 
             '49.49259, 0.1065',
             '50.36862, -4.13412']

// end coordinates
var end = ['51.94784, 1.2539', 
           '51.74784, 1.2539', 
           '50.79726, -1.11048',
           '43.45846, -3.80685']

function initialize() {
    for (var i=0; i < end.length; i++){
      calcRoute(start[i], end [i]);
    }
}

function calcRoute(source,destination){
  var polyline = new google.maps.Polyline({
     path: [],
     strokeColor: 'red',
     strokeWeight: 2,
     strokeOpacity: 1
  });

  polyline.setMap(map);          
}
</script>

我发现 here 一个有趣的例子,但它有 DirectionsTravelMode,我只想要两点之间的直线。 所以在我的示例中,我想在地图上绘制 4 条不相连的直线。

  1. 一个google.maps.LatLng对象是两个数字;或者 google.maps.LatLngLiteral 是一个 javascript 对象,带有 lat 和 lng 属性,也不是字符串。
for (var i=0; i < end.length; i++){
  var startCoords = start[i].split(",");
  var startPt = new google.maps.LatLng(startCoords[0],startCoords[1]);
  var endCoords = end[i].split(",");
  var endPt = new google.maps.LatLng(endCoords[0],endCoords[1]);
  calcRoute(startPt, endPt);
  bounds.extend(startPt);
  bounds.extend(endPt);
}
map.fitBounds(bounds);
  1. 您需要将它们添加到折线的路径中:
function calcRoute(source,destination){
  var polyline = new google.maps.Polyline({
     path: [source, destination],
     strokeColor: 'red',
     strokeWeight: 2,
     strokeOpacity: 1
  });

  polyline.setMap(map);          
}

proof of concept fiddle

代码片段:

var map;

function initialize() {

  var center = new google.maps.LatLng(51.97559, 4.12565);
  map = new google.maps.Map(document.getElementById('map'), {
    center: center,
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  var infowindow = new google.maps.InfoWindow({});

  var bounds = new google.maps.LatLngBounds();

  // start coordinates
  var start = ['51.97559, 4.12565',
    '55.46242, 8.43872',
    '49.49259, 0.1065',
    '50.36862, -4.13412'
  ]

  // end coordinates
  var end = ['51.94784, 1.2539',
    '51.74784, 1.2539',
    '50.79726, -1.11048',
    '43.45846, -3.80685'
  ]

  for (var i = 0; i < end.length; i++) {
    var startCoords = start[i].split(",");
    var startPt = new google.maps.LatLng(startCoords[0], startCoords[1]);
    var endCoords = end[i].split(",");
    var endPt = new google.maps.LatLng(endCoords[0], endCoords[1]);
    calcRoute(startPt, endPt);
    bounds.extend(startPt);
    bounds.extend(endPt);
  }
  map.fitBounds(bounds);
}

function calcRoute(source, destination) {
  var polyline = new google.maps.Polyline({
    path: [source, destination],
    strokeColor: 'red',
    strokeWeight: 2,
    strokeOpacity: 1
  });

  polyline.setMap(map);
}

google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

geocodezip 回答得好!

一个小的改进,而不是向地图添加许多折线元素(稍后可能需要您跟踪它们并遍历所有它们以删除或更改它们),您可以将所有路径放入一个多边形对象。

工作fiddle:http://jsfiddle.net/syoels/hyh81jfz/1/ (我使用了 geocodezip 的 fiddle 并做了一些小改动)

var geocoder;
var map;

function initialize() {

  var center = new google.maps.LatLng(51.97559, 4.12565);
  map = new google.maps.Map(document.getElementById('map'), {
    center: center,
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  var bounds = new google.maps.LatLngBounds();

  // start coordinates
  var start = ['51.97559, 4.12565',
    '55.46242, 8.43872',
    '49.49259, 0.1065',
    '50.36862, -4.13412'
  ];

  // end coordinates
  var end = ['51.94784, 1.2539',
    '51.74784, 1.2539',
    '50.79726, -1.11048',
    '43.45846, -3.80685'
  ];

  var paths = [];

  for (var i = 0; i < end.length; i++) {
    var startCoords = start[i].split(",");
    var startPt = new google.maps.LatLng(startCoords[0], startCoords[1]);
    var endCoords = end[i].split(",");
    var endPt = new google.maps.LatLng(endCoords[0], endCoords[1]);
    paths.push([startPt, endPt]);

    bounds.extend(startPt);
    bounds.extend(endPt);
  }
  map.fitBounds(bounds);

  var polyline = new google.maps.Polygon({
    paths: paths,
    strokeColor: 'red',
    strokeWeight: 2,
    strokeOpacity: 1
  });

  polyline.setMap(map);

}


google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
<div id="map"></div>