下面的代码我有一些信息窗口显示JSON文件中的数据。我在点击标记时InfoMarkers无法打开时遇到问题。关于我做错了什么的任何帮助都会很棒,因为这对我来说都是非常新的。对不起,如果我是一个痛苦。
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({
content: value.altitude + value.hex
});
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
window.setInterval(function() {
readData();
}, 1000);
}
var marker = {};
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 (marker[value.squawk]) {
marker[value.squawk].setPosition(myLatlng);
console.log("moving marker for " + value.squawk);
} else {
marker[value.squawk] = new google.maps.Marker({
position: myLatlng,
icon: 'airplane.jpg',
map: map,
title: "Callsign Altitude " + value.flight + value.altitude
});
console.log("creating marker for " + value.squawk);
}
marker.addListener('click', function() {
infowindow.open(map, marker);
});
});
});
}
答案 0 :(得分:0)
你的infoWindow位置错误...尝试在标记侦听器之前移动代码
var infowindow = new google.maps.InfoWindow({
content: value.altitude + value.hex
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
答案 1 :(得分:0)
你的代码应该如下所示。我还没有实现,但你必须在每个标记上绑定click事件。
var marker = {};
var objMarker;
$.each(data.aircraft, function(i, value) {
var infowindow = new google.maps.InfoWindow({
content: "content here"
});
var myLatlng = new google.maps.LatLng(value.lat, value.lon, value.flight, value.altitude);
objMarker = new google.maps.Marker({
position: myLatlng,
icon: 'airplane.jpg',
map: map,
title: "Callsign Altitude " + value.flight + value.altitude
});
marker.push(objMarker)
google.maps.event.addListener(marker[i], 'click', function() {
infowindow.open(map, marker[i]);
});
})