English 中文(简体)
多个多边形 MySQL
原标题:Multiple Polygons MySQL

I am trying to draw several seperated Polygons (buildings) out of a database. The data I get out of my database looks like this:

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

问题在于,不是所有要点都被划清了吗?

问题回答

看来 >paths 数组可能存在问题。 您没有从代码中清楚地看到 >paths 是 JavaScript Array 还是一个类似阵列的天体, 但是因为您在 paths 环中与 合作, 然后试图清除循环中的 paths , 我建议 :

  • Create a new paths Array within the loop as it is needed
  • Add code to manage the state of the array looping
  • Change the code you use to check the building attributes and testing equality
  • Since you are passing the paths array to the Polygon constructor, don t clear the array

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. later = 0 , 如 https:// stackoverflow.com/q/ 4020548/13132 > 。 > Is JavaScript s. clearty () 不是函数?





相关问题
Why do I get "map.set_center is not a function"?

This code have been working until I updated last night. The code works perfectly on my localhost, but I get an error on my test server. The error message (from Firebug) is "map.set_center is not a ...

The state of GMaps v3

I m about to start a Google map based project and am wondering if the release version of GMaps v3 has most of the features that are available in v2, or if it would be best to stick with v2 for now. Is ...

Bouncy marker in Google Maps v3

In Google Maps API v2 we can set to marker an option bouncy:true. It adds to marker eye-candy ability - after dragging this marker, it is bouncing. Is it possible to do in API v3 ?

GUnload() a single Google Map

I ve got some Javascript/HTML code which displays a variable number of maps dependent on what the user selects. I ve worked how to dynamically create multiple maps on a page - that s well documented. ...

How to change Gmap markers color?

I ve a custom google map with different points: Markers[0] = new Array(new GMarker(new GLatLng(45.0, 9.0)), "Location1", "<strong>Address Line</strong><br/>Some information"); ...

热门标签