Display Element in an Array

Discussion in 'JavaScript' started by hiyatran, Oct 10, 2012.

  1. #1
    I would like to put all my coordinates into an array and have a loop display each one but with a Google function.


    Here's the code, which draws the points on google map:

     var flightPlanCoordinates = [
        new google.maps.LatLng(37.772323, -122.214897),
        new google.maps.LatLng(21.291982, -157.821856),
        new google.maps.LatLng(-18.142599, 178.431),
        new google.maps.LatLng(-27.46758, 153.027892)
      ];
    
    Code (markup):
    I would like to be able to do something like this:

    var arrPos = new Array([37.772323, -122.214897], [21.291982, -157.821856], [-18.142599, 178.431], etc. );
    
    var flightPlanCoordinates = [ + 
       for (i=0; i<arrPos.length; i++){
          new google.maps.LatLng(arrPos[0]) + ", "
       }
       + "];"
    Code (markup):

    I know you can put a loop in an array but is there an alternate method to retrieving the points from the array??

    tks
     
    hiyatran, Oct 10, 2012 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I think what you are looking for is an array's PUSH method.

    
    var arrPos = [
    	[37.772323, -122.214897],
    	[21.291982, -157.821856],
    	[-18.142599, 178.431]
    ];
    
    var flightPlanCoordinates = [];
    for (i=0; i<arrPos.length; i++) {
    	flightPlanCoordinates.push(
    		new google.maps.LatLng(
    			arrPos[i][0],
    			arrPos[i][1]
    		)
    	);
    }
    
    Code (markup):
     
    deathshadow, Oct 13, 2012 IP