diff options
author | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2016-09-21 11:04:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-21 11:04:32 +0200 |
commit | eb97dbe383ca7697feab5860995b97181c39c607 (patch) | |
tree | 70bdd7b6ebea32aec132413fa703e92a2a0f63d0 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java | |
parent | 3b546b964609d0f596dac32e155b1489bb85645e (diff) | |
download | qtlocation-mapboxgl-eb97dbe383ca7697feab5860995b97181c39c607.tar.gz |
[android] Sources: peer model, mutability (#6054)
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java | 123 |
1 files changed, 102 insertions, 21 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 fd226a0060..a66e0a4adc 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,9 +1,10 @@ package com.mapbox.mapboxsdk.style.sources; -import com.google.gson.Gson; +import com.mapbox.services.commons.geojson.Feature; import com.mapbox.services.commons.geojson.FeatureCollection; import java.net.URL; +import java.util.ArrayList; import java.util.HashMap; /** @@ -12,8 +13,34 @@ import java.util.HashMap; * @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">the style specification</a> */ public class GeoJsonSource extends Source { - public static final String TYPE = "geojson"; - private static final String DATA_KEY = "data"; + + /** + * Internal use + */ + public GeoJsonSource(long nativePtr) { + super(nativePtr); + } + + /** + * Create an empty GeoJsonSource + * + * @param id the source id + */ + public GeoJsonSource(String id) { + initialize(id, null); + setGeoJson(FeatureCollection.fromFeatures(new ArrayList<Feature>())); + } + + /** + * Create an empty GeoJsonSource + * + * @param id the source id + * @param options options + */ + public GeoJsonSource(String id, GeoJsonOptions options) { + initialize(id, options); + setGeoJson(FeatureCollection.fromFeatures(new ArrayList<Feature>())); + } /** * Create a GeoJsonSource from a raw json string @@ -22,11 +49,26 @@ public class GeoJsonSource extends Source { * @param geoJson raw Json body */ public GeoJsonSource(String id, String geoJson) { - super(id, TYPE); if (geoJson == null || geoJson.startsWith("http")) { throw new IllegalArgumentException("Expected a raw json body"); } - setRawJson(geoJson); + initialize(id, null); + setGeoJson(geoJson); + } + + /** + * Create a GeoJsonSource from a raw json string + * + * @param id the source id + * @param geoJson raw Json body + * @param options options + */ + public GeoJsonSource(String id, String geoJson, GeoJsonOptions options) { + if (geoJson == null || geoJson.startsWith("http")) { + throw new IllegalArgumentException("Expected a raw json body"); + } + initialize(id, options); + setGeoJson(geoJson); } /** @@ -36,8 +78,20 @@ public class GeoJsonSource extends Source { * @param url remote json file */ public GeoJsonSource(String id, URL url) { - super(id, TYPE); - this.put(DATA_KEY, url.toExternalForm()); + initialize(id, null); + nativeSetUrl(url.toExternalForm()); + } + + /** + * Create a GeoJsonSource from a remote geo json file + * + * @param id the source id + * @param url remote json file + * @param options options + */ + public GeoJsonSource(String id, URL url, GeoJsonOptions options) { + initialize(id, options); + nativeSetUrl(url.toExternalForm()); } /** @@ -47,30 +101,57 @@ public class GeoJsonSource extends Source { * @param features the features */ public GeoJsonSource(String id, FeatureCollection features) { - super(id, TYPE); - setRawJson(features.toJson()); + initialize(id, null); + setGeoJson(features); + } + + /** + * Create a GeoJsonSource from a FeatureCollection + * + * @param id the source id + * @param features the features + * @param options options + */ + public GeoJsonSource(String id, FeatureCollection features, GeoJsonOptions options) { + initialize(id, options); + setGeoJson(features); + } + + public void setGeoJson(FeatureCollection features) { + checkValidity(); + setGeoJson(features.toJson()); } - public GeoJsonSource withCluster(boolean cluster) { - this.put("cluster", cluster); - return this; + public void setGeoJson(String json) { + checkValidity(); + setRawJson(json); } - public GeoJsonSource withClusterMaxZoom(float zoom) { - this.put("clusterMaxZoom", zoom); - return this; + public void setUrl(URL url) { + checkValidity(); + setUrl(url.toExternalForm()); } - public GeoJsonSource withClusterRadius(float radius) { - this.put("clusterRadius", radius); - return this; + public void setUrl(String url) { + checkValidity(); + nativeSetUrl(url); } - private void setRawJson(String geoJson) { + protected 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); + wrapper.put("data", geoJson); + nativeSetGeoJson(wrapper); } + + protected native void initialize(String layerId, Object options); + + protected native void nativeSetUrl(String url); + + private native void nativeSetGeoJson(Object geoJson); + + @Override + protected native void finalize() throws Throwable; + } |