这是我的要求
需要创建这样的数组。我使用 Spring mvc + ajax + jquery + google map 3.0 我将这些值作为 json 响应单独获取。
1. city name 2. long 3. lat
var markers = [['Bondi Beach', -33.890542, 151.274856],['Coogee Beach', -33.923036, 151.259052],['Cronulla Beach', -34.028249, 151.157507],['Manly Beach', -33.80010128657071, 151.28747820854187],['Maroubra Beach', -33.950198, 151.259302]];
如何使用 javascript 数组函数创建上述循环。可能记录超过 20,30 是 3D 数组吗?
请帮我解决这个问题
谢谢大家
下面显示我的脚本代码并帮助我修改它以获得我的目标结果
function jsonResponse(){
var longitude=0;
var latitude=0;
var merchant='';
var total =0;
$.ajax({
url: 'merchantsList.html',
dataType: 'json',
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
longitude=data.longitude;
latitude=data.latitude;
merchant=data.merchant;
total = data.length;
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
var markers = [['Bondi Beach', -33.890542, 151.274856],['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],['Manly Beach', -33.80010128657071, 151.28747820854187],
['Maroubra Beach', -33.950198, 151.259302]];
var infowindow = new google.maps.InfoWindow(), marker, i;
for (i = 0; i < markers.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
map: map
});}
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(markers[i][0]);
infowindow.open(map, marker);
};
})(marker, i));
}
最佳答案
一些疯狂的假设:var 标记是示例数据,您希望将其替换为 ajax 查询返回的内容; ajax数据中的城市名称==商家。
如果从商家列表.html 返回的数据有多个商家/长/纬度项目,您需要迭代数据并将每个项目添加到标记数组中。
那将是
var markers = [];
// ajax call start here..
// ...
success: function(data) {
var i, item;
for (i = 0; i < data.length; i++) {
item = data[i];
markers.push([item.merchant, item.longtitude, item.latitude]);
}
},
// ajax call end code here
关于javascript - 如何创建javascript多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21854098/