0

我已经从谷歌地图 api制作了一个教程,以将生成的 xml 文件从数据库和 php 中加载到地图中。现在我正在尝试使用 setTimeout 左右添加一个延迟,以像这个示例一样一个接一个地放置一个标记。我非常初学者,如果有人能告诉我如何做到这一点,我会非常高兴!

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")
}
4

2 回答 2

0

好吧,如果您检查示例中的代码,您需要的一切都在那里。看一眼:

1 -onclick附加到Drop Markers按钮的处理程序:

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

提供在线文档onclick在此链接

2 -drop()你的例子中的功能:

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

setTimeout该函数的在线文档可用:在此链接

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提供动画在线文档:在此链接

浏览代码,深入挖掘,你应该能够弄清楚这一点。

于 2012-05-01T00:58:37.733 回答
0

好的,这是一个似乎可行的解决方案。谢谢你 Sean 4 你的时间和小费。

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++;
}
于 2012-05-01T15:19:35.150 回答