2

I have the following problem code is further down. When I'm doing this

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

I can print out every name of every city I have. But now I want to have a multi dimensional array because I want to save also the following code

L.marker([response[i].lat, response[i].lon]).bindPopup(response[i].name);

And I thought that I can save it in a multidimensional array, so when we have as an example

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

So, now when I call city[1]['Locations'] I get the L.Marker, right?

This here is my code

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

}
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
devShuba
  • 91
  • 3
  • 10

3 Answers3

2

Proper initialization will fix this problem - You need to initialize the Object at position city[i] to be an object which holds your values rather than undefined.

    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;

also, you want to have an object rather than an array. Arrays can only have numeric indices while object can have identifiers like you want.

    city[i]['Location']
    // same as
    city[i].Location
Christoph
  • 50,121
  • 21
  • 99
  • 128
  • What do you mean by city[i] = []? When I let it all the same, but init the array as you said I still get an error in this line `city[i]["CityName"] = response[i].name;` – devShuba Apr 12 '13 at 12:29
  • 1
    @devShuba that was a typo, you need a object `{}` and not an array `[]`. – Christoph Apr 12 '13 at 12:31
0

It looks like in this line:

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

that your city[1]["Locations"] will be set to whatever .bindPopup(name) returns. This could be undefined or it could be a function. Is that method constructed to return a object its called on? What about just this:

city[1]["Locations"] =  L.marker([location]);
city[1]["Locations"].bindPopup(name);
shanabus
  • 12,989
  • 6
  • 52
  • 78
-1

You're using city[i] as an array before ever declaring it as such:

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;
    ...
jbabey
  • 45,965
  • 12
  • 71
  • 94
  • actually one should use an object instead of an array here. – Christoph Apr 12 '13 at 12:42
  • Yes, but apparently it is wrong. Arrays have numeric indices. This code accidently works because you can attach properties to the Array-Object. Try the following code: `city=[];city["foo"]="bar";alert(city.length) // 0, meeep`. – Christoph Apr 12 '13 at 13:42