Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a update based on your updated code. This should be pretty close. You need to <em>push</em> each position onto the <code>flightPlanCoordinates</code> array, not assign to that variable. Also that array doesn't need to be global. Regarding adding the polyline to the map, that's either the <code>.setMap()</code> call in the original code, or you can use the <code>map</code> property when you create the polyline, as you do for the markers and shown in the code below.</p> <p>I also pushed the polyline onto the <code>markers</code> array so it will be removed along with the markers. You may want to change the name of this array to reflect that it's not just markers but basically is everything you want to clear from the map on an update.</p> <p>I also moved this clearing code into the <code>$.getJSON()</code> callback instead of doing it before you make the call. This will give a smoother update instead of blanking the markers and polyline for a short time while you wait for the new data.</p> <p>Another tip: never use a <code>for</code>..<code>in</code> loop on an array (the loop that clears the markers from the map with <code>setMap(null)</code>). Use a numeric <code>for</code> loop instead.</p> <pre><code>var map; var markers=[]; var timeout=5000; var interval=null; function init2(){ var myLatlng = new google.maps.LatLng(45.7997016667,15.97143); var mapOptions = { zoom: 14, center: myLatlng, zoomControl: true, zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL }, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); update_markers(); } function update_markers(){ console.log('update...'); $.getJSON('/get', function(d){ for(var i = 0; i &lt; markers.length; ++i){ markers[i].setMap(null); } markers=[]; var polylineCoordinates = []; var l=d.length; for(var x=l-5;x&lt;l;x++){ var f1=parseFloat(d[x].lat); var f2=parseFloat(d[x].long); if(f1&gt;100){ continue; f1=f1/100.0; f2=f2/100.0; } var position = new google.maps.LatLng(f1, f2); markers.push(new google.maps.Marker({ position: position, map: map, title: d[x].time })); polylineCoordinates.push( position ); } markers.push(new google.maps.Polyline({ map: map, path: polylineCoordinates, strokeColor: '#FF0000', strokeOpacity: 1.0, strokeWeight: 2 })); } ); interval=setTimeout(update_markers, timeout); } google.maps.event.addDomListener(window, 'load', init2); $(function(){ $('#timeout_sel').change(function(){ clearTimeout(interval); timeout=$(this).val(); update_markers(); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload