androidgoogle-maps-android-api-2wkt

Android. Google map. How to draw polygon from array


I m new to android app development. I have WKT (POLYGON) How to draw polygon on google map from wkt?

I try

    String str;

        ArrayList<String> coordinates = new ArrayList<String>();

        str = tvwkt.getText().toString();

        str = str.replaceAll("\\(", "");
        str = str.replaceAll("\\)", "");
        str = str.replaceAll("POLYGON", "");
        str = str.replaceAll("POINT", "");
        str = str.replaceAll(", ", ",");
        str = str.replaceAll(" ", ",");
        str = str.replaceAll(",,", ",");


        String[] commatokens = str.split(",");
            for (String commatoken : commatokens) {
                coordinates.add(commatoken);
        }

        for (int i = 0; i < coordinates.size(); i++) {

            String[] tokens = coordinates.get(i).split("\\s");
            for (String token : tokens) {

                listPoints.add(token);
            }

        }

        PolygonOptions rectOptions = new PolygonOptions().addAll(listPoints).strokeColor(Color.BLUE).fillColor(Color.CYAN).strokeWidth(7);

        polygon = mMap.addPolygon(rectOptions);

But its not work. Hepl me please. thanks.


Solution

  • I can do it.

    Read LatLong from WKT and add to Array

    private LatLng[] GetPolygonPoints(String polygonWkt) {
    
        Bundle bundle = getIntent().getExtras();
        wkt = bundle.getString("wkt");
        ArrayList<LatLng> points = new ArrayList<LatLng>();
        Pattern p = Pattern.compile("(\\d*\\.\\d+)\\s(\\d*\\.\\d+)");
        Matcher m = p.matcher(wkt);
        String point;
    
        while (m.find()){
            point =  wkt.substring(m.start(), m.end());
            points.add(new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))));
        }
        return points.toArray(new LatLng[points.size()]);
    
    }
    

    then draw polygon

        public void Draw_Polygon() {
    
        LatLng[] points = GetPolygonPoints(polygonWkt);
    
            Polygon p = mMap.addPolygon(
                    new PolygonOptions()
                            .add(points)
                            .strokeWidth(7)
                            .fillColor(Color.CYAN)
                            .strokeColor(Color.BLUE)
            );
    
        //Calculate the markers to get their position
        LatLngBounds.Builder b = new LatLngBounds.Builder();
        for (LatLng point : points) {
            b.include(point);
        }
        LatLngBounds bounds = b.build();
        //Change the padding as per needed
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 20,20,5);
        mMap.animateCamera(cu);
    
    }
    

    finally

    public void onMapReady(GoogleMap googleMap) {
    
        mMap = googleMap;
    
        mMap.setMapType(MAP_TYPE_HYBRID);
    
        mMap.getUiSettings().setRotateGesturesEnabled(false);
    
        mMap.getUiSettings().setMapToolbarEnabled(false);
    
        LatLng[] points = GetPolygonPoints(polygonWkt);
    
        if (points.length >3){
    
            Draw_Polygon();
    
        }
        else {
    
            Add_Markers();
    
        }
    
    }