映射api3加载xml标记延迟

时间:2012-04-29 21:26:05

标签: xml google-maps-api-3 settimeout

我从谷歌地图api做了一个tutorial,用数据库和php加载标记,生成的xml文件到地图。现在我正在尝试使用setTimeout添加延迟,以便像这样example一个接一个地删除一个标记。我非常初学,如果有人能告诉我怎么做,我会很开心!

downloadUrl("php/addLocations.php", function (data) {
    var xml = parseXml(data);
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
        var name = markers[i].getAttribute("name");
        var address = markers[i].getAttribute("address");
        var type = markers[i].getAttribute("type");
        var profile_image = markers[i].getAttribute("myimage");
        var point = new google.maps.LatLng(
        parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
        var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + "<img src='uploads/" + profile_image + "' width='100px' />";
        var icon = customIcons[type] || {};
        var marker2 = new google.maps.Marker({
            map: bigmap,
            position: point,
            icon: icon.icon,
            animation: google.maps.Animation.DROP,
            shadow: icon.shadow
        });
        bindInfoWindow(marker2, bigmap, infoWindowLoc, html);
    }
});


var infoWindowLoc = new google.maps.InfoWindow;

function bindInfoWindow(marker2, bigmap, infoWindowLoc, html) {
    google.maps.event.addListener(marker2, 'click', function () {
        //openMarkerBox(); 
        // panning map
        bigmap.panTo(marker2.position);
        infoWindowLoc.setContent(html);
        infoWindowLoc.open(bigmap, marker2);
    });
}

function downloadUrl(url, callback) {
    var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;

    request.onreadystatechange = function () {
        if (request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request.responseText, request.status);
        }
    };

    request.open('GET', url, true);
    request.send(null);
}

function parseXml(str) {
    if (window.ActiveXObject) {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(str);
        return doc;
    } else if (window.DOMParser) {
        return (new DOMParser).parseFromString(str, 'text/xml');
    }
}

function doNothing() {
    alert("shit")
}

2 个答案:

答案 0 :(得分:0)

好吧,如果您检查example处的代码,那么您需要的一切就在那里。看看:

1 - 附加到投标标记按钮的onclick处理程序:

`<button id="drop" onclick="drop()">Drop Markers</button>`

onclick的在线文档可用:at this link

2 - 示例中的drop()函数:

function drop() {
    for (var i = 0; i < neighborhoods.length; i++) {
        setTimeout(function() {
            addMarker();
        }, i * 200);
    }
}

setTimeout功能的在线文档可用:at this link

3 - 示例中的addMarker()函数:

function addMarker() {
    markers.push(new google.maps.Marker({
        position:  neighborhoods[iterator],
        map:       map,
        draggable: false,
        animation: google.maps.Animation.DROP
    }));
    iterator++;
}

google.maps.marker动画的在线文档可用:at this link

浏览代码,深入挖掘,你应该能够解决这个问题。

答案 1 :(得分:0)

好的,这是一个似乎有效的解决方案。 谢谢Sean 4你的时间和tipps。

var iterator = 0;
var marker2;
var customIcons = {art: {icon: 'img/art_marker.png',},graff: {icon: 'img/graff_marker.png',}};

downloadUrl("php/addLocations.php", function (data) {
    var xml = parseXml(data);
    var markers = xml.documentElement.getElementsByTagName("marker");

    for (var i = 0; i < markers.length; i++) {

    myFunction();
    function myFunction() { 
        var name = markers[i].getAttribute("name");
        var address = markers[i].getAttribute("address");
        var type = markers[i].getAttribute("type");
        var profile_image = markers[i].getAttribute("myimage");
        var point = new google.maps.LatLng(
        parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));

        setTimeout(function() { 
            addMarker(name, address, type, profile_image, point);
}, i * 2200);

    }

}
});

function addMarker (name, address, type, profile_image, point) {

        var icon = customIcons[type] || {};
        var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + "<img src='uploads/" + profile_image + "' width='100px' />";
        var marker2 = new google.maps.Marker({
            map: bigmap,
            position: point,
            icon: icon.icon,
            animation: google.maps.Animation.DROP,
            shadow: icon.shadow
        });
        bindInfoWindow(marker2, bigmap, infoWindowLoc, html);
        iterator++;
}