diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java index 6f2113ab0e..fd226a0060 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java @@ -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); + } } |