为 google maps sdk android 设置 geojson 对象/地理层的颜色和阴影区域
Set the color and shaded area on a geojson object / geolayer for google maps sdk android
我想使用我的 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 + "]";
}
}
那么这个区域是根据什么来的。 (请注意,它始终是多边形,而不是线)。应该是普通的 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 + "]";
}
}
我误解了 google 文档。这是解决方案
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();
}
我想使用我的 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 + "]";
}
}
那么这个区域是根据什么来的。 (请注意,它始终是多边形,而不是线)。应该是普通的 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 + "]";
}
}
我误解了 google 文档。这是解决方案
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();
}