Search code examples
androidgoogle-maps-android-api-2geojson

Set the color and shaded area on a geojson object / geolayer for google maps sdk android


I want to take my Geojson layer and apply a color to it. With the code below I only have a black line on top of the area. That part is correct, I just need to add color and shading to the area. However I am not seeing any simple way to do this with a geojson or geolayer object

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 model

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 + "]";
  }
}

Then what the area is based on. (Note that is always a polygon, not lines). Should be vanilla geojson spec

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 + "]";
  }
}

Solution

  • I missunderstood the google docs. Here is the solution

      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();
      }