Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.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 将位置从二维数组加载到bMap(一个用于jQuery的谷歌地图插件)_Javascript_Jquery_Json_Google Maps_Multidimensional Array - Fatal编程技术网

Javascript 将位置从二维数组加载到bMap(一个用于jQuery的谷歌地图插件)

Javascript 将位置从二维数组加载到bMap(一个用于jQuery的谷歌地图插件),javascript,jquery,json,google-maps,multidimensional-array,Javascript,Jquery,Json,Google Maps,Multidimensional Array,我正在使用bMap jQuery插件来简化在我的网站上使用谷歌地图的过程。 将标记加载到Google地图的方法是使用以下方法 $('#map').data('bMap').insertMarkers({ "name":"Markers", "data":[ { "lat":51.49757618329838, "lng":-0.1746654510498047, "title":"Science Mus

我正在使用bMap jQuery插件来简化在我的网站上使用谷歌地图的过程。 将标记加载到Google地图的方法是使用以下方法

$('#map').data('bMap').insertMarkers({
    "name":"Markers",
       "data":[
        {
         "lat":51.49757618329838,
         "lng":-0.1746654510498047, 
         "title":"Science Museum", 
         "body":"Exhibition Road, London SW7"
        },{
         "lat":51.47769451182406,
         "lng":-0.0009441375732421875, 
         "title":"Royal Observatory Greenwich", 
         "body":"Blackheath Ave, Greenwich, London SE10"
        } 
       ]
      });
但是,我从如下数组加载位置:

MultiArray = new Array(2)    
MultiArray [0] = new Array(4)    
MultiArray [0][0] = "51.3149757618329838"    
MultiArray [0][1] = "-0.1249757618329838"    
MultiArray [0][2] = "Science Museum"    
MultiArray [0][3] = "Exhibition Road, London SW7"

MultiArray [1] = new Array(4)    
MultiArray [1][0] = "51.47769451182406"   
MultiArray [1][1] = "-0.0009441375732421875"    
MultiArray [1][2] = "Royal Observatory Greenwich"    
MultiArray [1][3] = "Blackheath Ave, Greenwich, London SE10"

MultiArray [2] = new Array(4)    
MultiArray [2][0] = "52.4756451182406"   
MultiArray [2][1] = "-0.0009441323532421875"    
MultiArray [2][2] = "Broadwalk College"    
MultiArray [2][3] = "Springfield Ave, Greenwich, London SE10"
如中所示,如果数组有10个项目,则将所有10个项目添加到映射中。这可能是一个通用的javascript问题。但是我在JSON路径上被误导了,所以我想知道是否还有其他方法


有没有办法做到这一点?我知道如果我使用php并读取数据库的位置,我可以动态生成这样的javascript。但我没有这种奢侈。我有一个javascript变量,它是一个位置数组,我希望使用第一个函数将其加载到地图中。

这是完全可能的。我没有使用多数组和单独添加的数组元素,而是添加了一个要添加到数组中的函数,而不是手动添加[0][1]等

var MultiArray = [];
MultiArray.push({
    "lat": "51.3149757618329838",
    "lng": "-0.1249757618329838",
    "title": "Science Museum",
    "body": "Exhibition Road, London SW7"
});

$('#map').data('bMap').insertMarkers({
    "name":"Markers",
    "data": MultiArray
    });
像往常一样未经测试,希望有帮助

//global mapData variable
var mapData = [];

//example function to add a location to mapData
function addLocation(lat,lng,title,body){
    //push the new Object
    mapData.push({
        'lat':lat,
        'lng':lng,
        'title':title,
        'body':body
    });
}

//example function to actually call your insertMarkers jQuery
function insertMarkers(){
    $('#map').data('bMap').insertMarkers({
    "name":"Markers",
    "data": mapData
    });
}

//example, add a location to mapData 
addLocation("51.3149757618329838",
    "-0.1249757618329838",
    "Science Museum",
    "Exhibition Road, London SW7"
    );
//example, add another location to mapData 
addLocation("51.47769451182406",
    "-0.0009441375732421875",   
    "Royal Observatory Greenwich",
    "Blackheath Ave, Greenwich, London SE10"
    );

//example, call the insertMarkers function (or just replace with your 
//own insertMarkers using the mapData object.
insertMarkers();
由于您特别询问如何将二维数组转换为要提供给map函数的数据,现在开始

//Your array
var MultiArray = new Array(2)    
MultiArray [0] = new Array(4)    
MultiArray [0][0] = "51.3149757618329838"    
MultiArray [0][1] = "-0.1249757618329838"    
MultiArray [0][2] = "Science Museum"    
MultiArray [0][3] = "Exhibition Road, London SW7"

// how to loop and just use the insertLocation function
// provided above
// I really don't recommend this, if you can output the array directly
// you might as well just use the "addLocation" function (or your own variety)
// directly
for(var i = 0; i<MultiArray.length; i++){
    addLocation(
       MultiArray[i][0],
       MultiArray[i][1],
       MultiArray[i][2],
       MultiArray[i][3]
    );
}

这是非常正确的。回答得很好。