summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiklas Ulvinge <ulvinge@gmail.com>2015-07-19 00:27:45 +0200
committerNiklas Ulvinge <ulvinge@gmail.com>2015-07-19 00:27:45 +0200
commit45311e98def801a8c47e5ec85e116c2d7d622e54 (patch)
tree948e3cb492addcc851e47dddcffb273f97586819
parent0f56c2b0d5766996d51c73f5e131ec8df21d5fca (diff)
downloadqtlocation-mapboxgl-45311e98def801a8c47e5ec85e116c2d7d622e54.tar.gz
Add multiple polygons at once to increase performance
-rw-r--r--android/cpp/jni.cpp112
-rw-r--r--android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java17
-rw-r--r--android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java7
-rw-r--r--android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java10
4 files changed, 118 insertions, 28 deletions
diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp
index 1e2288c61b..dd75caa214 100644
--- a/android/cpp/jni.cpp
+++ b/android/cpp/jni.cpp
@@ -248,6 +248,27 @@ jobject std_vector_string_to_jobject(JNIEnv *env, std::vector<std::string> vecto
return jlist;
}
+
+jlongArray std_vector_uint_to_jobject(JNIEnv *env, std::vector<uint32_t> vector) {
+ jlongArray jarray = env->NewLongArray(vector.size());
+ if (jarray == nullptr) {
+ env->ExceptionDescribe();
+ return nullptr;
+ }
+
+ std::vector<jlong> v;
+ for (const uint32_t& id : vector) {
+ v.push_back((jlong)id);
+ }
+
+ env->SetLongArrayRegion(jarray, 0, v.size(), &(v[0]));
+ if (env->ExceptionCheck()) {
+ env->ExceptionDescribe();
+ return nullptr;
+ }
+
+ return jarray;
+}
}
}
@@ -599,11 +620,8 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr
return (jlong) id;
}
-jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject polygon) {
- mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolygon");
- assert(nativeMapViewPtr != 0);
- NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
+std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> readPolygon(JNIEnv *env, jobject polygon) {
// ***** Java fields ***** //
// float alpha;
// boolean visible;
@@ -614,29 +632,10 @@ jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr,
// List<List<LatLng>> holes
jfloat alpha = env->GetFloatField(polygon, polygonAlphaId);
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
- return -1;
- }
-
jboolean visible = env->GetBooleanField(polygon, polygonVisibleId);
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
- return -1;
- }
visible = JNI_TRUE;
-
jint fillColor = env->GetIntField(polygon, polygonFillColorId);
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
- return -1;
- }
-
jint strokeColor = env->GetIntField(polygon, polygonStrokeColorId);
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
- return -1;
- }
int rF = (fillColor>>16)&0xFF;
int gF = (fillColor>>8)&0xFF;
@@ -664,14 +663,77 @@ jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr,
jobject points = env->GetObjectField(polygon, polygonPointsId);
mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points);
+ return std::make_pair(segment, shapeProperties);
+}
+
+
+jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject polygon) {
+ mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolygon");
+ assert(nativeMapViewPtr != 0);
+ NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
+
std::vector<mbgl::ShapeAnnotation> shapes;
- shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, shapeProperties);
+ std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> segment = readPolygon(env, polygon);
+
+ if (env->ExceptionCheck()) {
+ env->ExceptionDescribe();
+ return -1;
+ }
+
+ shapes.emplace_back(mbgl::AnnotationSegments {{ segment.first }}, segment.second);
std::vector<uint32_t> shapeAnnotationIDs = nativeMapView->getMap().addShapeAnnotations(shapes);
uint32_t id = shapeAnnotationIDs.at(0);
return (jlong) id;
}
+jlongArray JNICALL nativeAddPolygons(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject jlist) {
+ mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolygons");
+ assert(nativeMapViewPtr != 0);
+ NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
+
+ std::vector<mbgl::ShapeAnnotation> shapes;
+
+ if (jlist == nullptr) {
+ if (env->ThrowNew(nullPointerExceptionClass, "List cannot be null.") < 0) {
+ env->ExceptionDescribe();
+ return nullptr;
+ }
+ return nullptr;
+ }
+
+ jobjectArray array =
+ reinterpret_cast<jobjectArray>(env->CallObjectMethod(jlist, listToArrayId));
+ if (env->ExceptionCheck() || (array == nullptr)) {
+ env->ExceptionDescribe();
+ return nullptr;
+ }
+
+ jsize len = env->GetArrayLength(array);
+ if (len < 0) {
+ env->ExceptionDescribe();
+ return nullptr;
+ }
+
+ shapes.reserve(len);
+
+ for (jsize i = 0; i < len; i++) {
+ jobject polygon = reinterpret_cast<jobject>(env->GetObjectArrayElement(array, i));
+
+ std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> segment = readPolygon(env, polygon);
+ if (env->ExceptionCheck()) {
+ env->ExceptionDescribe();
+ return nullptr;
+ }
+
+ shapes.emplace_back(mbgl::AnnotationSegments {{ segment.first }}, segment.second);
+ }
+
+ std::vector<uint32_t> shapeAnnotationIDs = nativeMapView->getMap().addShapeAnnotations(shapes);
+ return std_vector_uint_to_jobject(env, shapeAnnotationIDs);
+}
+
+
void JNICALL nativeRemoveAnnotation(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jlong annotationId) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeRemoveAnnotation");
assert(nativeMapViewPtr != 0);
@@ -1348,6 +1410,8 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
reinterpret_cast<void *>(&nativeAddPolyline)},
{"nativeAddPolygon", "(JLcom/mapbox/mapboxgl/annotations/Polygon;)J",
reinterpret_cast<void *>(&nativeAddPolygon)},
+ {"nativeAddPolygons", "(JLjava/util/List;)[J",
+ reinterpret_cast<void *>(&nativeAddPolygons)},
{"nativeRemoveAnnotation", "(JJ)V", reinterpret_cast<void *>(&nativeRemoveAnnotation)},
{"nativeGetLatLng", "(J)Lcom/mapbox/mapboxgl/geometry/LatLng;",
reinterpret_cast<void *>(&nativeGetLatLng)},
diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java
index 4a8ed42a54..4ad0d66b89 100644
--- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java
+++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java
@@ -243,6 +243,23 @@ public class MapView extends SurfaceView {
return polygon;
}
+ public List<Polygon> addPolygons(List<PolygonOptions> polygonOptions) {
+ List<Polygon> polygons = new ArrayList<>();
+ for(PolygonOptions popts : polygonOptions) {
+ polygons.add(popts.getPolygon());
+ }
+
+ long[] ids = mNativeMapView.addPolygons(polygons);
+
+ for(int i=0; i<polygons.size(); i++) {
+ polygons.get(i).setId(ids[i]);
+ polygons.get(i).setMapView(this);
+ annotations.add(polygons.get(i));
+ }
+
+ return polygons;
+ }
+
public void removeAnnotation(Annotation annotation) {
long id = annotation.getId();
mNativeMapView.removeAnnotation(id);
diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java
index 6cab4bbd0f..304f014f69 100644
--- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java
+++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java
@@ -230,6 +230,11 @@ class NativeMapView {
return nativeAddPolygon(mNativeMapViewPtr, polygon);
}
+ public long[] addPolygons(List<Polygon> polygon) {
+ // NH TODO Throw exception if returns -1
+ return nativeAddPolygons(mNativeMapViewPtr, polygon);
+ }
+
public void removeAnnotation(long id) {
nativeRemoveAnnotation(mNativeMapViewPtr, id);
}
@@ -472,6 +477,8 @@ class NativeMapView {
private native long nativeAddPolygon(long mNativeMapViewPtr, Polygon polygon);
+ private native long[] nativeAddPolygons(long mNativeMapViewPtr, List<Polygon> polygon);
+
private native void nativeRemoveAnnotation(long nativeMapViewPtr, long id);
private native LatLng nativeGetLatLng(long nativeMapViewPtr);
diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java
index f8c35897c2..bb9a8e7518 100644
--- a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java
+++ b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java
@@ -327,10 +327,12 @@ public class MainActivity extends ActionBarActivity {
geojsonStr = Util.loadStringFromAssets(this, "small_poly.geojson");
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
MapView map = mMapFragment.getMap();
- Polygon polygon = map.addPolygon(new PolygonOptions()
- .add(latLngs)
- .strokeColor(Color.MAGENTA)
- .fillColor(Color.BLUE));
+ ArrayList<PolygonOptions> opts = new ArrayList<PolygonOptions>();
+ opts.add(new PolygonOptions()
+ .add(latLngs)
+ .strokeColor(Color.MAGENTA)
+ .fillColor(Color.BLUE));
+ Polygon polygon = map.addPolygons(opts).get(0);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {