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

Commit

Permalink
[android] #5869 - convert feature identifier variants to java strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Aug 9, 2016
1 parent 211133c commit 32709bf
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Feature> 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");
}
}
}
});

Expand Down
43 changes: 33 additions & 10 deletions platform/android/src/geometry/conversion/feature.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include "../../conversion/constant.hpp"
#include "../../conversion/conversion.hpp"

#include <mbgl/util/optional.hpp>
#include <mbgl/util/feature.hpp>
#include <mapbox/variant.hpp>
#include <mapbox/geometry.hpp>

#include <jni/jni.hpp>

#include <string>
Expand All @@ -15,25 +18,45 @@ namespace mbgl {
namespace android {
namespace conversion {

class FeatureIdVisitor {
public:

template<class T>
std::string operator()(const T& i) const {
return std::to_string(i);
}

std::string operator()(const std::string& i) const {
return i;
}

};


template <class T>
struct Converter<jni::jobject*, mapbox::geometry::feature<T>> {
Result<jni::jobject*> operator()(jni::JNIEnv& env, const mapbox::geometry::feature<T>& value) const {
template <>
struct Converter<jni::jobject*, mbgl::Feature> {
Result<jni::jobject*> 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<jni::jobject*> jid = convert<jni::jobject*>(env, id);

/**, TODO: converted geometry**/
return {reinterpret_cast<jni::jobject*>(jni::CallStaticMethod<jni::jobject*>(env, *javaClass, *fromGeometry, (jni::jobject*) nullptr))};
return {reinterpret_cast<jni::jobject*>(jni::CallStaticMethod<jni::jobject*>(env, *javaClass, *fromGeometry, (jni::jobject*) nullptr, (jni::jobject*) nullptr, *jid))};
}
};

template <class T>
struct Converter<jni::jarray<jni::jobject>*, std::vector<mapbox::geometry::feature<T>>> {
Result<jni::jarray<jni::jobject>*> operator()(jni::JNIEnv& env, const std::vector<mapbox::geometry::feature<T>>& value) const {
template <>
struct Converter<jni::jarray<jni::jobject>*, std::vector<mbgl::Feature>> {
Result<jni::jarray<jni::jobject>*> operator()(jni::JNIEnv& env, const std::vector<mbgl::Feature>& value) const {
static jni::jclass* featureClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/mapbox/services/commons/geojson/Feature")).release();
jni::jarray<jni::jobject>& jarray = jni::NewObjectArray(env, value.size(), *featureClass);

for(size_t i = 0; i < value.size(); i = i + 1) {
Result<jni::jobject*> converted = convert<jni::jobject*, mapbox::geometry::feature<T>>(env, value.at(i));
Result<jni::jobject*> converted = convert<jni::jobject*, mbgl::Feature>(env, value.at(i));
jni::SetObjectArrayElement(env, jarray, i, *converted);
}

Expand Down
11 changes: 11 additions & 0 deletions platform/android/src/geometry/conversion/geometry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "point.hpp"

#include <mapbox/geometry.hpp>

#include <jni/jni.hpp>

//inline jni::jobject* convert(JNIEnv& env, mapbox::geometry::feature<Geometry> feature) {
// if
//}
33 changes: 33 additions & 0 deletions platform/android/src/geometry/conversion/point.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "../../conversion/conversion.hpp"

#include <mbgl/util/optional.hpp>
#include <mapbox/geometry.hpp>
#include <jni/jni.hpp>

#include <string>
#include <array>
#include <vector>
#include <sstream>

namespace mbgl {
namespace android {
namespace conversion {

inline jni::jobject* createGeoJsonPoint(double x, double y) {

}

template <>
struct Converter<jni::jobject*, mapbox::geometry::feature<point>> {
Result<jni::jobject*> operator()(jni::JNIEnv& env, const mapbox::geometry::feature<point>& 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, "<init>", "(Lcom/mapbox/services/commons/geojson/Position;)V");
return {&jni::NewObject(env, *javaClass, *constructor, createGeoJsonPoint(value.x, value.y))};
}
};

}
}
}
30 changes: 30 additions & 0 deletions platform/android/src/geometry/conversion/position.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "../../conversion/conversion.hpp"

#include <mbgl/util/optional.hpp>
#include <mapbox/geometry.hpp>
#include <jni/jni.hpp>

#include <string>
#include <array>
#include <vector>
#include <sstream>

namespace mbgl {
namespace android {
namespace conversion {

template <class T>
struct Converter<jni::jobject*, mapbox::geometry::feature<point>> {
Result<jni::jobject*> operator()(jni::JNIEnv& env, const mapbox::geometry::feature<point>& 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::jobject*>(jni::CallStaticMethod<jni::jobject*>(env, *javaClass, *fromGeometry, (jni::jobject*) nullptr))};
}
};

}
}
}
7 changes: 3 additions & 4 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
#include <mbgl/util/exception.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/feature.hpp>

#include <mapbox/geometry/point.hpp>
#include <mapbox/geometry/box.hpp>
#include <mapbox/geometry.hpp>

#include <jni/jni.hpp>

Expand Down Expand Up @@ -934,14 +932,15 @@ void nativeSetVisibleCoordinateBounds(JNIEnv *env, jni::jobject* obj, jlong nati

jni::jarray<jni::jobject>* nativeQueryRenderedFeatures(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jfloat x, jni::jfloat y, jni::jarray<jni::jstring>* layerIds) {
using namespace mbgl::android::conversion;
using namespace mapbox::geometry;

mbgl::Log::Debug(mbgl::Event::JNI, "nativeQueryRenderedFeatures");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

//TODO: Convert layerIds

mapbox::geometry::point<double> point = {x, y};
point<double> point = {x, y};
return *convert<jni::jarray<jni::jobject>*, std::vector<mbgl::Feature>>(*env, nativeMapView->getMap().queryRenderedFeatures(point, {}));
}

Expand Down

0 comments on commit 32709bf

Please sign in to comment.