views:

35

answers:

2

Maybe I'm going completely off track with what I'm trying to do, but I'm tempted just to go back to version 2 because I got that working easily (but I would like to be mobile-friendly).

I'm trying to generate a few markers, and to save code, I've put the marker generation into a for loop, which loops through an array of markers (there are actual values instead of (long,lat,x,x,x) in the real array).

Is something just going completely over my head?

 function initialize() {
    var latlng = new google.maps.LatLng(25, 15);
    var myOptions = {
      zoom: 2,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


var myMarker = new Array();
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 

for(var i = 0; i < myMarker.length; i++) {
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(myMarker[i](1), myMarker[i](2)), 
        map: map, 
        title: myMarker[i](5)
      });
    var infowindow = new google.maps.InfoWindow({content: myMarker[i](5)}); 
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
}

}

This code works when I make the markers individually.

+3  A: 
Pointy
+1  A: 

Hmm few things I noticed are the array definitions and accessing them. eg

// here your defining the array, quick tip just use [] instead of new Array(); its faster, in both execution and to type :)
var myMarker = new Array(); 

//here your defining the 1st value in the array at the 0 index
myMarker[0] = new Array(long,lat,x,x,x); 

//but again here your overriding the first value of the array at 0 index
myMarker[0] = new Array(long,lat,x,x,x); 

so it should look more like this

var myMarker = new Array();
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[1] = new Array(long,lat,x,x,x); 
myMarker[2] = new Array(long,lat,x,x,x); 
myMarker[3] = new Array(long,lat,x,x,x); 
myMarker[4] = new Array(long,lat,x,x,x); 
myMarker[5] = new Array(long,lat,x,x,x); 
myMarker[6] = new Array(long,lat,x,x,x); 

and then when you go to access the values in your array your using

title: myMarker[i](5)

You should have, assuming you have 5 values in each marker array

title: myMarker[i][4]

arrays in javascript are 0 based indexing and use square barkets [] not parentheses.

edit: grammar fixes :)

RueTheWhirled