-1

Below is the code of my website. It refreshes every second. Upon refreshing the new lat/lng it will load the new markers but it keeps the old markers. I have tried many forums etc with no luck. Any help is appreciated thanks.

function initialize() {

var mapOptions = {
    center: new google.maps.LatLng(-36.363, 175.044),
    zoom: 5,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}; 

var infoWindow = new google.maps.InfoWindow();
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
window.setInterval(function() {
        readData();
    }, 1000);
}
   function readData() {

    $.getJSON('https://crossorigin.me/http://radar1.ddns.net:3080/data/aircraft.json', function (data) {
        $.each(data.aircraft, function (i, value) {



            var myLatlng = new google.maps.LatLng(value.lat, value.lon, value.flight, value.altitude);
        var marker = new google.maps.Marker({
                position: myLatlng,
                icon: 'airplane.jpg',
                map: map,
                title: "Callsign   Altitude  " + value.flight + value.altitude


            });

}

            var infowindow = new google.maps.InfoWindow({
                 content: '<b>Speed:</b> ' + value.speed+  '<b>   HEX:</b>' + value.hex+ '<b>   Altidtude:   </b>' +value.altitude + '<b>    Vertical Rate:  </b>' +value.vert_rate


});

 google.maps.event.addListener(marker, 'click', function() {
 infowindow.open(map, marker);
});






        });
    });

}

1 Answers1

-1

Here's how you create / update markers and infoWindows:

var infoWindows = {};
var markers = {};

function initMap() {
  var mapOptions = {
    center: new google.maps.LatLng(-36.363, 175.044),
    zoom: 5,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  window.setInterval(readData, 1000);
}

function text(value) {
  return '<b>Speed: </b> ' + value.speed + '<br><b>HEX: </b>' + value.hex + '<br><b>Altitude: </b>' + value.altitude + '<br><b>Vertical Rate: </b>' + value.vert_rate;
}

function createInfoWindow(value, marker, map) {
  iw = new google.maps.InfoWindow({
    content: text(value)
  });
  google.maps.event.addListener(marker, 'click', function() {
    iw.open(map, marker);
  });
  return iw;
}

function readData() {
  $.getJSON('https://crossorigin.me/http://radar1.ddns.net:3080/data/aircraft.json', function(data) {
    $.each(data.aircraft, function(i, value) {
      var myLatlng = new google.maps.LatLng(value.lat, value.lon, value.flight, value.altitude);
      if (markers[value.squawk]) {
        markers[value.squawk].setPosition(myLatlng);
        console.log("moving marker for " + value.squawk);
        infoWindows[value.squawk].setContent(text(value));
      } else {
        // create new
        markers[value.squawk] = new google.maps.Marker({
          position: myLatlng,
          // icon: 'airplane.jpg',
          map: map,
          title: "Callsign: " + value.flight + ", Altitude: " + value.altitude
        });
        console.log("creating marker for " + value.squawk);
        infoWindows[value.squawk] = createInfoWindow(value, markers[value.squawk] ,map)
      }
    });
  });
}
ChrisG
  • 8,206
  • 4
  • 20
  • 34