[android] add option to set geometry and feature on geojson source

This commit is contained in:
Ivo van Dongen
2017-03-23 17:53:59 +02:00
committed by Ivo van Dongen
parent fa7489fb7e
commit 02fd38ccbc
3 changed files with 96 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import android.support.annotation.UiThread;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Geometry;
import java.net.URL;
import java.util.ArrayList;
@@ -126,6 +127,70 @@ public class GeoJsonSource extends Source {
setGeoJson(features);
}
/**
* Create a GeoJsonSource from a {@link Feature}
*
* @param id the source id
* @param feature the feature
*/
public GeoJsonSource(String id, Feature feature) {
initialize(id, null);
setGeoJson(feature);
}
/**
* Create a GeoJsonSource from a {@link Feature} and non-default {@link GeoJsonOptions}
*
* @param id the source id
* @param feature the feature
* @param options options
*/
public GeoJsonSource(String id, Feature feature, GeoJsonOptions options) {
initialize(id, options);
setGeoJson(feature);
}
/**
* Create a GeoJsonSource from a {@link Geometry}
*
* @param id the source id
* @param geometry the geometry
*/
public GeoJsonSource(String id, Geometry geometry) {
initialize(id, null);
setGeoJson(geometry);
}
/**
* Create a GeoJsonSource from a {@link Geometry} and non-default {@link GeoJsonOptions}
*
* @param id the source id
* @param geometry the geometry
* @param options options
*/
public GeoJsonSource(String id, Geometry geometry, GeoJsonOptions options) {
initialize(id, options);
setGeoJson(geometry);
}
/**
* Updates the GeoJson with a single feature
*
* @param feature the GeoJSON {@link Feature} to set
*/
public void setGeoJson(Feature feature) {
nativeSetFeature(feature);
}
/**
* Updates the GeoJson with a single geometry
*
* @param geometry the GeoJSON {@link Geometry} to set
*/
public void setGeoJson(Geometry<?> geometry) {
nativeSetGeometry(geometry);
}
/**
* Updates the GeoJson
*
@@ -182,6 +247,10 @@ public class GeoJsonSource extends Source {
private native void nativeSetFeatureCollection(FeatureCollection geoJson);
private native void nativeSetFeature(Feature feature);
private native void nativeSetGeometry(Geometry<?> geometry);
private native Feature[] querySourceFeatures(Object[] filter);
@Override

View File

@@ -72,6 +72,26 @@ namespace android {
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setGeoJSON(GeoJSON(features));
}
void GeoJSONSource::setFeature(jni::JNIEnv& env, jni::Object<geojson::Feature> jFeature) {
using namespace mbgl::android::geojson;
// Convert the jni object
auto feature = Feature::convert(env, jFeature);
// Update the core source
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setGeoJSON(GeoJSON(feature));
}
void GeoJSONSource::setGeometry(jni::JNIEnv& env, jni::Object<geojson::Geometry> jGeometry) {
using namespace mbgl::android::geojson;
// Convert the jni object
auto geometry = Geometry::convert(env, jGeometry);
// Update the core source
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setGeoJSON(GeoJSON(geometry));
}
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));
@@ -108,6 +128,8 @@ namespace android {
"finalize",
METHOD(&GeoJSONSource::setGeoJSONString, "nativeSetGeoJsonString"),
METHOD(&GeoJSONSource::setFeatureCollection, "nativeSetFeatureCollection"),
METHOD(&GeoJSONSource::setFeature, "nativeSetFeature"),
METHOD(&GeoJSONSource::setGeometry, "nativeSetGeometry"),
METHOD(&GeoJSONSource::setURL, "nativeSetUrl"),
METHOD(&GeoJSONSource::querySourceFeatures, "querySourceFeatures")
);

View File

@@ -2,6 +2,7 @@
#include "source.hpp"
#include <mbgl/style/sources/geojson_source.hpp>
#include "../../geojson/geometry.hpp"
#include "../../geojson/feature.hpp"
#include "../../geojson/feature_collection.hpp"
#include <jni/jni.hpp>
@@ -28,6 +29,10 @@ public:
void setFeatureCollection(jni::JNIEnv&, jni::Object<geojson::FeatureCollection>);
void setFeature(jni::JNIEnv&, jni::Object<geojson::Feature>);
void setGeometry(jni::JNIEnv&, jni::Object<geojson::Geometry>);
void setURL(jni::JNIEnv&, jni::String);
jni::Array<jni::Object<geojson::Feature>> querySourceFeatures(jni::JNIEnv&,