多边形数组在Google地图API中不起作用

时间:2014-08-31 17:36:58

标签: javascript google-maps-api-3

我有一个代码,可以在google map api中从数组中绘制多边形,但它不起作用

<!DOCTYPE html>
 <html>
  <head>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false" type="text/javascript"></script>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script>
    function drawPoly(multipolygonWKT) {
        var polylines = [];
        var toReturn = [];

        var formattedValues = multipolygonWKT.replace("))", "");
        formattedValues = formattedValues.replace("((", "");


        var linesCoords = formattedValues.split("),(");



        for (i = 0; i < linesCoords.length; i++) {
            polylines[i] = [];
            var singleLine = linesCoords[i].split(",");

            for (j = 0; j < singleLine.length; j++) {
                var coordinates = singleLine[j].split(" ");
                var latlng = new google.maps.LatLng(parseFloat(coordinates[1]), parseFloat(coordinates[0]));

                polylines[i].push(latlng);

            }
        }

        //by now you should have the polylines array filled with arrays that hold the coordinates of the polylines of the multipolyline
        //lets loop thru this array

        for (k = 0; k < polylines.length; k++) {
            var line = polylines[k];
          if (k > -1) {
                toReturn.push(
            new google.maps.Polygon({
                paths: line,
                strokeColor: 'red',
                strokeOpacity: 1,
                strokeWeight: 2,
                zIndex: 1
            })
        );
            }
        }
        return toReturn;
    }


    $(document).ready(function () {
        var map = new google.maps.Map(document.getElementById("map"), {
            center: new google.maps.LatLng(24.886436490787712, 70.2685546875),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
        debugger;
        var mps = ["MULTIPOLYGON(((25 80.1,18.4 66.1,32.3 64.7,25.7 80.1)),(25.1 80.5,18.8 66.1,32.9 64.2,25.1 80.5))"];
        for (i in mps) {
            var lines = drawPoly(mps[i].replace("MULTIPOLYGON", ""));
            for (k = 0; k < lines.length; k++) {
                lines[k].setMap(map);
                google.maps.event.addListener(lines[k], 'mouseover', function () {
                    this.setOptions({ fillColor: "red" });
                });
                google.maps.event.addListener(lines[k], 'mouseout', function () {
                    this.setOptions({ fillColor: "white" });

                });
            }
            lines.length = 0;
        }
    });
</script>
        </head>
        <body>
            <div style="height:1000px;width:1200px;" id="map"></div>
        </body>
        </html>

这段代码在这里工作this 但是我的拉特隆没有工作

1 个答案:

答案 0 :(得分:1)

您在WKT表示中反转了纬度和经度。那应该是&#34;经度纬度&#34;:

改变这个:

var mps = ["MULTIPOLYGON(((25 80.1,18.4 66.1,32.3 64.7,25.7 80.1)),(25.1 80.5,18.8 66.1,32.9 64.2,25.1 80.5))"];

要:

var mps = ["MULTIPOLYGON(((80.1 25,66.1 18.4,64.7 32.3,80.1 25.7),(80.5 25.1,66.1 18.8,64.2 32.9,80.5 25.1)))"];

Working fiddle