我有以下問題代碼是更進一步。 當我這樣做我的數組未定義
city[i] = response[i].name;
我可以打印出每個城市我的每一個名字。但現在我想有一個多維數組,因爲我也想保存下面的代碼
L.marker([response[i].lat, response[i].lon]).bindPopup(response[i].name);
而且我認爲我可以將它保存在一個多維數組,所以當我們作爲一例
city[1]["CityName"] = "New York"
city[1]["Locations"] = L.marker([location]).bindPopup(name);
所以,現在當我打電話city[1]['Locations']
我得到L.Marker,對吧?
這這裏是我的代碼
function init()
{
region = 'all';
var url = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution = "(c) OSM contributors, ODBL";
var minimal = L.tileLayer(url, {styleID: 22677, attribution: attribution});
$.ajax
({
type: 'GET',
url: 'webservice.php',
data: {region: region},
success: function(response, textStatus, XMLHttpRequest)
{
var city = new Array();
var lygStr = '';
for(var i = 0; i < response.length; i++)
{
//alert(response[i].lat + " | " + response[i].lon + " | " + response[i].name);
alert(response[i].name);
city[i]["CityName"] = response[i].name;
//L.marker([response[i].lat, response[i].lon]).bindPopup(response[i].name);
if(i + 1 == response.length)
{
lygStr += city[i]["CityName"];
}
else
{
lygStr += city[i]["CityName"] + ", ";
}
}
alert("Test" + lygStr);
var cities = L.layerGroup([lygStr]);
map = L.map("map1",
{
center: new L.Latlng(resposne[1].lat, response[0].lon),
zoom: 10,
layers: [minimal, cities]
});
}
});
}
http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – gearsdigital