From d3d8c056030d29bdf00b6d0deea40de82564f450 Mon Sep 17 00:00:00 2001 From: Tobrun Van Nuland Date: Mon, 25 Jul 2016 13:50:35 +0200 Subject: [PATCH] [android] #352 - update gyp with new C++ files, added feature concept for Android binding, removed Multipoint inheritance issue --- .../mapboxsdk/annotations/MultiPoint.java | 46 +++++++++++++++++- .../mapboxsdk/annotations/MultiPolygon.java | 9 +++- .../annotations/MultiPolygonFeature.java | 4 +- .../mapbox/mapboxsdk/annotations/Polygon.java | 35 ++++++++++++++ .../mapboxsdk/annotations/Polyline.java | 40 ++++++++++++++++ platform/android/platform.gyp | 3 ++ .../src/feature/android_conversion.hpp | 0 platform/android/src/feature/feature.cpp | 0 platform/android/src/feature/feature.hpp | 0 platform/android/src/jni.cpp | 48 +++++++++++++------ 10 files changed, 165 insertions(+), 20 deletions(-) create mode 100644 platform/android/src/feature/android_conversion.hpp create mode 100644 platform/android/src/feature/feature.cpp create mode 100644 platform/android/src/feature/feature.hpp diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java index 5621c336ba0..05328dc2e77 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java @@ -1,5 +1,49 @@ package com.mapbox.mapboxsdk.annotations; -public class MultiPoint extends PointCollectionShape { +import com.mapbox.mapboxsdk.geometry.LatLng; +import java.util.ArrayList; +import java.util.List; + +public class MultiPoint extends Shape { + + private List points; + private float alpha = 1.0f; + + protected MultiPoint() { + super(); + points = new ArrayList<>(); + } + + /** + * Returns a copy of the points. + * + * @return points - as a copy + */ + public List getPoints() { + return new ArrayList<>(points); + } + + /** + * Sets the points of this polyline. This method will take a copy + * of the points, so further mutations to points will have no effect + * on this polyline. + * + * @param points the points of the polyline + */ + void setPoints(List points) { + this.points = new ArrayList<>(points); + } + + void addPoint(LatLng point) { + points.add(point); + } + + public float getAlpha() { + return alpha; + } + + void setAlpha(float alpha) { + this.alpha = alpha; + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygon.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygon.java index 88dff9a0bef..3a65ea6d9ef 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygon.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygon.java @@ -1,13 +1,18 @@ package com.mapbox.mapboxsdk.annotations; +import java.util.ArrayList; import java.util.List; public class MultiPolygon extends Shape{ private List polygons; - public MultiPolygon(List polygons) { - this.polygons = polygons; + public MultiPolygon() { + polygons = new ArrayList<>(); + } + + public void addPolygon(Polygon polygon){ + polygons.add(polygon); } public List getPolygons() { diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygonFeature.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygonFeature.java index 21d3359f8e0..1bf4cfa30b7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygonFeature.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPolygonFeature.java @@ -1,14 +1,12 @@ package com.mapbox.mapboxsdk.annotations; -import java.util.List; import java.util.Map; public class MultiPolygonFeature extends MultiPolygon implements Feature{ private long featureId; private Map attributes; - public MultiPolygonFeature(List polygonList) { - super(polygonList); + public MultiPolygonFeature() { } @Override diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java index 709ed0fa8a0..4f53586fa28 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java @@ -12,11 +12,46 @@ */ public class Polygon extends PointCollectionShape { + private List points; + private float alpha = 1.0f; + private int fillColor = Color.BLACK; // default fillColor is black private int strokeColor = Color.BLACK; // default strokeColor is black public Polygon() { super(); + points = new ArrayList<>(); + } + /** + * Returns a copy of the points. + * + * @return points - as a copy + */ + public List getPoints() { + return new ArrayList<>(points); + } + + /** + * Sets the points of this polyline. This method will take a copy + * of the points, so further mutations to points will have no effect + * on this polyline. + * + * @param points the points of the polyline + */ + void setPoints(List points) { + this.points = new ArrayList<>(points); + } + + void addPoint(LatLng point) { + points.add(point); + } + + public float getAlpha() { + return alpha; + } + + void setAlpha(float alpha) { + this.alpha = alpha; } public int getFillColor() { diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java index 3d79b8457f1..bdfa9aeb322 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java @@ -2,6 +2,11 @@ import android.graphics.Color; +import com.mapbox.mapboxsdk.geometry.LatLng; + +import java.util.ArrayList; +import java.util.List; + /** * Polyline is a geometry feature with an unclosed list of coordinates drawn as a line */ @@ -9,9 +14,44 @@ public class Polyline extends PointCollectionShape { private int color = Color.BLACK; // default color is black private float width = 10; // As specified by Google API Docs (in pixels) + private List points; + private float alpha = 1.0f; public Polyline() { super(); + points = new ArrayList<>(); + } + + /** + * Returns a copy of the points. + * + * @return points - as a copy + */ + public List getPoints() { + return new ArrayList<>(points); + } + + /** + * Sets the points of this polyline. This method will take a copy + * of the points, so further mutations to points will have no effect + * on this polyline. + * + * @param points the points of the polyline + */ + void setPoints(List points) { + this.points = new ArrayList<>(points); + } + + void addPoint(LatLng point) { + points.add(point); + } + + public float getAlpha() { + return alpha; + } + + void setAlpha(float alpha) { + this.alpha = alpha; } public int getColor() { diff --git a/platform/android/platform.gyp b/platform/android/platform.gyp index 171fc0b51a1..f0307923804 100644 --- a/platform/android/platform.gyp +++ b/platform/android/platform.gyp @@ -27,10 +27,13 @@ 'sources': [ 'src/native_map_view.cpp', 'src/jni.cpp', + 'src/java_types.cpp', 'src/attach_env.cpp', 'src/log_android.cpp', 'src/http_file_source.cpp', 'src/asset_file_source.cpp', + 'src/feature/value.cpp', + 'src/feature/feature.cpp', 'src/thread.cpp', '../default/string_stdlib.cpp', '../default/image.cpp', diff --git a/platform/android/src/feature/android_conversion.hpp b/platform/android/src/feature/android_conversion.hpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/platform/android/src/feature/feature.cpp b/platform/android/src/feature/feature.cpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/platform/android/src/feature/feature.hpp b/platform/android/src/feature/feature.hpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 9c270af45ec..5d291c5be92 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -163,8 +163,8 @@ jni::jmethodID* offlineRegionDeleteOnErrorId = nullptr; jni::jclass* polygonFeatureClass = nullptr; jni::jmethodID* polygonFeatureConstructorId = nullptr; -//jni::jclass* multiPolygonFeatureClass = nullptr; -//jni::jmethodID* multiPolygonFeatureConstructorId = nullptr; +jni::jclass* multiPolygonFeatureClass = nullptr; +jni::jmethodID* multiPolygonFeatureConstructorId = nullptr; jni::jclass* polylineFeatureClass = nullptr; jni::jmethodID* polylineFeatureConstructorId = nullptr; @@ -1119,24 +1119,44 @@ jni::jobject* nativeGetVisibleFeatures(JNIEnv *env, jni::jobject* obj, jlong na assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); mbgl::optional> optionalLayerIDs; + + // call core for features at point std::vector features = nativeMapView->getMap().queryRenderedFeatures(mbgl::ScreenCoordinate(x, y), optionalLayerIDs); - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetVisibleFeatures 2: "+std::to_string(features.size())); + mbgl::Log::Debug(mbgl::Event::JNI, "Amount of features found: "+std::to_string(features.size())); - // create FeatureWrapper return type + // create wrapping java object to put features in jni::jobject* joutput = &jni::NewObject(*env, *featureWrapperClass, *featureWrapperConstructorId); - //jni::jobject* jhashmap = &jni::NewObject(*env, *mapClass, *mapConstructorId); mbgl::ToFeatureType toFeatureType; for (const auto &feature : features) { const mbgl::FeatureType featureType = apply_visitor(toFeatureType, feature.geometry); - if(featureType==mbgl::FeatureType::Point) { - mbgl::Log::Debug(mbgl::Event::JNI, "It's a point"); - }else if(featureType==mbgl::FeatureType::LineString) { + if(featureType == mbgl::FeatureType::Point) { + mbgl::Log::Debug(mbgl::Event::JNI, "It's a point"); + + // create point + jni::jobject* point = &jni::NewObject(*env, *pointFeatureClass, *pointFeatureConstructorId); + + // add point to wrapper + jni::CallMethod(*env, joutput, *featureWrapperAddId, point); + + }else if(featureType == mbgl::FeatureType::LineString) { mbgl::Log::Debug(mbgl::Event::JNI, "It's a polyline"); - jni::CallMethod(*env, joutput, *featureWrapperAddId,&jni::NewObject(*env, *polylineFeatureClass, *polylineFeatureConstructorId)); - }else if(featureType==mbgl::FeatureType::Polygon) { - jni::CallMethod(*env, joutput, *featureWrapperAddId,&jni::NewObject(*env, *polygonFeatureClass, *polygonFeatureConstructorId)); + + // create polyline + jni::jobject* polyline = &jni::NewObject(*env, *polylineFeatureClass, *polylineFeatureConstructorId); + + // add polyline to wrapper + jni::CallMethod(*env, joutput, *featureWrapperAddId, polyline); + + }else if(featureType == mbgl::FeatureType::Polygon) { mbgl::Log::Debug(mbgl::Event::JNI, "It's a polygon"); + + // create polyline + jni::jobject* polygon = &jni::NewObject(*env, *polygonFeatureClass, *polygonFeatureConstructorId); + + // add polygon to wrapper + jni::CallMethod(*env, joutput, *featureWrapperAddId,polygon); + }else{ mbgl::Log::Debug(mbgl::Event::JNI, "Unsupported feature type found!"); } @@ -1687,9 +1707,9 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { polygonFeatureClass = jni::NewGlobalRef(env, polygonFeatureClass).release(); polygonFeatureConstructorId = &jni::GetMethodID(env, *polygonFeatureClass, "", "()V"); - //multiPolygonFeatureClass = &jni::FindClass(env, "com/mapbox/mapboxsdk/annotations/MultiPolygonFeature"); - //multiPolygonFeatureClass = jni::NewGlobalRef(env, multiPolygonFeatureClass).release(); - //multiPolygonFeatureConstructorId = &jni::GetMethodID(env, *multiPolygonFeatureClass, "", "()V"); + multiPolygonFeatureClass = &jni::FindClass(env, "com/mapbox/mapboxsdk/annotations/MultiPolygonFeature"); + multiPolygonFeatureClass = jni::NewGlobalRef(env, multiPolygonFeatureClass).release(); + multiPolygonFeatureConstructorId = &jni::GetMethodID(env, *multiPolygonFeatureClass, "", "()V"); polylineFeatureClass = &jni::FindClass(env, "com/mapbox/mapboxsdk/annotations/PolylineFeature"); polylineFeatureClass = jni::NewGlobalRef(env, polylineFeatureClass).release();