我有这段代码,它加载一个 xml 文件并将一些标记推送到地图中。此 xml 文件每 5 秒再次调用一次,我使用 marker.setPosition 更新标记的位置,我想要的是在单击标记时更新信息窗口。现在的问题是总是显示在开始时加载的第一个数据。因此,如果有人知道如何编码,我将不胜感激。
//<![CDATA[
var gmarkers = [];
var map = null;
var infoWindow = null;
var markerclusterer = null;
var infowindow = new google.maps.InfoWindow();
function createMarker(latlng, imei, alias, speed, timestamp, estado, conectado, altitude, angle, soloFecha, hora, html, fechaUk) {
if(estado == 1)
image = '/artworks/icons/truck_green3.png';
else
image = '/artworks/icons/truck_red.png';
var textoLabel= html
var contentString = html;
var marker = new MarkerWithLabel({
position: latlng,
icon: image,
draggable: true,
// map: map,
labelContent: textoLabel,
labelAnchor: new google.maps.Point(40, 0),
labelClass: "labels", // the CSS class for the label
labelStyle: {opacity: 0.50},
zIndex: Math.round(latlng.lat()*-100000)<<5
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
map.setZoom(map.getZoom()+2);
map.setCenter(latlng);
});
gmarkers.push(marker);
}//end function createMarker
// This function picks up the click and opens the corresponding info window
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}//end function picks up the click
// create the map
function initialize() {
var myOptions = {
zoom: 12,
center: new google.maps.LatLng(37.169619,-3.756981),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}// end create the map
setInterval(getMarkers, 5000); // call every 5 seconds the function getMarkers
function getMarkers() {
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
// Read the data from example.xml
downloadUrl("vehiculos.asp", function(doc) {
var xmlDoc = xmlParse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var imei = markers[i].getAttribute("imei");
var alias = markers[i].getAttribute("alias");
var speed= markers[i].getAttribute("speed");
var timestamp= markers[i].getAttribute("timestamp");
var estado= markers[i].getAttribute("estado");
var conectado= markers[i].getAttribute("conectado");
var altitude= markers[i].getAttribute("altitude");
var angle= markers[i].getAttribute("angle");
var soloFecha= markers[i].getAttribute("soloFecha");
var hora= markers[i].getAttribute("hora");
var fechaUk= markers[i].getAttribute("fechaUk");
var html="<b>"+alias+"</b><br> a una velocidad de "+speed+" km/h <br/> ultima posicion a las: "+timestamp;
// start, check if marker is already created, if so we update it, if not we create it
if (gmarkers[i] != undefined)
gmarkers[i].setPosition(point);
else
var marker = createMarker(point, imei, alias, speed, timestamp, estado, conectado, altitude, angle, soloFecha, hora, html, fechaUk);
// end, check if marker is already created, if so we update it, if not we create it
}
markerCluster = new MarkerClusterer(map, gmarkers);
});
}//end function getMarkers