2015-07-13 66 views
0

我在我的項目中使用osmbonuspack。傳入GeoJSON的有田「風格」:如何在osmbonuspack中使用FolderOverlay從JSON中獲取樣式?

{"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}}} 

但是當我把它在地圖上的所有數據均是黑色的(無樣式或默認樣式)

 KmlDocument kmlDocument = new KmlDocument(); 

     kmlDocument.parseGeoJSON(str); 

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

     mMapView.getOverlays().add(kmlOverlay); 

     mMapView.invalidate(); 

回答

1

我找到了解決方案。現在只適用於polygones,我沒有其他文件中的對象。

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){} 

    } 
} 
相關問題