【问题标题】:Multiple Polygons MySQL多个多边形 MySQL
【发布时间】:2012-05-29 23:36:22
【问题描述】:

我正在尝试从数据库中绘制几个单独的多边形(建筑物)。 我从数据库中获取的数据如下所示:

<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);

问题是不是所有的点都画出来了?我不明白问题出在哪里?

【问题讨论】:

    标签: google-maps-api-3


    【解决方案1】:

    paths 数组似乎有问题。从您包含的代码中不清楚 paths 是 JavaScript Array 还是类似数组的对象,但由于您在 for 循环中使用 paths 然后尝试清除 paths在循环内,我建议:

    • 根据需要在循环内创建一个新的pathsArray
    • 添加代码来管理数组循环的状态
    • 更改用于检查建筑属性和测试相等性的代码
    • 由于您将路径数组传递给 Polygon 构造函数,因此不要清除该数组

    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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 2013-02-24
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 2011-07-03
      • 2019-07-23
      • 2021-06-18
      相关资源
      最近更新 更多