2017-12-18 3 views
0

Ich möchte meine Geojson-Ebene nehmen und eine Farbe darauf anwenden. Mit dem Code unten habe ich nur eine schwarze Linie über dem Bereich. Dieser Teil ist korrekt, ich muss nur Farbe und Schattierung hinzufügen. Ich bin jedoch keine einfache Art und Weise zu sehen, dies mit einem GeoJSON zu tun oder geolayerLegen Sie die Farbe und den schattierten Bereich auf einem Geojson-Objekt/Geolayer für Google Maps SDK Android

enter image description here

private void drawNeighborhood(int neighborhoodIndex) { 
    try { 
     mMap.clear(); 
     if (mViewModel.validateNeighborboodsState()) { 
     return; 
     } 

     Features[] featuresArray = mViewModel.getFeature(); 
     final LatLngBounds.Builder builder = LatLngBounds.builder(); 
     if (featuresArray.length > neighborhoodIndex) { 
     Features currentFeature = featuresArray[neighborhoodIndex]; 
     GeoJsonLayer geoJsonLayer = getGeoJsonLayer(currentFeature); 
     createViewArea(builder, currentFeature); 
     final CameraUpdate cameraUpdate = 
      CameraUpdateFactory.newLatLngBounds(builder.build(), 200); 
     mMap.animateCamera(cameraUpdate); 
     geoJsonLayer.addLayerToMap(); 
     } 

    } catch (JSONException e) { 
     Timber.e("GeoJSON file could not be converted to a JSONObject"); 
    } 

    } 




private GeoJsonLayer getGeoJsonLayer(Features features) throws JSONException { 
    Gson gson = new Gson(); 
    String raw = gson.toJson(features); 
    JSONObject json = new JSONObject(raw); 
    return new GeoJsonLayer(mMap, json); 
    } 

Feature-Modell

public class Features { 
    private Properties properties; 

    private String type; 

    private Geometry geometry; 

    public Properties getProperties() { 
    return properties; 
    } 

    public void setProperties(Properties properties) { 
    this.properties = properties; 
    } 

    public String getType() { 
    return type; 
    } 

    public void setType(String type) { 
    this.type = type; 
    } 

    public Geometry getGeometry() { 
    return geometry; 
    } 

    public void setGeometry(Geometry geometry) { 
    this.geometry = geometry; 
    } 

    @Override 
    public String toString() { 
    return "ClassPojo [properties = " + properties + ", type = " + type + ", geometry = " + geometry + "]"; 
    } 
} 

Dann Objekt, was die Gegend basiert. (Beachten Sie, dass es sich immer um ein Polygon und nicht um Linien handelt). Sollte Vanille Geojson-Spezifikation sein

public class Geometry { 
    private String type; 

    private String[][][] coordinates; 

    public String getType() { 
    return type; 
    } 

    public void setType(String type) { 
    this.type = type; 
    } 

    public String[][][] getCoordinates() { 
    return coordinates; 
    } 

    public void setCoordinates(String[][][] coordinates) { 
    this.coordinates = coordinates; 
    } 

    @Override 
    public String toString() { 
    return "ClassPojo [type = " + type + ", coordinates = " + coordinates + "]"; 
    } 
} 

Antwort

1

Ich missverstanden die Google Docs. Hier ist die Lösung

Features[] featuresArray = mViewModel.getFeature(); 
    final LatLngBounds.Builder builder = LatLngBounds.builder(); 
    if (featuresArray.length > neighborhoodIndex) { 
    Features currentFeature = featuresArray[neighborhoodIndex]; 
    GeoJsonLayer geoJsonLayer = getGeoJsonLayer(currentFeature); 


    GeoJsonPolygonStyle geoJsonPolygonStyle = geoJsonLayer.getDefaultPolygonStyle(); 


    int color = Color.parseColor(currentFeature.getProperties().getColor()); 
    int colorTransparent = ColorUtils.setAlphaComponent(color, 100); 
    geoJsonPolygonStyle.setStrokeColor(color); 
    geoJsonPolygonStyle.setFillColor(colorTransparent); 
    createViewArea(builder, currentFeature); 
    final CameraUpdate cameraUpdate = 
     CameraUpdateFactory.newLatLngBounds(builder.build(), 200); 
    mMap.animateCamera(cameraUpdate); 
    geoJsonLayer.addLayerToMap(); 
    } 
Verwandte Themen