2011-11-22 20 views
0

以下代码在用户点击特定Google地球标记时未能重定向用户。其他一切正常。任何指针?Google Earth中每个标记都带有链接的标记阵列

的JavaScript:

$(function() { 
    var latArray = ($("#google_map_section").attr("data-lat")).split(", "); 
    var longArray = ($("#google_map_section").attr("data-long")).split(", "); 
    var nameArray = ($("#google_map_section").attr("data-name")).split(", "); 
    var urlArray = ($("#google_map_section").attr("data-url")).split(", "); 
    var mylatlngs = []; 
    var markers = []; 
    var counter = 0; 
    var bounds = new google.maps.LatLngBounds(); 

    while(counter < latArray.length){ 
     mylatlngs.push(new google.maps.LatLng(
      parseFloat(latArray[counter]), 
      parseFloat(longArray[counter])) 
     ); 
     counter++ 
    }; 
    var myOptions = { 
     zoom: 12, 
     center: mylatlngs[mylatlngs.length-1], 
     mapTypeId: google.maps.MapTypeId.SATELLITE 
    }; 
    var map = new google.maps.Map(document.getElementById("google_map_section"), 
     myOptions 
    ); 
    counter = 0; 
    while (counter < mylatlngs.length){ 
     markers.push(new google.maps.Marker({ 
      position: mylatlngs[counter], 
      map: map, 
      title: nameArray[counter], 
      url: urlArray[counter] 
     })); 
     bounds.extend(mylatlngs[counter]); 
     counter++ 
    }; 
    counter = 0; 
    while (counter < markers.length){ 
     google.maps.event.addListener(markers[counter], 'click', function() { 
      window.location.href = this[counter].url; 
     }); 
     counter++ 
    }; 
    map.fitBounds(bounds); 
    }); 

的HTML页面的源代码:

<div id="google_map_section" data-url="http://localhost:3000/impacts/12, http://localhost:3000/events/9, http://localhost:3000/impacts/10, http://localhost:3000/events/7" data-name="Dead fish!, Oil spill, Acid River Pollution, AMD Seepage" data-lat="-34.0, -34.0333333, -33.638271, -33.724289" data-long="17.0, 18.35, 18.990891, 18.95591"></div> 

更新:

萤火虫给event.addListener功能里面以下警告: “遗漏的类型错误:无法读取未定义的属性“网址”

进一步更新:

好了,问题是分配到URL中这一行:

url: urlArray[counter] 

现在,这是有趣的,如果我直接分配数组的元素的事件监听像这样:

google.maps.event.addListener(markers[counter], 'click', function() { 
    window.location.href = urlArray[counter]; 
}); 

它仍然无法正常工作,但硬编码的URL呢!所以这个:

google.maps.event.addListener(markers[counter], 'click', function() { 
    window.location.href = 'http://www.google.com'; 
}); 

作品!我勒个去?

回答

1

啊哈!得到它了。出于某种原因,事件监听器必须分配在一个单独的函数中。总之,对于标记作品的代码如下:

while (counter < mylatlngs.length){ 
    marker = new google.maps.Marker({ 
     position: mylatlngs[counter], 
     map: map, 
     title: nameArray[counter], 
     url: urlArray[counter] 
    }); 
    addListen(marker); 
    markers.push(marker); 
    bounds.extend(mylatlngs[counter]); 
    counter++; 
}; 

map.fitBounds(bounds); 

function addListen(markerPass){ 
    google.maps.event.addListener(markerPass, 'click', function() { 
     window.location.href = markerPass.url; 
    }); 
}; 

有人仍然欢迎,为什么这会工作发表评论......