【问题标题】:Set the color and shaded area on a geojson object / geolayer for google maps sdk android在 google maps sdk android 的 geojson 对象/geolayer 上设置颜色和阴影区域
【发布时间】:2017-12-18 23:15:14
【问题描述】:

我想获取我的 Geojson 图层并为其应用颜色。使用下面的代码,我在该区域顶部只有一条黑线。那部分是正确的,我只需要为该区域添加颜色和阴影。但是,我没有看到任何使用 geojson 或 geolayer 对象的简单方法

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

特征模型

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

然后该区域基于什么。 (请注意,它始终是多边形,而不是线)。应该是 vanilla geojson 规范

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

【问题讨论】:

    标签: android google-maps-android-api-2 geojson


    【解决方案1】:

    我误解了谷歌文档。这是解决方案

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

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      相关资源
      最近更新 更多