Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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 使用文件保存Google Maps的数组项_Javascript_Arrays_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 使用文件保存Google Maps的数组项

Javascript 使用文件保存Google Maps的数组项,javascript,arrays,google-maps,google-maps-api-3,Javascript,Arrays,Google Maps,Google Maps Api 3,我用谷歌地图创建了一个地图,但是它有很多标记,把它们放在同一个html文件中变得很难。要使用标记,我首先创建一个数组,如下所示 var markers = [ ['Balham Leisure Centre',51.441234,-0.152297,'leisure_center.png'], ['Putney Leisure Centre',51.463955,-0.228771,'leisure_center.png'], ['Latchmere Leisure Cent

我用谷歌地图创建了一个地图,但是它有很多标记,把它们放在同一个html文件中变得很难。要使用标记,我首先创建一个数组,如下所示

var markers = [

   ['Balham Leisure Centre',51.441234,-0.152297,'leisure_center.png'],
   ['Putney Leisure Centre',51.463955,-0.228771,'leisure_center.png'],
   ['Latchmere Leisure Centre',51.470967,-0.163805,'leisure_center.png']
]
    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0],
            icon: iconBase + markers[i][3],
        });

}
然后按如下方式使用它们

var markers = [

   ['Balham Leisure Centre',51.441234,-0.152297,'leisure_center.png'],
   ['Putney Leisure Centre',51.463955,-0.228771,'leisure_center.png'],
   ['Latchmere Leisure Centre',51.470967,-0.163805,'leisure_center.png']
]
    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0],
            icon: iconBase + markers[i][3],
        });

}

然后让主页读取该文件并将其放入markers数组中,这样所有内容仍然可以正常工作。

因为您不使用任何框架,所以在原生JS中,您希望使用XMLHttpRequest对象。将数据保存在单独的文件中(例如,您可能希望使用JSON),然后按照下面的代码进行操作

基本用法:

var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    if (req.readyState == XMLHttpRequest.DONE) {
        console.log(req.responseText);
    }
}
req.open('GET', url);
req.send();
阅读更多: