diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesActivity.java index 5ad909e7f03..2183bf4401a 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesActivity.java @@ -51,6 +51,14 @@ public void onMapClick(@NonNull LatLng point) { Log.i(TAG, String.format("Requesting features for %sx%s (%sx%s adjusted for density)", pixel.x, pixel.y, pixel.x / density, pixel.y / density)); List features = mapboxMap.queryRenderedFeatures(pixel); Log.i(TAG, String.format("Got %s features", features.size())); + + for (Feature feature : features) { + if (feature != null) { + Log.i(TAG, String.format("Got feature %s", feature.getId())); + } else { + Log.i(TAG, "Got NULL feature %s"); + } + } } }); diff --git a/platform/android/src/geometry/conversion/feature.hpp b/platform/android/src/geometry/conversion/feature.hpp index 00e6e5f1b26..c91be1637d5 100644 --- a/platform/android/src/geometry/conversion/feature.hpp +++ b/platform/android/src/geometry/conversion/feature.hpp @@ -1,9 +1,12 @@ #pragma once +#include "../../conversion/constant.hpp" #include "../../conversion/conversion.hpp" -#include +#include +#include #include + #include #include @@ -15,25 +18,45 @@ namespace mbgl { namespace android { namespace conversion { +class FeatureIdVisitor { +public: + + template + std::string operator()(const T& i) const { + return std::to_string(i); + } + + std::string operator()(const std::string& i) const { + return i; + } + +}; + -template -struct Converter> { - Result operator()(jni::JNIEnv& env, const mapbox::geometry::feature& value) const { +template <> +struct Converter { + Result operator()(jni::JNIEnv& env, const mbgl::Feature& value) const { static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/mapbox/services/commons/geojson/Feature")).release(); - static jni::jmethodID* fromGeometry = &jni::GetStaticMethodID(env, *javaClass, "fromGeometry", "(Lcom/mapbox/services/commons/geojson/Geometry;)Lcom/mapbox/services/commons/geojson/Feature;"); + static jni::jmethodID* fromGeometry = &jni::GetStaticMethodID(env, *javaClass, "fromGeometry", "(Lcom/mapbox/services/commons/geojson/Geometry;Lcom/google/gson/JsonObject;Ljava/lang/String;)Lcom/mapbox/services/commons/geojson/Feature;"); + + //Get Id + FeatureIdVisitor idEvaluator; + std::string id = (value.id) ? mapbox::geometry::identifier::visit(value.id.value(), idEvaluator) : ""; + Result jid = convert(env, id); + /**, TODO: converted geometry**/ - return {reinterpret_cast(jni::CallStaticMethod(env, *javaClass, *fromGeometry, (jni::jobject*) nullptr))}; + return {reinterpret_cast(jni::CallStaticMethod(env, *javaClass, *fromGeometry, (jni::jobject*) nullptr, (jni::jobject*) nullptr, *jid))}; } }; -template -struct Converter*, std::vector>> { - Result*> operator()(jni::JNIEnv& env, const std::vector>& value) const { +template <> +struct Converter*, std::vector> { + Result*> operator()(jni::JNIEnv& env, const std::vector& value) const { static jni::jclass* featureClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/mapbox/services/commons/geojson/Feature")).release(); jni::jarray& jarray = jni::NewObjectArray(env, value.size(), *featureClass); for(size_t i = 0; i < value.size(); i = i + 1) { - Result converted = convert>(env, value.at(i)); + Result converted = convert(env, value.at(i)); jni::SetObjectArrayElement(env, jarray, i, *converted); } diff --git a/platform/android/src/geometry/conversion/geometry.hpp b/platform/android/src/geometry/conversion/geometry.hpp new file mode 100644 index 00000000000..fc0d5ce6fa8 --- /dev/null +++ b/platform/android/src/geometry/conversion/geometry.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "point.hpp" + +#include + +#include + +//inline jni::jobject* convert(JNIEnv& env, mapbox::geometry::feature feature) { +// if +//} diff --git a/platform/android/src/geometry/conversion/point.hpp b/platform/android/src/geometry/conversion/point.hpp new file mode 100644 index 00000000000..20581b1f846 --- /dev/null +++ b/platform/android/src/geometry/conversion/point.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "../../conversion/conversion.hpp" + +#include +#include +#include + +#include +#include +#include +#include + +namespace mbgl { +namespace android { +namespace conversion { + +inline jni::jobject* createGeoJsonPoint(double x, double y) { + +} + +template <> +struct Converter> { + Result operator()(jni::JNIEnv& env, const mapbox::geometry::feature& value) const { + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/mapbox/services/commons/geojson/Point")).release(); + static jni::jmethodID* constructor = &jni::GetMethodId(env, *javaClass, "", "(Lcom/mapbox/services/commons/geojson/Position;)V"); + return {&jni::NewObject(env, *javaClass, *constructor, createGeoJsonPoint(value.x, value.y))}; + } +}; + +} +} +} \ No newline at end of file diff --git a/platform/android/src/geometry/conversion/position.hpp b/platform/android/src/geometry/conversion/position.hpp new file mode 100644 index 00000000000..9ef71d6d9a4 --- /dev/null +++ b/platform/android/src/geometry/conversion/position.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "../../conversion/conversion.hpp" + +#include +#include +#include + +#include +#include +#include +#include + +namespace mbgl { +namespace android { +namespace conversion { + +template +struct Converter> { + Result operator()(jni::JNIEnv& env, const mapbox::geometry::feature& value) const { + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/mapbox/services/commons/geojson/Point")).release(); + static jni::jmethodID* constructor = &jni::GetStaticMethodID(env, *javaClass, "fromGeometry", "(Lcom/mapbox/services/commons/geojson/Geometry;)Lcom/mapbox/services/commons/geojson/Feature;"); + /**, TODO: converted geometry**/ + return {reinterpret_cast(jni::CallStaticMethod(env, *javaClass, *fromGeometry, (jni::jobject*) nullptr))}; + } +}; + +} +} +} \ No newline at end of file diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 2078ffb40a9..217dc6f12c4 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -28,10 +28,8 @@ #include #include #include -#include -#include -#include +#include #include @@ -934,6 +932,7 @@ void nativeSetVisibleCoordinateBounds(JNIEnv *env, jni::jobject* obj, jlong nati jni::jarray* nativeQueryRenderedFeatures(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jfloat x, jni::jfloat y, jni::jarray* layerIds) { using namespace mbgl::android::conversion; + using namespace mapbox::geometry; mbgl::Log::Debug(mbgl::Event::JNI, "nativeQueryRenderedFeatures"); assert(nativeMapViewPtr != 0); @@ -941,7 +940,7 @@ jni::jarray* nativeQueryRenderedFeatures(JNIEnv *env, jni::jobject //TODO: Convert layerIds - mapbox::geometry::point point = {x, y}; + point point = {x, y}; return *convert*, std::vector>(*env, nativeMapView->getMap().queryRenderedFeatures(point, {})); }