谷歌地图 - 绘制多个单独的折线

时间:2015-10-10 11:47:48

标签: google-maps google-polyline

所以我试图在谷歌地图上绘制多个分离的折线。 到目前为止,我没有那么多:

<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条未连接的直线。

2 个答案:

答案 0 :(得分:6)

  1. 一个google.maps.LatLng对象是两个数字;或google.maps.LatLngLiteral是一个带有lat和lng属性的javascript对象,也不是字符串。
  2. 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. 您需要将这些添加到折线的路径中:
    2. 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

      代码段

      &#13;
      &#13;
      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);
      &#13;
      html,
      body,
      #map {
        height: 100%;
        width: 100%;
        margin: 0px;
        padding: 0px
      }
      &#13;
      <script src="https://maps.googleapis.com/maps/api/js"></script>
      <div id="map"></div>
      &#13;
      &#13;
      &#13;

答案 1 :(得分:2)

geocodezip的答案很好!

一项小改进,而不是在地图中添加许多折线元素(以后可能需要您跟踪它们并迭代所有这些元素以便删除或更改它们),您可以将所有路径放在一个多边形对象。

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

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>