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

Commit

Permalink
refs #1969: bind getAnnotationsInBounds() in JNI
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Aug 27, 2015
1 parent ce6b958 commit abdb8de
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
94 changes: 94 additions & 0 deletions android/cpp/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jfieldID latLngZoomLatitudeId = nullptr;
jfieldID latLngZoomLongitudeId = nullptr;
jfieldID latLngZoomZoomId = nullptr;

jclass bboxClass = nullptr;
jfieldID bboxSWId = nullptr;
jfieldID bboxNEId = nullptr;

jclass markerClass = nullptr;
jmethodID markerConstructorId = nullptr;
jfieldID markerPositionId = nullptr;
Expand Down Expand Up @@ -970,6 +974,64 @@ void JNICALL nativeRemoveAnnotations(JNIEnv *env, jobject obj, jlong nativeMapVi
nativeMapView->getMap().removeAnnotations(ids);
}

jobject JNICALL nativeGetAnnotationsInBounds(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject bbox) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetAnnotationsInBounds");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

if (env->ExceptionCheck() || (bbox == nullptr)) {
env->ExceptionDescribe();
return nullptr;
}

jobject sw = env->GetObjectField(bbox, bboxSWId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}

jdouble swLat = env->GetDoubleField(sw, latLngLatitudeId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}

jdouble swLon = env->GetDoubleField(sw, latLngLongitudeId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}

jobject ne = env->GetObjectField(bbox, bboxNEId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}

jdouble neLat = env->GetDoubleField(ne, latLngLatitudeId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}

jdouble neLon = env->GetDoubleField(ne, latLngLongitudeId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}

mbgl::LatLngBounds bounds;
bounds.sw = { swLat, swLon };
bounds.ne = { neLat, neLon };

// assume only points for now
std::vector<uint32_t> annotations = nativeMapView->getMap().getAnnotationsInBounds(bounds, mbgl::AnnotationType::Point);

jobject ret = std_vector_uint_to_jobject(env, annotations);

return ret;
}

void JNICALL nativeSetSprite(JNIEnv *env, jobject obj, jlong nativeMapViewPtr,
jstring symbol, jint width, jint height, jfloat scale, jbyteArray jpixels) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetSprite");
Expand Down Expand Up @@ -1246,6 +1308,24 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

bboxClass = env->FindClass("com/mapbox/mapboxgl/geometry/BoundingBox");
if (bboxClass == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

bboxSWId = env->GetFieldID(bboxClass, "sw", "Lcom/mapbox/mapboxgl/geometry/LatLng;");
if (bboxSWId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

bboxNEId = env->GetFieldID(bboxClass, "ne", "Lcom/mapbox/mapboxgl/geometry/LatLng;");
if (bboxNEId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

markerClass = env->FindClass("com/mapbox/mapboxgl/annotations/Marker");
if (markerClass == nullptr) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -1596,6 +1676,8 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
reinterpret_cast<void *>(&nativeAddPolygons)},
{"nativeRemoveAnnotation", "(JJ)V", reinterpret_cast<void *>(&nativeRemoveAnnotation)},
{"nativeRemoveAnnotations", "(J[J)V", reinterpret_cast<void *>(&nativeRemoveAnnotations)},
{"nativeGetAnnotationsInBounds", "(J)Lcom/mapbox/mapboxgl/geometry/BoundingBox;",
reinterpret_cast<void *>(&nativeGetAnnotationsInBounds)},
{"nativeSetSprite", "(JLjava/lang/String;IIF[B)V", reinterpret_cast<void *>(&nativeSetSprite)},
{"nativeOnLowMemory", "(J)V", reinterpret_cast<void *>(&nativeOnLowMemory)},
{"nativeSetDebug", "(JZ)V", reinterpret_cast<void *>(&nativeSetDebug)},
Expand Down Expand Up @@ -1639,6 +1721,13 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

bboxClass = reinterpret_cast<jclass>(env->NewGlobalRef(bboxClass));
if (bboxClass == nullptr) {
env->ExceptionDescribe();
env->DeleteGlobalRef(bboxClass);
return JNI_ERR;
}

markerClass = reinterpret_cast<jclass>(env->NewGlobalRef(markerClass));
if (markerClass == nullptr) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -1795,6 +1884,11 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
latLngZoomLatitudeId = nullptr;
latLngZoomZoomId = nullptr;


env->DeleteGlobalRef(bboxClass);
bboxSWId = nullptr;
bboxNEId = nullptr;

env->DeleteGlobalRef(markerClass);
markerClass = nullptr;
markerConstructorId = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.mapbox.mapboxgl.annotations.PolygonOptions;
import com.mapbox.mapboxgl.annotations.Polyline;
import com.mapbox.mapboxgl.annotations.PolylineOptions;
import com.mapbox.mapboxgl.geometry.BoundingBox;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.geometry.LatLngZoom;
import com.mapzen.android.lost.api.LocationListener;
Expand Down Expand Up @@ -428,6 +429,10 @@ public List<Annotation> getAnnotations() {
return Collections.unmodifiableList(mAnnotations);
}

public List<Annotation> getAnnotationsInBounds(BoundingBox bbox) {
return mNativeMapView.getAnnotationsInBounds(bbox);
}

//
// Property methods
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import android.graphics.PointF;
import android.view.Surface;

import com.mapbox.mapboxgl.annotations.Annotation;
import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.annotations.Polygon;
import com.mapbox.mapboxgl.annotations.Polyline;
import com.mapbox.mapboxgl.geometry.BoundingBox;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.geometry.LatLngZoom;
import com.mapbox.mapboxgl.geometry.ProjectedMeters;

import java.util.ArrayList;
import java.util.List;

// Class that wraps the native methods for convenience
Expand Down Expand Up @@ -353,6 +356,10 @@ public void removeAnnotations(long[] ids) {
nativeRemoveAnnotations(mNativeMapViewPtr, ids);
}

public List<Annotation> getAnnotationsInBounds(BoundingBox bbox) {
return nativeGetAnnotationsInBounds(mNativeMapViewPtr, bbox);
}

public void setSprite(String symbol, int width, int height, float scale, byte[] pixels) {
nativeSetSprite(mNativeMapViewPtr, symbol, width, height, scale, pixels);
}
Expand Down Expand Up @@ -563,6 +570,8 @@ private native void nativeSetBearing(long nativeMapViewPtr, double degrees,

private native void nativeRemoveAnnotations(long nativeMapViewPtr, long[] id);

private native ArrayList<Annotation> nativeGetAnnotationsInBounds(long mNativeMapViewPtr, BoundingBox bbox);

private native void nativeSetSprite(long nativeMapViewPtr, String symbol,
int width, int height, float scale, byte[] pixels);

Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/android/jni.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ extern jfieldID latLngZoomLatitudeId;
extern jfieldID latLngZoomLongitudeId;
extern jfieldID latLngZoomZoomId;

extern jclass bboxClass;
extern jfieldID bboxSWId;
extern jfieldID bboxNEId;

extern jclass markerClass;
extern jmethodID markerConstructorId;
extern jfieldID markerPositionId;
Expand Down

0 comments on commit abdb8de

Please sign in to comment.