Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
将坐标从.txt文件放入Javascript数组_Javascript_Arrays_Google Maps_Coordinates_Google Maps Markers - Fatal编程技术网

将坐标从.txt文件放入Javascript数组

将坐标从.txt文件放入Javascript数组,javascript,arrays,google-maps,coordinates,google-maps-markers,Javascript,Arrays,Google Maps,Coordinates,Google Maps Markers,我得到了具有以下坐标的txt文件: 21.178797 56.888384 21.373250 56.588044 24.136921 56.965521 26.231814 56.858971 24.123382 56.951146 25.399601 57.540989 24.542900 57.090442 我需要将它们放在这种类型的数组中: var locations = [ ['Title A', 3.180967,101.715546, 1], ['Title B', 3.20

我得到了具有以下坐标的txt文件:

21.178797 56.888384
21.373250 56.588044
24.136921 56.965521
26.231814 56.858971
24.123382 56.951146
25.399601 57.540989
24.542900 57.090442
我需要将它们放在这种类型的数组中:

var locations = [
 ['Title A', 3.180967,101.715546, 1],
 ['Title B', 3.200848,101.616669, 2],
 ['Title C', 3.147372,101.597443, 3],
 ['Title D', 3.19125,101.710052, 4]
 ];
因此,我可以使用循环放置多个标记,如下所示:

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

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

  }

}
我需要数组看起来像这样:

var locations = [
[3.180967,101.715546],
[3.200848,101.616669],
[3.147372,101.597443],
[3.19125,101.710052]
];

提前感谢)

解析数据

使用
String#split
将文本拆分为一个行数组,然后将每行拆分为一个数组:

var str=`21.178797 56.888384
21.373250 56.588044
24.136921 56.965521
26.231814 56.858971
24.123382 56.951146
25.399601 57.540989
24.542900 57.090442`;
var result=str.split('\n').map(函数(行,索引){
return['Title'+String.fromCharCode(index+65)].concat(line.split('');
});

控制台日志(结果)文件是否在服务器上?或者“用户”将从其计算机加载的本地文件?现在它与.js文件(在计算机上)位于同一文件夹中,我使用Python从Api获取坐标,并将它们放入txt文件中。
var locations = [
[3.180967,101.715546],
[3.200848,101.616669],
[3.147372,101.597443],
[3.19125,101.710052]
];