Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] complete peer model, add geojson options construction time
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Sep 5, 2016
1 parent 7763ce7 commit f1b9ffb
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.mapbox.mapboxsdk.style.sources;

import java.util.HashMap;

/**
* Options for the <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">GeoJsonSource</a>
*/
public class GeoJsonOptions extends HashMap<String, Object> {

/**
* Defaults to 18.
* Maximum zoom level at which to create vector tiles (higher means greater detail at high zoom levels).
*/
public GeoJsonOptions withMaxZoom(int maxZoom) {
this.put("maxzoom", maxZoom);
return this;
}

/**
* Defaults to 128.
* Tile buffer size on each side (measured in 1/512ths of a tile; higher means fewer rendering artifacts near tile edges but slower performance).
*/
public GeoJsonOptions withBuffer(int buffer) {
this.put("buffer", buffer);
return this;
}

/**
* Defaults to 0.375.
* Douglas-Peucker simplification tolerance (higher means simpler geometries and faster performance).
*/
public GeoJsonOptions withTolerance(float tolerance) {
this.put("tolerance", tolerance);
return this;
}

/**
* Defaults to false.
* If the data is a collection of point features, setting this to true clusters the points by radius into groups.
*/
public GeoJsonOptions withCluster(boolean cluster) {
this.put("cluster", cluster);
return this;
}

/**
* Defaults to 50.
* Radius of each cluster when clustering points, measured in 1/512ths of a tile.
*/
public GeoJsonOptions withClusterMaxZoom(int clusterMaxZoom) {
this.put("clusterMaxZoom", clusterMaxZoom);
return this;
}

/**
* Max zoom to cluster points on. Defaults to one zoom less than maxzoom (so that last zoom features are not clustered).
*/
public GeoJsonOptions withClusterRadius(int clusterRadius) {
this.put("clusterRadius", clusterRadius);
return this;
}

}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -13,13 +14,32 @@
*/
public class GeoJsonSource extends Source {

/**
* Internal use
*/
public GeoJsonSource(long nativePtr) {
super(nativePtr);
}

/**
* Create an empty GeoJsonSource
*
* @param id the source id
*/
public GeoJsonSource(String id) {
initialize(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>()));
}

/**
Expand All @@ -32,8 +52,23 @@ public GeoJsonSource(String id, String geoJson) {
if (geoJson == null || geoJson.startsWith("http")) {
throw new IllegalArgumentException("Expected a raw json body");
}
initialize(id);
nativeSetGeoJson(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);
}

/**
Expand All @@ -43,7 +78,19 @@ public GeoJsonSource(String id, String geoJson) {
* @param url remote json file
*/
public GeoJsonSource(String id, URL url) {
initialize(id);
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());
}

Expand All @@ -54,46 +101,55 @@ public GeoJsonSource(String id, URL url) {
* @param features the features
*/
public GeoJsonSource(String id, FeatureCollection features) {
initialize(id);
nativeSetGeoJson(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) {
nativeSetGeoJson(features.toJson());
checkValidity();
setGeoJson(features.toJson());
}

public void setGeoJson(String json) {
nativeSetGeoJson(json);
checkValidity();
setRawJson(json);
}

public void setUrl(URL url) {
nativeSetUrl(url.toExternalForm());
checkValidity();
setUrl(url.toExternalForm());
}

public void setUrl(String url) {
checkValidity();
nativeSetUrl(url);
}

public GeoJsonSource withCluster(boolean cluster) {
//TODO this.put("cluster", cluster);
return this;
}

public GeoJsonSource withClusterMaxZoom(float zoom) {
//TODO this.put("clusterMaxZoom", zoom);
return this;
}

public GeoJsonSource withClusterRadius(float radius) {
//TODO this.put("clusterRadius", radius);
return this;
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", geoJson);
nativeSetGeoJson(wrapper);
}

protected native void initialize(String layerId);
protected native void initialize(String layerId, Object options);

protected native void nativeSetUrl(String url);

protected native void nativeSetGeoJson(String geoJson);
private native void nativeSetGeoJson(Object geoJson);

@Override
protected native void finalize() throws Throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
public class RasterSource extends Source {
public static final int DEFAULT_TILE_SIZE = 512;

/**
* Internal use
*/
public RasterSource(long nativePtr) {
super(nativePtr);
}

public RasterSource(String id, URL url) {
this(id, url.toExternalForm());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
*/
public class VectorSource extends Source {

/**
* Internal use
*/
public VectorSource(long nativePtr) {
super(nativePtr);
}

/**
* Create a vector source from a remote url
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;

Expand Down Expand Up @@ -110,10 +111,13 @@ private void addClusteredGeoJsonSource() {
//Add a clustered source
try {
mapboxMap.addSource(
new GeoJsonSource("earthquakes", new URL("https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"))
.withCluster(true)
.withClusterMaxZoom(14)
.withClusterRadius(50)
new GeoJsonSource("earthquakes",
new URL("https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"),
new GeoJsonOptions()
.withCluster(true)
.withClusterMaxZoom(14)
.withClusterRadius(50)
)
);
} catch (MalformedURLException malformedUrlException) {
Log.e(TAG, "That's not an url... " + malformedUrlException.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,13 @@ private void addDynamicParksLayer() {
return;
}

//Add a source
//Add an empty source
mapboxMap.addSource(new GeoJsonSource("dynamic-park-source"));

FillLayer layer = new FillLayer("dynamic-parks-layer", "dynamic-park-source");
layer.setProperties(
fillColor(Color.RED),
fillOutlineColor(Color.BLUE),
fillColor(Color.GREEN),
fillOutlineColor(Color.GREEN),
fillOpacity(0.8f),
fillAntialias(true)
);
Expand Down Expand Up @@ -325,6 +325,12 @@ public void run() {

GeoJsonSource source = mapboxMap.getSourceAs("dynamic-park-source");

if (source == null) {
Log.e(TAG, "Source not found");
Toast.makeText(RuntimeStyleActivity.this, "Source not found", Toast.LENGTH_SHORT).show();
return;
}

List<Feature> features = new ArrayList<>();
features.add(parks.getFeatures().get(park));
source.setGeoJson(FeatureCollection.fromFeatures(features));
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {

java::registerNatives(env);
registerNativeLayers(env);
registerNativeSources(env);

latLngClass = &jni::FindClass(env, "com/mapbox/mapboxsdk/geometry/LatLng");
latLngClass = jni::NewGlobalRef(env, latLngClass).release();
Expand Down
22 changes: 17 additions & 5 deletions platform/android/src/style/sources/geojson_source.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include "geojson_source.hpp"

#include <mbgl/style/conversion.hpp>
#include "../android_conversion.hpp"
#include "../conversion/geojson.hpp"
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson_options.hpp>

#include <string>

namespace mbgl {
namespace android {

GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, jni::String sourceId)
: Source(env, std::make_unique<mbgl::style::GeoJSONSource>(jni::Make<std::string>(env, sourceId))) {
GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, jni::String sourceId, jni::Object<> options)
: Source(env, std::make_unique<mbgl::style::GeoJSONSource>(
jni::Make<std::string>(env, sourceId),
options ? *style::conversion::convert<style::GeoJSONOptions>(Value(env, options)) : style::GeoJSONOptions()
)
) {
}

GeoJSONSource::GeoJSONSource(mbgl::Map& map, mbgl::style::GeoJSONSource& coreSource)
Expand All @@ -30,11 +36,17 @@ namespace android {

//Update the core source
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setGeoJSON(*converted);

//Repaint
updateStyle(false);
}

void GeoJSONSource::setURL(jni::JNIEnv& env, jni::String url) {
//Update the core source
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setURL(jni::Make<std::string>(env, url));

//Repaint
updateStyle(false);
}

jni::Class<GeoJSONSource> GeoJSONSource::javaClass;
Expand All @@ -53,11 +65,11 @@ namespace android {
//Register the peer
jni::RegisterNativePeer<GeoJSONSource>(
env, GeoJSONSource::javaClass, "nativePtr",
std::make_unique<GeoJSONSource, JNIEnv&, jni::String>,
std::make_unique<GeoJSONSource, JNIEnv&, jni::String, jni::Object<>>,
"initialize",
"finalize",
METHOD(&GeoJSONSource::setGeoJSON, "nativeSetGeoJson"),
METHOD(&GeoJSONSource::setGeoJSON, "nativeSetUrl")
METHOD(&GeoJSONSource::setURL, "nativeSetUrl")
);
}

Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/geojson_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GeoJSONSource : public Source {

static void registerNative(jni::JNIEnv&);

GeoJSONSource(jni::JNIEnv&, jni::String);
GeoJSONSource(jni::JNIEnv&, jni::String, jni::Object<>);

GeoJSONSource(mbgl::Map&, mbgl::style::GeoJSONSource&);

Expand Down
Loading

0 comments on commit f1b9ffb

Please sign in to comment.