osmbonuspack 中的 FolderOverlay 如何从 JSON 获取样式?
How say FolderOverlay in osmbonuspack to take style from JSON?
我在我的项目中使用osmbonuspack。传入的 GeoJson 具有字段 "style":
{"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();
我找到了解决方案。现在只针对多边形,我在文件中没有其他对象。
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){}
}
}
我在我的项目中使用osmbonuspack。传入的 GeoJson 具有字段 "style":
{"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();
我找到了解决方案。现在只针对多边形,我在文件中没有其他对象。
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){}
}
}