2014-03-29 49 views
0
var markers; 
function initListMarkers() { 
     var markers; 

    var mpMarkers = [ 
     ['place 1', 13.784420, 100.684456, 1], 
     ['', 13.744571, 100.436233, 7], 
     ['', 13.807593, 100.510734, 8], 
     ['', 13.783086, 100.493740, 9], 
     ['', 13.806426, 100.578541, 10], 
     ['', 13.660516, 100.605148, 11], 
     ['', 13.761079, 100.710033, 12], 
     ['', 13.691707, 100.750974, 13], 
     ['', 13.680032, 100.476874, 14], 
     ['', 13.678364, 100.747069, 15], 
     ['swb', 13.676029, 100.734709, 16], 
    ]; 

    var infoWindowContent = [ 
     ['<div class="myplace-info-content"><h3>place 1</h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3></h3><p></p></div>'], 
     ['<div class="myplace-info-content"><h3>swb</h3><p>suwarnnabhumi airport</p></div>'], 
    ]; 

    var infoWindow = new google.maps.InfoWindow(), marker, i; 

    for(i = 0; i < mpMarkers.length; i++) { 
     var position = new google.maps.LatLng(mpMarkers[i][1], mpMarkers[i][2]); 

     bounds.extend(position); 

     marker = new google.maps.Marker({ 
      position: position, 
      map: map, 
      title: mpMarkers[i][0] 
     }); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
      return function() { 
       infoWindow.setContent(infoWindowContent[i][0]); 
       infoWindow.open(map, marker); 
      } 
     })(marker, i)); 

     var mp_id = mpMarkers[i][3]; 
     markers[mp_id] = marker; 

     map.fitBounds(bounds); 
    } 

    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) { 
     this.setZoom(12); 
     google.maps.event.removeListener(boundsListener); 
    }); 
} 
的螢火

JavaScript變量甚至沒有定義定義?

TypeError: markers is undefined

markers[mp_id] = marker;

,但被定義的標記。 第一名是外部功能。 第二位id函數。

下面的功能正常工作。

function addMarkerDraggable(group_id) { 
    // get current view center location 
    var current_view = map.getCenter(); 
    var current_lat = current_view.lat(); 
    var current_lng = current_view.lng(); 

    /** 
    * set variable from ajax fixed by http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call 
    */ 
    var mp_id = get_from_ajax_as_json(); 

    mp_id = mp_id.responseJSON; 
    mp_id = mp_id.mp_id; 

    if (mp_id != 'undefined' && mp_id != '') { 
     // myplace id was set, set marker. 
     marker = new google.maps.Marker({ 
      draggable: true, 
      position: current_view, 
      icon: default_marker_icon, 
      map: map, 
      title: '', 
      animation: google.maps.Animation.DROP 
     }); 

     // set markers array for easy to remove. 
     markers[mp_id] = marker; 
    } 
}// addMarkerDraggable 

爲什麼它的工作原理不同,它應該是相同的?

如何從函數initListMarkers()設置標記數組?

回答

0

您聲明瞭markers,但您尚未爲其分配任何值。如果你想成爲一個數組,只是它的值初始化到一個新的數組,像這樣:

var markers = []; 

陣列是理想的,當你在一個特定的順序元素,用數字0 hellip索引的順序; n。但是,粗略看一下代碼,看起來更像是將它用作關聯數組或映射,其中有許多項目,沒有特定的順序,由一個字符串索引。如果是這種情況,通常更適合使用對象:

var markers = {}; 
+0

爲什麼addMarkerDraggable()函數可以設置標記變量而無需在函數中再次聲明它? 要聲明它的功能,我不能使用這種變化的外部功能或從其他功能。 – vee

+0

@vee由於'markers [mp_id] = marker'不是簡單地設置'markers'的值,而是訪問變量的值,然後嘗試設置該對象的屬性(或元素)的值(或數組)。換句話說'marker'包含對有效值的引用(不是'undefined'或'null'),然後才能使用'markers [mp_id]'。 –