0

I have the below array of coordinates:

var coords = [35.77204705542798, -5.815865197320899, 35.77205120747819, -5.815754188240848, 35.77197468036722, -5.815810097181759, 35.77201185726312, -5.816182008817898, 35.77188028308802, -5.816782866625928, 35.77144809183601, -5.817054836919457, 35.77130127311978, -5.817231221149015, 35.77121654704168, -5.817323279458099, 35.77197602552531, -5.818491135420929, 35.77211527443405, -5.818831898394636, 35.77220613582161, -5.819054426882189, 35.77225761824354, -5.819182167400329, 35.77557809840525, -5.819685065789988, 35.77522352494348, -5.821346612263469, 35.77815743782872, -5.822735160242799, 35.77863231735067, -5.822959942846667, 35.77915215012052, -5.823206036666789, 35.77917987363854, -5.82321907562955, 35.77913908191616, -5.825751400638378, 35.77947725976961, -5.826044519616627, 35.77923063670355, -5.82893344672563, 35.77920722271806, -5.829544463218058, 35.77924325929096, -5.82965650991142, 35.77913169320556, -5.830906964998152, 35.77917349450947, -5.830173084585434, 35.78563643141488, -5.829373128632887, 35.7859055726137, -5.829317961099034, 35.78659089504106, -5.829249272155759, 35.78704770052305, -5.829163948962961, 35.78696081760869, -5.829114610331397, 35.78687737614365, -5.829067226012621, 35.78679073445069, -5.829018024365507, 35.78661289235281, -5.828929232334034, 35.78652027010703, -5.828884857172518, 35.78643576729606, -5.828844387274908, 35.78635159916791, -5.828804167105649, 35.7862672257822, -5.828763857888811, 35.7872100444153, -5.866787159467321]

Basically, the order is like the following ([lat,lng,lat,lng........])

var waypts = [];

//should execute for loop here, get lat and lng from the array
stop = new google.maps.LatLng(lat, lng)
waypts.push({
    location: stop,
    stopover: true
});
///

I've tried the below, but it seems to run only once

for(var i=0; i<=coords.length; i++){
//get only even so I can have something like coords.slice(0,2), coords.slice(2,4)
if ((i % 2) == 0){
coords.slice(i+0,i+2)
 }
}

The final result that will be run on the background should be similar to:

stop = new google.maps.LatLng(35.77205120747819, -5.815754188240848)
waypts.push({
    location: stop,
    stopover: true
});

stop = new google.maps.LatLng(35.77197468036722, -5.815810097181759)
waypts.push({
    location: stop,
    stopover: true
});
1
  • @Santi I've posted the desired output Commented Jun 13, 2017 at 19:37

5 Answers 5

1

You could just iterate over every pair :

for(var i=1; i<coords.length; i+=2){
  var lat=coords[i-1],long=coords[i];

 stop = new google.maps.LatLng(lat, lng)
}
Sign up to request clarification or add additional context in comments.

Comments

1

Look this working example, I reduced the number of coordinates to be more readable.

var coords = [35.77204705542798, -5.815865197320899, 35.77205120747819, -5.815754188240848, 35.77197468036722, -5.815810097181759];

for (var i = 0; i <= coords.length - 2; i++) {
  if ((i % 2) == 0) {
    console.log("Lat:" + coords[i]);
    console.log("Lon:" + coords[i + 1]);
    /*stop = new google.maps.LatLng(coords[i], coords[i + 1])
    waypts.push({
      location: stop,
      stopover: true
    });*/
  }
}

Comments

1

You can use while and shift it is quite clean.

while(coords.length) stop = new google.maps.LatLng(coords.shift(), coords.shift());

Comments

1

No need to do modular math - just use i and ++i (this will increment i and the loop will process normally)

for(var i=0; i<=coords.length; i += 2) {
    if ((i + 1) <= coords.length) {
        waypts.push({
            location: new google.maps.LatLng(coords[i], coords[i + 1]),
            stopover: true
        });
    }
}

2 Comments

It would be a way cleaner to do i += 2 in the loop header and just i+1 in the body. Mutating the loop var in the body is a big no-no.
@georg - Yeah I was thinking that - I'm sure it'll lead to a headache sooner or later. I edited the answer per your suggestion - thanks for the input!
0

You could convert the coords array into an array of objects, each including a lat and long variable. I'm using different values for each lat/long to make it not so long.

var coords = [{lat:1,long:2},{lat:3,long:4},{lat:5,long:6},{lat:7,long:8}];
for(var i=0; i<=coords.length; i++) {
    // perform rest of operation here
}

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.