Search code examples
androidosmdroid

How say FolderOverlay in osmbonuspack to take style from JSON?


I use osmbonuspack in my project. Incoming GeoJson has fields "style":

{"type":"FeatureCollection",
"features":
    [{"type":"Feature",
      "geometry":{
          "coordinates":[[.......]],
          "type":"Polygon"},
      "properties":{"id":1433861787198,
      "monitoring_in":false,
      "emergency_message_in":false,
      "monitoring_out":false,
      "emergency_message_out":false,
      "send_notification":false,
      "style":{"stroke":true,
           "color":"#0033ff",
           "weight":5.0,
           "opacity":0.5,
           "fill":true,
           "fillOpacity":0.2,
           "clickable":true}}}

but when I put it on map all figures are black (without style or default style)

        KmlDocument kmlDocument = new KmlDocument();

        kmlDocument.parseGeoJSON(str);

        FolderOverlay kmlOverlay = (FolderOverlay) kmlDocument.mKmlRoot.buildOverlay(mMapView, null, null, kmlDocument);

        mMapView.getOverlays().add(kmlOverlay);

        mMapView.invalidate();

Solution

  • I found solution. Now only for polygones, I haven't another objects in file.

    private void showGeoJsonObjects(String str){
    
        KmlDocument kmlDocument = new KmlDocument();
    
        kmlDocument.parseGeoJSON(str);
    
        //Add styler
        KmlFeature.Styler styler = new MyKmlStyler();
    
        FolderOverlay kmlOverlay = (FolderOverlay) kmlDocument.mKmlRoot.buildOverlay(mMapView, null, styler, kmlDocument);
    
        mMapView.getOverlays().add(kmlOverlay);
    
        mMapView.invalidate();
    }
    
    
    class MyKmlStyler implements KmlFeature.Styler {
        @Override
        public void onFeature(Overlay overlay, KmlFeature kmlFeature) {
    
        }
    
        @Override
        public void onPoint(Marker marker, KmlPlacemark kmlPlacemark, KmlPoint kmlPoint) {
    
        }
    
        @Override
        public void onLineString(Polyline polyline, KmlPlacemark kmlPlacemark, KmlLineString kmlLineString) {
    
        }
    
        @Override
        public void onPolygon(Polygon polygon, KmlPlacemark kmlPlacemark, KmlPolygon kmlPolygon) {
            try {
                String styleString = kmlPlacemark.getExtendedData("style");
                JSONObject o = new JSONObject(styleString);
                if(o.getBoolean("stroke")) {
                    String colorHash = "#"+Integer.toHexString((int)(o.getDouble("opacity")*255))+o.getString("color").replace("#","");
                    polygon.setStrokeColor(Color.parseColor(colorHash));
                    polygon.setStrokeWidth((float) o.getDouble("weight"));
                }
                if(o.getBoolean("fill")){
                    String colorHash = "#"+Integer.toHexString((int)(o.getDouble("fillOpacity")*255))+o.getString("color").replace("#","");
                    polygon.setFillColor(Color.parseColor(colorHash));
                }
            }catch (Exception e){}
    
        }
    }