2013-03-29 169 views
0

我以XML格式從Google地圖獲取數據。所有功能都在工作,但我沒有問題。它只顯示我最後的數據。爲什麼是這樣,我該如何解決它?從xml谷歌地圖獲取數據

這是我的代碼(jsFiddle

function getline() { 


downloadUrl("line.php", function(doc) { 



     var g = google.maps; 

     var xmlDoc = xmlParse(doc); 

     bounds = new google.maps.LatLngBounds(); 

     // ========= Now process the polylines =========== 
     var lines = xmlDoc.documentElement.getElementsByTagName("line"); 

     // read each line 
     for (var a = 0; a < lines.length; a++) { 
     // get any line attributes 
     var colour = lines[a].getAttribute("colour"); 
     var width = parseFloat(lines[a].getAttribute("width")); 
     var diameter = lines[a].getAttribute("diameter"); 
     var project = lines[a].getAttribute("projectid"); 
     var contract = lines[a].getAttribute("contract"); 
     var comp = lines[a].getAttribute("complated"); 
     var id = lines[a].getAttribute("id_line"); 
     // read each point on that line 
     var points = lines[a].getElementsByTagName("point"); 
     var pts = []; 
     var length = 0; 
     var point = null; 

     for (var i = 0; i < points.length; i++) { 
      pts[i] = new g.LatLng(parseFloat(points[i].getAttribute("lng")), 
           parseFloat(points[i].getAttribute("lat"))); 
      if (i > 0) { 
      length += pts[i-1].distanceFrom(pts[i]); 
      if (isNaN(length)) { alert("["+i+"] length="+length+" segment="+pts[i-1].distanceFrom(pts[i])) }; 
      } 
      bounds.extend(pts[i]); 
      point = pts[parseInt(i/2)]; 

       var info = "<b> Contract: </b>" + contract + " <br/> <b>Project: </b>" + project +"<br/> <b>Diameter: </b>" + diameter + " <br/> <b>Complated: </b>" + comp + " <hr><br/><a class=\"inline-link-1\" href=\"#\">Change Data</a> <a class=\"inline-link-1\" href=\"#\">Edit</a> >" + id +"" ; 

     } 
     // length *= 0.000621371192; // miles/meter 

if (comp < 1) { 
colorr = '#FA0505' } 

if (comp > 0 && comp < 25) { 
colorr = '#FFA640' } 

if (comp > 24 && comp < 50) { 
colorr = '#FFFD91' } 

if (comp > 49 && comp < 75) { 
colorr = '#E8E400' } 

if (comp > 74 && comp < 100) { 
colorr = '#BFFFAD' } 

if (comp == 100) { 
colorr = '#0F8500' } 


     var polyline = new g.Polyline({ 
          map:map, 
          path:pts, 
          strokeColor:colorr, 
          strokeWeight:width, 
          clickable: true 
          }); 
     //createClickablePolyline(polyloine, html, label, point, length); 
    // map.addOverlay(polyline); 


    google.maps.event.addListener(polyline,'mouseover', function() { 


     this.setOptions({strokeColor: '#690469' }); 
      this.setOptions({strokeOpacity: 1.0 }); 
      this.setOptions({strokeWeight: 4 }); 
    }); 

    google.maps.event.addListener(polyline,'mouseout', function() { 


     this.setOptions({strokeColor: colorr }); 
      this.setOptions({strokeOpacity: 1.0 }); 
      this.setOptions({strokeWeight: 4 }); 
    }); 


    var mpenc = new google.maps.InfoWindow(); 
    google.maps.event.addListener(polyline,'click', function(event) { 
    mpenc.setContent(info, this); 
    mpenc.setPosition(event.latLng, this); 
    mpenc.open(map); 
    }); 

     } 
     map.fitBounds(bounds); 




    }); 



} 
+0

你的意思是由循環生成的所有元素都具有points.length元素的值?你可能沒有正確地結束循環變量,看看這個[so it it](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – kieran

+0

@布里奧請!確保你應該總是在這裏發佈代碼,不要只放棄鏈接 – swiftBoy

+0

謝謝你的回答..你能解釋我的錯誤在哪裏嗎?我無法找到它..我可以顯示你的我的XML,如果需要的話。 – brio

回答

0

看起來你複製了一些代碼,並改變它。爲什麼這個評論呢?

//createClickablePolyline(polyline, html, label, point, length); 

這個函數就是對「數據」,折線和信息窗口關閉得到,所以他們彼此(至少在我這個代碼的版本)相關。

working v3 version of my code

+0

是的。它的代碼。我編輯它..但現在我不能糾正它顯示數據..只顯示我最後的數據 – brio

+0

將您的版本與工作版本比較。添加了createClickablePolyline功能。 – geocodezip

+0

當添加此功能腳本停止工作時;( – brio