There is an array in which 5 values ​​of longitude and latitude, that is, the location of different objects. Looked at the documentation, made an array like there. Happened. The data is displayed, but in the interval, when data is updated in the array, it does not want to change its position. Why? This is about myLatLng data

Here is a piece of code:

 //Получаю значения и обновляю.ве ок var znak = parseFloat($('td').eq(2).html()) || 0, lng0 = znak; var znac = setInterval(function (){ znak = lng0 = parseFloat($('td').eq(2).html()); },2000); var znak1 = parseFloat($('td').eq(3).html()) || 0, lat1 = znak1; var znac = setInterval(function (){ znak1 = lat1 = parseFloat($('td').eq(3).html()); },2000); console.log(znak); console.log(znak1); 

// then put it in an array;

 var myLatLng ={lat:znak,lng:znak1}; var locations = [ ['Azer', myLatLng, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; 

I add to the map and here everything is well displayed

  var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); 

// but here is the problem: further in the interval the array does not want to change its value

  var setId=setInterval(function (){ for (i = 0; i < locations.length; i++){ marker.setPosition( new google.maps.LatLng(locations[i][1], locations[i][2])) console.log(locations[i][1]); } }, 2500); 
  • I have already seen such a question somewhere :) - Grundy
  • Well, you are lying) I have already done an array))) I will progress)))))) it’s a shame that the rake is the same) - elik
  • need advice help) where what's wrong) Just don't tell me again to say everything) - elik
  • Vsezh seems to be correct ... I put the cycle on a timer; it doesn’t work ... - elik
  • @Grundy where au?) - elik

1 answer 1

The problem is that all 4 positions are applied quickly in a cycle. As a result, the last should always be shown.

And no, it's worse.

  1. 4 markers are created in the first loop.

     var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); } 

    But the link is saved only on the latter. Because of this follows

  2. in the second cycle

     for (i = 0; i < locations.length; i++){ marker.setPosition( new google.maps.LatLng(locations[i][1], locations[i][2])) console.log(locations[i][1]); } 

    You only change the position of the last marker, and it always remains the same as it was, because as a result, the original coordinate is assigned to the marker.

  3. In addition, even if the storage of markers is znak , when the variables znak , and znak1 value in the locations array does not change. That is, you always have permanent coordinates.

For solutions:

  1. Save all markers, for example using an array

     var markers=[]; for (var i = 0; i < locations.length; i++) { markers[i] = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); } 
  2. Update position only markers that need to change, in this case, the first marker, without a cycle.

     markers[0].setPosition( new google.maps.LatLng(locations[i][1], locations[i][2])) 

    or in a cycle to update everything, if everything can change, the current code is already working for this case.

  3. in order for the values ​​in the array to change, you need to change them in the object that is in this array:

     setInterval(function (){ myLatLng.lat = parseFloat($('td').eq(2).html()); myLatLng.lng= parseFloat($('td').eq(3).html()); },2000); 
  4. Since the timers have the same intervals, it makes no sense to start several timers, everything can be placed in one.

  • I could not bear the soul and decided to write an answer?)))) Thanks right now, I will read what he says! - elik
  • Even if you correct the storage of markers, if you change the variables znak, and znak1 does NOT change the value in the locations array, can you tell why this is so? - elik
  • @elik, this is how it works. - Grundy
  • It is clear! Thank you very much right now I try your tips, see what happens) - elik
  • Thanks, everything worked well - elik