簡體   English   中英

Google Map API V2的Javascript全局變量

[英]global var on Javascript for Google Map API V2

我想創建一個JavaScript全局變量,其中包含我需要的一些值。 我已經從XML文件中讀取了一些值,並將其用於顯示Google Map的地圖。 我會將這些信息存儲在一些變量中,並在用戶要求時顯示它們。

我有以下代碼:

var tot_pts = new Array(); //global variable

if (GBrowserIsCompatible()) { 

  // Display the map, with some controls and set the initial location 
  ...

  // A function to create the marker and set up the event window
  function createMarker(point,train) {
    var marker = new GMarker(point);
    return marker;
  }

  request.open("GET", "example.xml", true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = GXml.parse(request.responseText);
      //read value on XML
      tot_pts.push(new_path);

      //show all value on map
      for (var i = 0; i < tot_pts.length; i++) {
        point= [];
        point[0]=tot_pts[i].start;
        point[1]=tot_pts[i].end;
        map.addOverlay(new GPolyline(point,tot_pts[i].colour,3));
        marker = createMarker(tot_pts[i].start,tot_pts[i].train);
        map.addOverlay(marker);
        marker = createMarker(tot_pts[i].end,tot_pts[i].train);
        map.addOverlay(marker);
      }
    }//end of if request=4
  }// end of on ready...

  request.send(null);

}// end of compatible browser   

如果我將最后一個“ for循環”移到此代碼中的另一個位置,則數組tot_pts為空。 為什么會這樣? 我可以通過某種方式改進代碼嗎?

request.readyState == 4表示XMLHttpRequest的響應已准備就緒。 因此,當您獲得響應時,可以在步驟-> tot_pts.push(new_path);中將數組tot_pts推入一些值(new_path); 在此之前,該數組將顯示為空,因為未明確為其設置值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM