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

Commit

Permalink
[android] #352 - introducing FeatureWrapper, added PolygonFeature, de…
Browse files Browse the repository at this point in the history
…tecting polygons correctly
  • Loading branch information
tobrun committed Jul 26, 2016
1 parent b5aa1a6 commit 9da8d48
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mapbox.mapboxsdk.annotations;

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

public class FeatureWrapper {

private List<Feature> features;

public FeatureWrapper() {
features = new ArrayList<>();
}

public void setFeatures(List<Feature>features){
this.features = features;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Polygon is a geometry annotation that's a closed loop of coordinates.
*/
public final class Polygon extends PointCollectionShape {
public class Polygon extends PointCollectionShape {

private int fillColor = Color.BLACK; // default fillColor is black
private int strokeColor = Color.BLACK; // default strokeColor is black
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mapbox.mapboxsdk.annotations;

import java.util.Map;

public class PolygonFeature extends Polygon implements Feature {

private long featureId;
private Map<String, Object> attributes;

public PolygonFeature() {
}

@Override
public long getFeatureId() {
return featureId;
}

@Override
public Map<String, Object> getAttributes() {
return attributes;
}

@Override
public Object getAttribute(String key) {
return attributes.get(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Build;
import android.util.Log;
import android.view.Surface;

import com.mapbox.mapboxsdk.annotations.Feature;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonFeature;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;
Expand Down Expand Up @@ -473,7 +476,8 @@ public double[] getCameraValues() {
}

public List<Feature> getVisibleFeatures(float x, float y, List<String> layerIds) {
nativeGetVisibleFeatures(mNativeMapViewPtr, x, y, layerIds.toArray(new String[layerIds.size()]));
PolygonFeature polygonFeature = nativeGetVisibleFeatures(mNativeMapViewPtr, x, y, layerIds.toArray(new String[layerIds.size()]));
Log.v(MapboxConstants.TAG,polygonFeature.toString());
return new ArrayList<>();
}

Expand Down Expand Up @@ -653,5 +657,5 @@ private native void nativeSetVisibleCoordinateBounds(long mNativeMapViewPtr, Lat

private native double[] nativeGetCameraValues(long mNativeMapViewPtr);

private native LatLng nativeGetVisibleFeatures(long mNativeMapViewPtr, float x, float y, String[] layerIds);
private native PolygonFeature nativeGetVisibleFeatures(long mNativeMapViewPtr, float x, float y, String[] layerIds);
}
40 changes: 18 additions & 22 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ jni::jmethodID* offlineRegionStatusOnErrorId = nullptr;
jni::jmethodID* offlineRegionDeleteOnDeleteId = nullptr;
jni::jmethodID* offlineRegionDeleteOnErrorId = nullptr;

jni::jclass* polygonFeatureClass = nullptr;
jni::jmethodID* polygonFeatureConstructorId = nullptr;

// Offline declarations end

bool attach_jni_thread(JavaVM* vm, JNIEnv** env, std::string threadName) {
Expand Down Expand Up @@ -1097,34 +1100,23 @@ jni::jobject* nativeGetVisibleFeatures(JNIEnv *env, jni::jobject* obj, jlong na
std::vector<mbgl::Feature> features = nativeMapView->getMap().queryRenderedFeatures(mbgl::ScreenCoordinate(x, y), optionalLayerIDs);
mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetVisibleFeatures 2: "+std::to_string(features.size()));

jni::jobject* joutput;
//jni::jobject* jhashmap = &jni::NewObject(*env, *mapClass, *mapConstructorId);

mbgl::ToFeatureType toFeatureType;
for (const auto &feature : features) {
//for (auto &pair : feature.properties) {
// auto &value = pair.second;
// PropertyValueEvaluator evaluator;
// jni::CallMethod<void>(*env, hashMap, *put, std_string_to_jstring(env2, pair.first.c_str(),mbgl::Value::visit(value, evaluator)));
//}
if(typeid(feature.geometry)==typeid(mbgl::Point<double>)) {
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(typeid(feature.geometry)==typeid(mbgl::LineString<double>)) {
mbgl::Log::Debug(mbgl::Event::JNI, "It's a line");
}else if(typeid(feature.geometry)==typeid(mbgl::Polygon<double>)) {
}else if(featureType==mbgl::FeatureType::LineString) {
mbgl::Log::Debug(mbgl::Event::JNI, "It's a polyline");
}else if(featureType==mbgl::FeatureType::Polygon) {
joutput = &jni::NewObject(*env, *polygonFeatureClass, *polygonFeatureConstructorId);
mbgl::Log::Debug(mbgl::Event::JNI, "It's a polygon");
}else if(typeid(feature.geometry)==typeid(mbgl::MultiPoint<double>)) {
mbgl::Log::Debug(mbgl::Event::JNI, "It's a multipoint");
}else if(typeid(feature.geometry)==typeid(mbgl::MultiLineString<double>)) {
mbgl::Log::Debug(mbgl::Event::JNI, "It's a multilinestring");
}else if(typeid(feature.geometry)==typeid(mbgl::MultiPolygon<double>)) {
mbgl::Log::Debug(mbgl::Event::JNI, "It's a mulitpolygon");
}else if(typeid(feature.geometry)==typeid(mapbox::geometry::geometry_collection<double>)) {
mbgl::Log::Debug(mbgl::Event::JNI, "It's a geometrycollecition");
}else{
mbgl::Log::Debug(mbgl::Event::JNI, "It's a something else");
mbgl::Log::Debug(mbgl::Event::JNI, "Unsupported feature type found!");
}
}
mbgl::LatLng latLng = nativeMapView->getMap().latLngForPixel(mbgl::ScreenCoordinate(x, y));
return &jni::NewObject(*env, *latLngClass, *latLngConstructorId, latLng.latitude, latLng.longitude);
return joutput;
}

// Offline calls begin
Expand Down Expand Up @@ -1666,6 +1658,10 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
//mapConstructorId = &jni::GetMethodID(env, *mapClass, "<init>", "()V");
//putFunctionId = &jni::GetMethodID(env, *mapClass, "put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

polygonFeatureClass = &jni::FindClass(env, "com/mapbox/mapboxsdk/annotations/PolygonFeature");
polygonFeatureClass = jni::NewGlobalRef(env, polygonFeatureClass).release();
polygonFeatureConstructorId = &jni::GetMethodID(env, *polygonFeatureClass, "<init>", "()V");

jni::jclass& nativeMapViewClass = jni::FindClass(env, "com/mapbox/mapboxsdk/maps/NativeMapView");

onInvalidateId = &jni::GetMethodID(env, nativeMapViewClass, "onInvalidate", "()V");
Expand Down Expand Up @@ -1747,7 +1743,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
MAKE_NATIVE_METHOD(nativeAddCustomLayer, "(JLcom/mapbox/mapboxsdk/layers/CustomLayer;Ljava/lang/String;)V"),
MAKE_NATIVE_METHOD(nativeRemoveCustomLayer, "(JLjava/lang/String;)V"),
MAKE_NATIVE_METHOD(nativeSetContentPadding, "(JDDDD)V"),
MAKE_NATIVE_METHOD(nativeGetVisibleFeatures,"(JFF[Ljava/lang/String;)Lcom/mapbox/mapboxsdk/geometry/LatLng;")
MAKE_NATIVE_METHOD(nativeGetVisibleFeatures,"(JFF[Ljava/lang/String;)Lcom/mapbox/mapboxsdk/annotations/PolygonFeature;")
);

// Offline begin
Expand Down

0 comments on commit 9da8d48

Please sign in to comment.