Expose sync methods in GeoJsonSource (#3560)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
liyanjin
2025-06-24 20:38:22 +08:00
committed by GitHub
parent 55ff6a4428
commit 282735772b
4 changed files with 217 additions and 0 deletions

View File

@@ -69,6 +69,16 @@ void GeoJSONSource::setGeoJSONString(jni::JNIEnv& env, const jni::String& jStrin
setAsync(converterFn);
}
void GeoJSONSource::setGeoJSONStringSync(jni::JNIEnv& env, const jni::String& jString) {
std::shared_ptr<std::string> json = std::make_shared<std::string>(jni::Make<std::string>(env, jString));
Update::Converter converterFn = [this, json](ActorRef<GeoJSONDataCallback> _callback) {
converter->self().ask(&FeatureConverter::convertJson, json, _callback).wait();
};
setAsync(converterFn);
}
void GeoJSONSource::setFeatureCollection(jni::JNIEnv& env, const jni::Object<geojson::FeatureCollection>& jFeatures) {
setCollectionAsync(env, jFeatures);
}
@@ -81,6 +91,19 @@ void GeoJSONSource::setGeometry(jni::JNIEnv& env, const jni::Object<geojson::Geo
setCollectionAsync(env, jGeometry);
}
void GeoJSONSource::setFeatureCollectionSync(jni::JNIEnv& env,
const jni::Object<geojson::FeatureCollection>& jFeatures) {
setCollectionSync(env, jFeatures);
}
void GeoJSONSource::setFeatureSync(jni::JNIEnv& env, const jni::Object<geojson::Feature>& jFeature) {
setCollectionSync(env, jFeature);
}
void GeoJSONSource::setGeometrySync(jni::JNIEnv& env, const jni::Object<geojson::Geometry>& jGeometry) {
setCollectionSync(env, jGeometry);
}
void GeoJSONSource::setURL(jni::JNIEnv& env, const jni::String& url) {
// Update the core source
source.as<style::GeoJSONSource>()->setURL(jni::Make<std::string>(env, url));
@@ -207,6 +230,18 @@ void GeoJSONSource::setAsync(Update::Converter converterFn) {
update->converterFn(update->callback->self());
}
template <class JNIType>
void GeoJSONSource::setCollectionSync(jni::JNIEnv& env, const jni::Object<JNIType>& jObject) {
auto global = jni::NewGlobal<jni::EnvAttachingDeleter>(env, jObject);
auto object = std::make_shared<decltype(global)>(std::move(global));
Update::Converter converterFn = [this, object](ActorRef<GeoJSONDataCallback> _callback) {
converter->self().ask(&FeatureConverter::convertObject<JNIType>, object, _callback).wait();
};
setAsync(converterFn);
}
void GeoJSONSource::registerNative(jni::JNIEnv& env) {
// Lookup the class
static auto& javaClass = jni::Class<GeoJSONSource>::Singleton(env);
@@ -225,6 +260,10 @@ void GeoJSONSource::registerNative(jni::JNIEnv& env) {
METHOD(&GeoJSONSource::setFeatureCollection, "nativeSetFeatureCollection"),
METHOD(&GeoJSONSource::setFeature, "nativeSetFeature"),
METHOD(&GeoJSONSource::setGeometry, "nativeSetGeometry"),
METHOD(&GeoJSONSource::setGeoJSONStringSync, "nativeSetGeoJsonStringSync"),
METHOD(&GeoJSONSource::setFeatureCollectionSync, "nativeSetFeatureCollectionSync"),
METHOD(&GeoJSONSource::setFeatureSync, "nativeSetFeatureSync"),
METHOD(&GeoJSONSource::setGeometrySync, "nativeSetGeometrySync"),
METHOD(&GeoJSONSource::setURL, "nativeSetUrl"),
METHOD(&GeoJSONSource::getURL, "nativeGetUrl"),
METHOD(&GeoJSONSource::querySourceFeatures, "querySourceFeatures"),

View File

@@ -54,6 +54,12 @@ private:
void setFeatureCollection(jni::JNIEnv&, const jni::Object<geojson::FeatureCollection>&);
void setFeature(jni::JNIEnv&, const jni::Object<geojson::Feature>&);
void setGeometry(jni::JNIEnv&, const jni::Object<geojson::Geometry>&);
void setGeoJSONStringSync(jni::JNIEnv&, const jni::String&);
void setFeatureCollectionSync(jni::JNIEnv&, const jni::Object<geojson::FeatureCollection>&);
void setFeatureSync(jni::JNIEnv&, const jni::Object<geojson::Feature>&);
void setGeometrySync(jni::JNIEnv&, const jni::Object<geojson::Geometry>&);
void setURL(jni::JNIEnv&, const jni::String&);
jni::Local<jni::Array<jni::Object<geojson::Feature>>> querySourceFeatures(jni::JNIEnv&,
@@ -80,6 +86,9 @@ private:
void setAsync(Update::Converter);
template <class JNIType>
void setCollectionSync(jni::JNIEnv&, const jni::Object<JNIType>&);
}; // class GeoJSONSource
} // namespace android