0

我能够从 XML 文件中提取我的标记并将它们放在谷歌地图上,但现在我希望每个标记在单击后链接到其各自的 url。有人可以让我知道我做错了什么吗?出于某种原因,我只得到最后一个条目的网址。

downLoad("phpsqlajax.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
        var city = markers[i].getAttribute("city");
        var state = markers[i].getAttribute("state");
        var country = markers[i].getAttribute("country");
        var markerUrl = markers[i].getAttribute("url");
        var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
        var marker = new google.maps.Marker({
            position: point,
            map: googleMap,
            icon: 'map-pin.png',
            url: markerUrl
        });
        google.maps.event.addListener(marker, 'click', function() {
            window.location.href = marker.url;
        });
    }
});
4

1 回答 1

0

像这样创建标记函数(未测试):

function createMarker(point, url) {
    var marker = new google.maps.Marker({
        position: point,
        map: googleMap,
        icon: 'map-pin.png',
        url: markerUrl
    });
    google.maps.event.addListener(marker, 'click', function() {
        window.location.href = marker.url;
    });
}

然后像这样调用它:

downLoad("phpsqlajax.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
        var city = markers[i].getAttribute("city");
        var state = markers[i].getAttribute("state");
        var country = markers[i].getAttribute("country");
        var markerUrl = markers[i].getAttribute("url");
        var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
        createMarker(point, markerUrl);
    }
});
于 2013-05-20T21:09:55.397 回答