我下面有一大堆脚本。当我点击标记时,信息窗口显示旧的或来自其他平面的内容。关于可能原因的任何想法?非常感谢任何帮助。我已经在堆栈溢出上查看了其他类似的东西但是无法让它们工作。
var infoWindows = {};
var markers = {};
function getIconForPlane(value) {
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) {
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)
}
});
});
}
答案 0 :(得分:0)
通过将iw
更改为createInfoWindow
iw = new google.maps.InfoWindow({
方法中将var iw = new google.maps.InfoWindow({
变量置于本地
function createInfoWindow(value, marker, map) {
// added the var keyword to make the variable local and not global
var iw = new google.maps.InfoWindow({
content: text(value)
});
google.maps.event.addListener(marker, 'click', function() {
// without the var above the iw would point to the last infowindow
// and that is what would open when clicking any marker
iw.open(map, marker);
});
return iw;
}