[android] #5941 - add geojson typed constructor to GeoJsonSource and an example

This commit is contained in:
Ivo van Dongen
2016-08-16 16:11:39 +02:00
parent 2a5bf311ac
commit 01a33749f9
2 changed files with 39 additions and 28 deletions

View File

@@ -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);
}
}