2

次の問題コードがさらに下にあります。私がこれをしているとき

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]
        });
    }
});

}
4

3 に答える 3

2

適切な初期化により、この問題は解決されます。位置でオブジェクトを初期化してcity[i]、未定義ではなく値を保持するオブジェクトにする必要があります。

    var city = [];   // don't use new Array() !
    var lygStr = '';
    for(var i = 0; i < response.length; i++)
    {
        city[i] = {}; // you need to create an object here

        city[i]["CityName"] = response[i].name;

また、配列ではなくオブジェクトが必要です。配列は数値インデックスのみを持つことができますが、オブジェクトは必要に応じて識別子を持つことができます。

    city[i]['Location']
    // same as
    city[i].Location
于 2013-04-12T12:27:46.553 に答える
0

次の行のようになります。

city[1]["Locations"] =  L.marker([location]).bindPopup(name);

city[1]["Locations"]返されるものに設定されます.bindPopup(name)。これは、undefinedまたは関数である可能性があります。そのメソッドは、呼び出されたオブジェクトを返すように構築されていますか? これだけでどうですか:

city[1]["Locations"] =  L.marker([location]);
city[1]["Locations"].bindPopup(name);
于 2013-04-12T12:29:39.500 に答える
-1

city[i]配列として宣言する前に、配列として使用しています。

var city = []; // don't use the new Array(); syntax
var lygStr = '';

for(var i = 0; i < response.length; i++) {
    // before using city[i] as an array, you must declare it as an array
    city[i] = [];    

    city[i]["CityName"] = response[i].name;
    ...
于 2013-04-12T12:28:03.933 に答える