简体   繁体   English

在地图上绘制多边形

[英]Drawing Polygon on Map

I'am trying to draw a polygon with about 260 coordinates on my Map. 我正在尝试在地图上绘制约260个坐标的多边形。 But I cant figure out why the polygon is not drawn ? 但是我不知道为什么不绘制多边形? There are no null references in the PolygonOptions nor are their wrong data. PolygonOptions中没有空引用,也没有错误的数据。

 protected void drawOverlayPolygon(JSONArray coordinates) {
    PolygonOptions rectOptions = new PolygonOptions();
    if (coordinates != null) {
        int j = 0;
        while(!coordinates.isNull(j))
            j++;
        ArrayList<LatLng> points = new ArrayList<LatLng>(j);
        for (int i = 0; i < j; i++) {
            try {
                JSONArray latlng = coordinates.getJSONArray(i);
                // latlng = coordinates.getJSONArray(i);
                if (latlng != null) {
                    LatLng point = new LatLng(Float.parseFloat(latlng
                            .getString(0)), Float.parseFloat(latlng
                            .getString(1)));

                //  rectOptions.add(point);
                    points.add(point);
                    Log.i("LatLng Polygon",
                            Float.parseFloat(latlng.getString(0)) + " / "
                                    + Float.parseFloat(latlng.getString(1)));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            // }

        }

        rectOptions.addAll(points);
        rectOptions.strokeColor(Color.RED);
        rectOptions.fillColor(Color.BLUE);

        Polygon polygon = mapFragment.getMap().addPolygon(rectOptions);
        polygon.setVisible(true);


        Polygon polygon2 = mapFragment.getMap().addPolygon(
                new PolygonOptions()
                        .add(new LatLng(0, 0), new LatLng(0, 5),
                                new LatLng(3, 5)).strokeColor(Color.RED)
                        .fillColor(Color.BLUE));
    }
}

For testing I added a simple Polygon (polygon2) and it gets drawn. 为了测试,我添加了一个简单的Polygon(polygon2)并绘制了它。 I am parsing the coordinates from a JSON File. 我正在从JSON文件解析坐标。 I logged the corrds and they seem to be alright. 我记录了皮条,它们似乎还好。 One thing I noticed is that die JSONArray coordinates contains null references. 我注意到的一件事是该JSONArray坐标包含空引用。

Can anybody give me a clue ? 有人可以给我一个提示吗?

try adding: 尝试添加:

rectOptions.visible(true);

before adding the Polygon to the map. 在将多边形添加到地图之前。

Thx Steffi I tried to add that but did not work. Thx Steffi我试图补充一点,但是没有用。 Everyone who runs in this problem check if the order of your Lat Lng Coordinates is correct. 遇到此问题的每个人都要检查纬度坐标的顺序是否正确。 Otherwise your polygone is drawn somewhere in the nowhere :) 否则,您的多边形将被绘制在任何地方:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM