Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图API v3动态生成行?_Javascript_Google Maps Api 3_Google Polyline - Fatal编程技术网

Javascript 谷歌地图API v3动态生成行?

Javascript 谷歌地图API v3动态生成行?,javascript,google-maps-api-3,google-polyline,Javascript,Google Maps Api 3,Google Polyline,我正在开发一个谷歌地图原型,我想画一条线连接我地图上的每个标记。但是,目前我正在硬编码线的坐标,将其与用于标记的坐标分开,并想知道如何使其成为动态的,以便只需添加标记,就可以从这些点自动生成线 我希望动态生成的脚本部分被注释 以下是我当前的代码: <!DOCTYPE html> <html> <head> <title>Travel Log</title> <meta name="viewport" conte

我正在开发一个谷歌地图原型,我想画一条线连接我地图上的每个标记。但是,目前我正在硬编码线的坐标,将其与用于标记的坐标分开,并想知道如何使其成为动态的,以便只需添加标记,就可以从这些点自动生成线

我希望动态生成的脚本部分被注释

以下是我当前的代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Travel Log</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
      var map;
      var locations = [
        ['Mankato, MN', 44.1834814, -93.9774519, 1],
        ['Duluth, MN', 46.7649885, -92.1112232, 2],
        ['Rochester, MN', 43.9959876, -92.4811724, 3],
        ['Fargo, ND', 46.8541979, -96.8285138, 4],
        ['Minneapolis, MN', 44.970697, -93.2614785, 5]
      ];
      function initialize() {
        var mapOptions = {
          zoom: 5,
          center: new google.maps.LatLng(46.4418595,-93.3655146)
        };
        map = new google.maps.Map(document.getElementById('map-canvas'),
            mapOptions);

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

        var marker, i;

        for (i = 0; i < locations.length; i++) {  
          marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
          });

          google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, marker);
            }
          })(marker, i));
        }

        /////////////////////////////////////////
        //This is the part I want to be dynamic//
        /////////////////////////////////////////
        var lineCoordinates = [
          new google.maps.LatLng(44.1834814, -93.9774519),
          new google.maps.LatLng(46.7649885, -92.1112232),
          new google.maps.LatLng(43.9959876, -92.4811724),
          new google.maps.LatLng(46.8541979, -96.8285138),
          new google.maps.LatLng(44.970697, -93.2614785)
        ];

        var tripPath = new google.maps.Polyline({
          path: lineCoordinates,
          geodesic: true,
          strokeColor: '#000',
          strokeOpacity: 1.0,
          strokeWeight: 2
        });

        tripPath.setMap(map);
      }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

旅行日志
html,正文,#地图画布{
身高:100%;
边际:0px;
填充:0px
}
var映射;
变量位置=[
[Mankato,MN',44.1834814,-93.9774519,1],
[Duluth,MN',46.7649885,-92.1112232,2],
[Rochester,MN',43.9959876,-92.4811724,3],
[Fargo,ND',46.8541979,-96.8285138,4],
[明尼苏达州明尼阿波利斯,44.970697,-93.2614785,5]
];
函数初始化(){
变量映射选项={
缩放:5,
中心:新google.maps.LatLng(46.4418595,-93.3655146)
};
map=new google.maps.map(document.getElementById('map-canvas'),
地图选项);
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i

您可以很好地使用

var lineCoordinates = locations.map(function (val) {
    return new google.maps.LatLng(val[1], val[2]);
});

在循环内填充
线坐标