[android] #5941 - add geojson typed constructor to GeoJsonSource and an example
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.mapbox.mapboxsdk.style.sources;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.mapbox.services.commons.geojson.FeatureCollection;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -23,12 +26,7 @@ public class GeoJsonSource extends Source {
|
||||
if (geoJson == null || geoJson.startsWith("http")) {
|
||||
throw new IllegalArgumentException("Expected a raw json body");
|
||||
}
|
||||
|
||||
//Wrap the String in a map as an Object is expected by the
|
||||
//style conversion template
|
||||
HashMap<String, String> wrapper = new HashMap<>();
|
||||
wrapper.put(DATA_KEY, geoJson);
|
||||
this.put(DATA_KEY, wrapper);
|
||||
setRawJson(geoJson);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,6 +40,17 @@ public class GeoJsonSource extends Source {
|
||||
this.put(DATA_KEY, url.toExternalForm());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a GeoJsonSource from a FeatureCollection
|
||||
*
|
||||
* @param id the source id
|
||||
* @param features the features
|
||||
*/
|
||||
public GeoJsonSource(String id, FeatureCollection features) {
|
||||
super(id, TYPE);
|
||||
setRawJson(features.toJson());
|
||||
}
|
||||
|
||||
public GeoJsonSource withCluster(boolean cluster) {
|
||||
this.put("cluster", cluster);
|
||||
return this;
|
||||
@@ -56,4 +65,12 @@ public class GeoJsonSource extends Source {
|
||||
this.put("clusterRadius", radius);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void setRawJson(String geoJson) {
|
||||
//Wrap the String in a map as an Object is expected by the
|
||||
//style conversion template
|
||||
HashMap<String, String> wrapper = new HashMap<>();
|
||||
wrapper.put(DATA_KEY, geoJson);
|
||||
this.put(DATA_KEY, wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user