基于标题更新的图标旋转保持在同一标题中

时间:2017-07-02 06:50:40

标签: javascript google-maps

Hello bellow是违规代码的片段。在JSON文件中是value.track,它使用标记旋转设置地图标记图标。问题是图标旋转保持与第一次告诉的相同。 I.E首次加载时航向为360然后飞机转到180,除非我刷新页面,否则不会旋转图标以匹配航向。我猜这可以用“var”修复一些但不确定在哪里。如果这不是很清楚我很抱歉我对Java Script很新。换句话说,标记位置更新但不是标记旋转!

 var infoWindows = {};
  var markers = {};
 function getIconForPlane(value) {
    var r = 255, g = 255, b = 0;
    return {
        path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
        scale: 5,
        fillColor: 'rgb('+r+','+g+','+b+')',
        fillOpacity: 0.9,
        rotation: value.track
        };
    }

function initialize() {

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>Flight: </b>' + 
value.flight 
+' <br><b>HEX: </b>' + value.hex + '<br><b>Altitude: </b>' + value.altitude 
 + 
 '<br><b>Vertical Rate: </b>' + value.vert_rate +'<br><b>Last radar contact: 
 </b>' +value.seen +'<b>s</b>';
window.setInterval(text, 1000);
}

 function createInfoWindow(value, marker, map) {
 var 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.hex]) {
    markers[value.hex].setPosition(myLatlng);
    console.log("moving marker for " + value.hex);
    infoWindows[value.hex].setContent(text(value));
     } else {
    // create new
    markers[value.hex] = new google.maps.Marker({
      position: myLatlng,
      icon: getIconForPlane(value),
      map: map,
      title: "Callsign: " + value.flight + ", Altitude: " + value.altitude
    });
    console.log("creating marker for " + value.hex);
    infoWindows[value.hex] = createInfoWindow(value, markers[value.hex] 
 ,map)
  }
   });
  });
}

1 个答案:

答案 0 :(得分:0)

更新位置时更新标记的图标:

if (markers[value.hex]) {
  markers[value.hex].setPosition(myLatlng);
  markers[value.hex].setIcon(getIconForPlane(value));
  infoWindows[value.hex].setContent(text(value));
} else {
  // create new
  markers[value.hex] = new google.maps.Marker({
    position: myLatlng,
    icon: getIconForPlane(value),
    map: map,
    title: "Callsign: " + value.flight + ", Altitude: " + value.altitude
  });
  infoWindows[value.hex] = createInfoWindow(value, markers[value.hex],map);
}

(您可能还想更新标题,因为高度可能会发生变化)