如何从WKT添加MultiPolygon do Android Google Maps v2

时间:2015-03-19 16:18:37

标签: google-maps google-maps-android-api-2 polygon wkt

我在spatialite数据库中有几何。在jts拓扑套件的帮助下,我设法在谷歌地图v2上绘制一些简单的多边形,在坐标数组中转换WKT。但我无法弄清楚如何正确绘制多边形。各个较小的形状是连接的,它们不应该是。

正确的多边形: Image

故障MultiPolygon: enter image description here

它应该如何: enter image description here

1 个答案:

答案 0 :(得分:2)

所以经过几天的尝试,我得到了这样的工作。 我希望它有所帮助。

private void test(){    

        GeometryFactory geometryFactory = new GeometryFactory();
        WKTReader reader=new WKTReader(geometryFactory);
        PolygonOptions polyOptions = null;      
        ArrayList<String> featureList = new ArrayList<String>();
        MultiPolygon multipolygon = null;
        Polygon polygon = null;
        Coordinate[] outerCoordinates = null;
        Coordinate[] innerCoordinates = null;
        ArrayList<LatLng> outer = null;     
        ArrayList<LatLng> inner = null;  

        try {           

            //Gets a list of features in WKT format
            featureList = HtmlActivity.mDbHelper.getFeatureList();  

            //Iterates the feature list
            for (String feature : featureList) {    

                multipolygon = (MultiPolygon) reader.read(feature);

                //Gets each polygon of a multipolygon
                for(int i = 0; i < multipolygon.getNumGeometries(); i++)
                {
                    outer = new ArrayList<LatLng>();                    
                    polyOptions = new PolygonOptions();             
                    polygon = (Polygon) multipolygon.getGeometryN(i);

                    //Gets each polygon outer coordinates
                    outerCoordinates = polygon.getExteriorRing().getCoordinates();                          
                    for (Coordinate outerCoordinate : outerCoordinates){        
                        outer.add(new LatLng(outerCoordinate.y, outerCoordinate.x));  
                    }   
                    polyOptions.addAll(outer);              

                    //Getting each polygon interior coordinates (hole)  if they exist
                    if(polygon.getNumInteriorRing() > 0){
                        for(int j = 0; j < polygon.getNumInteriorRing(); j++){
                            inner = new ArrayList<LatLng>();
                            innerCoordinates = polygon.getInteriorRingN(j).getCoordinates();                            
                            for (Coordinate innerCoordinate : innerCoordinates){        
                                inner.add(new LatLng(innerCoordinate.y, innerCoordinate.x));  
                            }   
                            polyOptions.addHole(inner); 
                        }   

                    }                       
                    polyOptions.strokeColor(Color.rgb(30, 30, 30));
                    polyOptions.strokeWidth(2);       
                    polyOptions.fillColor(Color.argb(255, 255, 0, 0));  
                    googleMap.addPolygon(polyOptions);
                }                   
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }     

    }