多边形数组在Google地图API中不起作用 [英] Polygon array does not work in Google map API

查看:62
本文介绍了多边形数组在Google地图API中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code that draw polygon from array in google map api but it does not work

<!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 code work here by this lat lon this but not work whis my lat lon

推荐答案

您在WKT表示中反转了经度和纬度。这应该是经度纬度:

You have the latitude and longitude reversed in the WKT representation. That should be "Longitude Latitude":

更改此:

Change this:

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

工作小提琴

这篇关于多边形数组在Google地图API中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆