2011-05-03 52 views
1

我有一個forloop,其中有一個函數調用。在該函數中,我將值推送到一個名爲markers的數組。使用函數外部的javascript數組的值

有沒有辦法訪問forloop之外的標記數組的值?

下面的代碼:

<script type="text/javascript"> 
    // arrays to hold copies of the markers and html used by the side_bar 
    // because the function closure trick doesnt work there 
    var map = null; 
    geocoder = new google.maps.Geocoder(); 
    var side_bar_html = ""; 
    var icon = ''; 
    var markers = []; 

    function codeAddress(this_address,index,callback) { 
     geocoder.geocode({ 'address': this_address}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       callback.call(window,index,results[0].geometry.location) 
      } else { 
       alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    } 


    function initialize() { 
     // create the map 
     var myOptions = { 
      zoom: 3, 
      center: new google.maps.LatLng(46.90, -121.00), 
      mapTypeControl: true, 
      mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
      navigationControl: true, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 

     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

     google.maps.event.addListener(map, 'click', function() { 
      infowindow.close(); 
     }); 


     for (var i = 0; i < businesses.length; i++) { 
      codeAddress(businesses[i].address,i,function(i,point){ 
       var description = businesses[i].description; 

       if(businesses[i].business_type == "Wine"){ 
        //http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|00CC99|000000 
        icon = 'http://google-maps-icons.googlecode.com/files/wineyard.png'; 
       }else if(businesses[i].business_type == "Golf"){ 
        icon = 'http://google-maps-icons.googlecode.com/files/golf.png'; 
       }else{ 
        icon = 'http://google-maps-icons.googlecode.com/files/festival.png'; 
       } 

       var marker = createMarker(point,businesses[i].name,description,icon); 

       // put the assembled side_bar_html contents into the side_bar div 
       document.getElementById("side_bar").innerHTML = side_bar_html; 
      });//End codeAddress-function 
     }//End for-loop 

     console.log(markers); 
     var markerCluster = new MarkerClusterer(map, markers);    

    } 

    // A function to create the marker and set up the event window function 
    function createMarker(latlng, name, html,icon) { 
     var contentString = html; 
     var marker = new google.maps.Marker({ 
      position: latlng, 
      map: map, 
      icon: icon, 
      zIndex: Math.round(latlng.lat()*-100000)<<5 
      }); 

     google.maps.event.addListener(marker, 'click', function() { 
      infowindow.setContent(contentString); 
      infowindow.open(map,marker); 
     }); 

     // save the info we need to use later for the side_bar 
     markers.push(marker); 

     // add a line to the side_bar html 
     side_bar_html += '<a href="javascript:myclick(' + (markers.length-1) + ')">' + name + '<\/a><br />'+html+'<br />'; 

    } 

    var infowindow = new google.maps.InfoWindow({ 
     size: new google.maps.Size(150,50) 
    }); 

    // This function picks up the click and opens the corresponding info window 
    function myclick(i) { 
     google.maps.event.trigger(markers[i], "click"); 
    } 

</script> 

正如你所看到的,最後一行寫着 「變種markerCluster =新MarkerClusterer(地圖上,標記);」這是我想要從中獲取信息的地方。

謝謝!

+1

看起來你缺少幾行代碼來顯示函數和數組是如何聲明的。 – epascarello 2011-05-03 22:13:41

+0

我更新了代碼以顯示更多內容。 – bigmike7801 2011-05-03 22:40:11

回答

1

的問題是你不佔呼叫codeAddress的異步特性。您正在循環調用該函數,這會觸發對Google Maps API的一系列調用。

您正在運行這一行:

var markerCluster = new MarkerClusterer(map, markers); 

...回調已被觸發之前也。

修復,維護一個計數器。每次觸發回調將該計數器增加1.一旦它等於businesses.length,就知道所有地址都已進行地理編碼,並且所有標記都已添加到陣列中。現在您可以創建MarkerCluster

+0

你的答案似乎迄今爲止最有意義,但是,我會把它放在哪裏計數器和我在哪裏創建MarkerCluster? – bigmike7801 2011-05-03 23:21:43

+0

我懂了!非常感謝! – bigmike7801 2011-05-03 23:29:36

0

是的,在for循環之前聲明它。

var markers 
for(.... 
+0

我試過這個,但它就好像forloop中的函數正在清除標記數組或其他東西。 – bigmike7801 2011-05-03 22:34:47

0

攜帶的標記定義的for循環外...

var markers = new Array(); 
for (var i = 0; i < businesses.length; i++) { 
    markers[i] = ... 
+0

我嘗試這個,但它是因爲該函數後清除數組 – bigmike7801 2011-05-03 22:34:08

+0

您是否將var標記的值分配給數組? – judda 2011-05-03 22:36:16

+0

標記的值正被分配給標記數組。 – bigmike7801 2011-05-03 22:41:33