0

早上好。

我有下一个代码,它将一些标记从 xml 文件放入地图中,每 5 秒我再次调用 xml 文件并更新标记的位置,但问题是信息窗口没有更新。有谁知道如何更新它们=

这里有代码:

//<![CDATA[

var side_bar_html = ""; 


var gmarkers = []; 

var map = null;
var markerclusterer = null;




function myclick(i) {
    google.maps.event.trigger(gmarkers[i], "click");
    map.setZoom(map.getZoom()+2); 
}

function initialize() {
    // create the map
    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);
}
function getMarkers() {
    google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
    });


    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 html="<b>"+alias+" "+speed+" km/h <br/> "+timestamp;


            var marker = createMarker(point,alias+" "+imei,html,estado,alias, speed, timestamp, altitude, angle);
        }   
        markerCluster = new MarkerClusterer(map, gmarkers);

    });
}
var infowindow = new google.maps.InfoWindow(
{ 

    size: new google.maps.Size(150,60)
});

setInterval(function updateMarkers() {
    google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
    });

    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 html="<b>"+alias+" a "+speed+" km/h <br/> "+timestamp;


            var newLatLng = gmarkers[i].setPosition(point,alias+" "+imei,html,estado,alias,speed,timestamp,altitude,angle);


        }   
        markerCluster = new MarkerClusterer(map, gmarkers); 
    });

}, 5000);
//finish updateMarkers

function createMarker(latlng, imei, html, estado, alias, speed, timestamp, altitude, angle) {
    if(estado == 1)
        image = '/artworks/icons/truck_green3.png';
    else
        image = '/artworks/icons/truck_red.png';

    var textoLabel= alias+" speed: "+ speed+" km/h";
    var contentString = html;
    var newLatLng = new MarkerWithLabel({
        position: latlng,
        icon: image,
        // map: map,
        draggable: true,

        flat: true,
        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(newLatLng, 'click', function() {
        infowindow.setContent(timestamp + alias); 
        infowindow.open(map,newLatLng);
    });

    gmarkers.push(newLatLng);

// side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + imei +'<\/a><br>';
}
4

1 回答 1

0

您没有在 setInterval 函数中分配事件处理程序,因此没有 infowindows。

创建标记后将此代码添加到 setInterval()

        google.maps.event.addListener(newLatLng, 'click', function() {
        infowindow.setContent(timestamp + alias); 
        infowindow.open(map,newLatLng);

当您处于循环中时,这可能会导致关闭问题。因此,您可以执行类似将for 循环中的值传递给事件侦听器 - Javascript 中的解决方案。

于 2012-10-31T12:50:04.660 回答