1

I have a Google Map involving several different arrays of data, and I'm having trouble coding sections of it efficiently. For example, I have this chunk of code, and would like to turn it into a function that runs the same thing for various different names instead of just Wilma. I can easily make an array holding the names, and pass that as a parameter, but then how would I build array names like "data_NAME[k][3]"? I imagine it must be simple to concatenate a string with part of an array name, but I'm stuck on syntax. Any help is greatly appreciated.

if (currentcroc == 'Wilma') {

    for (k = 0; k < data_wilma.length; k++) {

        var pointmarker = new google.maps.Marker({
            position: latlng_wilma[k],
            icon: markerType['point'+data_wilma[k][3]],
            shadow: markerType['pointshadow'],
            shape: pointshape,
            map:map,
            zIndex: 4                           
        });

        pointarray.push(pointmarker);


        (function (k, pointmarker) {

            google.maps.event.addListener(pointmarker, 'mouseover', function() {        
                tooltip.show('Wilma: '+data_wilma[k][2]);
            });

            google.maps.event.addListener(pointmarker, 'mouseout', function() {
                tooltip.hide();
            });

        })(k, pointmarker); 

    }   

}

2 Answers 2

3

Use an associative array (map) where the key is the name (wilma, for example) and the value is the 2d-array:

var nameToDataMap = {
  wilma: [ [0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]
         ],
  fred: [ [0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]
        ]
};

var nameToLatLongMap = {
   wilma: [ ... ],
   fred: [ ... ]
};

Then your code becomes:

var data = nameToDataMap[currentcroc];
var latLong = nameToLatLongMap[currentcroc];

for (k = 0; k < data.length; k++) {

    var pointmarker = new google.maps.Marker({
        position: latLong[k],
        icon: markerType['point' + data[k][3]],
        shadow: markerType['pointshadow'],
        shape: pointshape,
        map:map,
        zIndex: 4                           
    });

    pointarray.push(pointmarker);

    (function (k, pointmarker) {

        google.maps.event.addListener(pointmarker, 'mouseover', function() {        
            tooltip.show(currentcroc + ': ' + data[k][2]);
        });

        google.maps.event.addListener(pointmarker, 'mouseout', function() {
            tooltip.hide();
        });

    })(k, pointmarker); 
}   
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! I was approaching the problem from completely the wrong angle.
0

As long as you're accessing the attribute from somewhere (eg. if it is local) you can access attributes as key fields.

Eg.

tooltip.show('Wilma: ' + this['data_' + 'wilma'][k][2]);

Apply variable names and abstractions as necessary :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.