2012-05-23 81 views
0

我想从数据库中画出几个分离的多边形(建筑物)。 我得到了我的数据库中的数据是这样的:多个多边形MySQL

<buildings> 
    <building build_id="94" build_gebaeude="A" 
     build_geoX="49.26173942769648" build_geoY="7.350542675857582" 
    /> 
    <building build_id="95" build_gebaeude="A" 
     build_geoX="49.26173942769648" build_geoY="7.3524094933319475" 
    /> 
    <building build_id="96" build_gebaeude="A" 
     build_geoX="49.26019903253632" build_geoY="7.35234512031559" 
    /> 
    <building build_id="97" build_gebaeude="A" 
     build_geoX="49.26032506667364" build_geoY="7.350692879562416" 
    /> 
    <building build_id="98" build_gebaeude="B" 
     build_geoX="49.26155738350112" build_geoY="7.362129818801918" 
    /> 
    <building build_id="99" build_gebaeude="B" 
     build_geoX="49.26157138692462" build_geoY="7.364275586013832" 
    /> 
    <building build_id="100" build_gebaeude="B" 
     build_geoX="49.260255047748224" build_geoY="7.364361416702309" 
    /> 
    <building build_id="101" build_gebaeude="B" 
     build_geoX="49.260311062896506" build_geoY="7.362065445785561" 
    /> 
</buildings> 

下面的代码显示我在做什么得出建筑物:

for (var i = 0; i < building.length-1; i++) { 
    var point = new google.maps.LatLng(
     parseFloat(building[i].getAttribute("build_geoX")), 
     parseFloat(building[i].getAttribute("build_geoY")) 
    ); 

    latlngbounds.extend(point); 

    if(building[i].getAttribute("build_gebaeude") == 
     building[i+1].getAttribute("build_gebaeude")) 
    { 
     path.push(point); 
    } 
    else { 
     path.push(point); 
     poly = new google.maps.Polygon({ 
      paths: path, 
      strokeWeight: 5, 
      strokeOpacity: 1, 
      fillOpacity: 0.30, 
      strokeColor:'#FFFFFF', 
      fillColor: '#a3141d' 
     }); 
     polygons.push(poly); 
     path.clear(); 
    } 
} 
polygons[0].setMap(map); 
polygons[1].setMap(map); 

的问题是,并非所有的点绘制?我不明白问题在哪里?

回答

0

看起来像paths阵列可能有问题。这不是从你已包括paths是否是一个JavaScript Array或类似阵列的对象代码清晰,但因为你与pathsfor循环内的工作,然后试图清除paths内环路,我建议:

  • 创建循环内的新pathsArray,因为它需要
  • 添加代码管理阵列循环
  • 更改您用来检查建筑物的属性代码和测试平等
  • 的状态由于要传递的路径阵列的多边形构造,不清除阵列

var paths = null; 
var aNewPathIsNeeded = true; 
for (var i = 0; i < building.length-1; i++) { 
    //Create your point 
    //Extend your bounds 
    //Since you always add the point to the paths array, move it out of the if 
    //check and perform some array state management: 

    if (aNewPathIsNeeded) { 
     paths = new Array(point); 
     aNewPathIsNeeded = false; 
    } 
    else { paths.push(point); } 

    //Change the if condition to deal with just the specific case of interest: 

    var currentBldgAttr = building[i].getAttribute("build_gebaeude"); 
    var nextBldgAttr = building[i+1].getAttribute("build_gebaeude"); 
    if (!(currentBldgAttr === nextBldgAttr)) { //Change to use: '===' 
     //Create your polygon 
     //Add the polygon to the polygons array 
     //New code to manage paths state: 
     aNewPathIsNeeded = true; 
    } 
} 

我不知道你到底是什么问题,但我相信这个代码将帮助您移动前锋。作为附注,clear()不是JavaScript Array方法;当您想清空Array时,请使用:array.length = 0,如:Is JavaScript's array.clear() not a function?