谷歌地图api与多标记上的链接

时间:2011-10-19 06:46:51

标签: javascript google-maps-api-3

我一直在使用这个:google maps marker as link api v3

我得到的是我地图中的所有标记都具有相同的链接 所以我改变了一些代码而没有任何帮助

我正在努力的主要事情是,在第一个for循环中,var i介于0到n之间,但在geocoder.gercode函数内,它始终是n值(最高值)而不是来自0到n,因此我的链接始终是最后一个

var AllPlaces = new Array();
var address = [<%=LocationStr%>];
var links = [<%=LinkStr%>];
var dolinks = (links.length == 0) ? false : true;
var MaxAddress = address.length;
var ThisLink;
var Markers = new Array();
var Mj = 0;
var geocoder;
function initialize() {
    geocoder = new google.maps.Geocoder();
    for (var i = 0; i < MaxAddress; i++) {
        ThisLink = "";
        Mj = i;
        if (dolinks) {
            ThisLink = links[i];
        }
        geocoder.geocode( { 'address': address[i]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                AllPlaces.push(results[0].geometry.location);
                if (MaxAddress == 1) {
                    map.setCenter(results[0].geometry.location);
                }
                else {
                    DoCenter(AllPlaces,map);
                }
                Markers[i] = new google.maps.Marker({
                    map: map,
                    url: ThisLink, 
                    position: results[0].geometry.location
                });
                alert(Mj);
                if (dolinks) {
                    google.maps.event.addListener(Markers[i], 'click', function() {
                        window.location.href = Markers[i].url;
                    });
                }
            }
        });
    }
    var latlng = new google.maps.LatLng();
    var myOptions = {
        zoom: <%=ZoomStr%>,
        mapTypeControl: false,
        streetViewControl: false,
        panControl: false,
        zoomControl: true,
        scaleControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }; 
    var map = new google.maps.Map(document.getElementById("<%=DivID%>"), myOptions);
    }

    function DoCenter(LL,obj) {
        if (LL.length < MaxAddress) return;
        var bounds = new google.maps.LatLngBounds ();
        for (var i = 0, LtLgLen = LL.length; i < LtLgLen; i++) {
            bounds.extend (LL[i]);
        }
        obj.fitBounds (bounds);
}

2 个答案:

答案 0 :(得分:0)

geocoder.geocode异步运行。你的for循环可能在第一个地理编码回调执行之前完成。创建一个新功能以关联每个链接。 注意:每个函数都在新的上下文中运行。 修改后的代码:

var AllPlaces = new Array();
var address = [<%=LocationStr%>];
var links = [<%=LinkStr%>];
var dolinks = (links.length == 0) ? false : true;
var MaxAddress = address.length;
var ThisLink;
var Markers = new Array();
var Mj = 0;
var geocoder;
function reversGeoCord(address, ThisLink ){

   geocoder = new google.maps.Geocoder();
   geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                AllPlaces.push(results[0].geometry.location);
                if (MaxAddress == 1) {
                    map.setCenter(results[0].geometry.location);
                }
                else {
                    DoCenter(AllPlaces,map);
                }
                Markers[i] = new google.maps.Marker({
                    map: map,
                    url: ThisLink, 
                    position: results[0].geometry.location
                });
                alert(Mj);
                if (dolinks) {
                    google.maps.event.addListener(Markers[i], 'click', function() {
                        window.location.href = Markers[i].url;
                    });
                }
            }
        });
}
function initialize() {

    for (var i = 0; i < MaxAddress; i++) {
        ThisLink = "";
        Mj = i;
        if (dolinks) {
            ThisLink = links[i];
        }
        reversGeoCord(address[i], ThisLink  );
    }
    var latlng = new google.maps.LatLng();
    var myOptions = {
        zoom: <%=ZoomStr%>,
        mapTypeControl: false,
        streetViewControl: false,
        panControl: false,
        zoomControl: true,
        scaleControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }; 
    var map = new google.maps.Map(document.getElementById("<%=DivID%>"), myOptions);
    }

    function DoCenter(LL,obj) {
        if (LL.length < MaxAddress) return;
        var bounds = new google.maps.LatLngBounds ();
        for (var i = 0, LtLgLen = LL.length; i < LtLgLen; i++) {
            bounds.extend (LL[i]);
        }
        obj.fitBounds (bounds);
}

答案 1 :(得分:0)

只是为了澄清问题所在。您的 i 正在增加,但正如您所知, i 是实时的。即当你循环它的价值也在变化。例如:

var arr = [];
for(var i=0; i<5; i++) {
   var func = function() {
      console.log(i);
   }
   arr.push(func);
}

for(var j=0; j<5; j++) {
    arr[j]();
}

上面代码的结果是:

5
5
5
5
5

解决方案是创建一个匿名函数并立即调用它:

var arr = [];
for(var i=0; i<5; i++) {
    var func = function(index) {
        return function() {
            console.log(index);
        }
    }(i);
    arr.push(func);
}
for(var j=0; j<5; j++) {
    arr[j]();
}