summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2015-12-17 14:39:19 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2015-12-17 15:15:55 -0800
commit110f1ec796ab46e4f16bcf9673f0aa89ae153aeb (patch)
treebee9af94074a24ec4b3ae757047747b5da1625bf
parentd3c92a8b5cf9a5ac89320c66cf58ff9f84ab28ee (diff)
downloadqtlocation-mapboxgl-110f1ec796ab46e4f16bcf9673f0aa89ae153aeb.tar.gz
[ios, android] Add methods to remove a custom layer
-rw-r--r--include/mbgl/ios/MGLMapView+MGLCustomStyleLayerAdditions.h2
-rw-r--r--include/mbgl/map/map.hpp1
-rw-r--r--ios/app/MBXViewController.mm20
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java5
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/NativeMapView.java6
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MainActivity.java13
-rw-r--r--platform/android/src/jni.cpp8
-rw-r--r--platform/ios/src/MGLMapView.mm5
-rw-r--r--src/mbgl/map/map.cpp4
-rw-r--r--src/mbgl/map/map_context.cpp6
-rw-r--r--src/mbgl/map/map_context.hpp1
11 files changed, 68 insertions, 3 deletions
diff --git a/include/mbgl/ios/MGLMapView+MGLCustomStyleLayerAdditions.h b/include/mbgl/ios/MGLMapView+MGLCustomStyleLayerAdditions.h
index 2fe7ea6be5..e24f7ebab6 100644
--- a/include/mbgl/ios/MGLMapView+MGLCustomStyleLayerAdditions.h
+++ b/include/mbgl/ios/MGLMapView+MGLCustomStyleLayerAdditions.h
@@ -20,6 +20,8 @@ typedef void (^MGLCustomStyleLayerCompletionHandler)(void);
- (void)insertCustomStyleLayerWithIdentifier:(NSString *)identifier preparationHandler:(MGLCustomStyleLayerPreparationHandler)preparation drawingHandler:(MGLCustomStyleLayerDrawingHandler)drawing completionHandler:(MGLCustomStyleLayerCompletionHandler)completion belowStyleLayerWithIdentifier:(nullable NSString *)otherIdentifier;
+- (void)removeCustomStyleLayerWithIdentifier:(NSString *)identifier;
+
- (void)setCustomStyleLayersNeedDisplay;
@end
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp
index f8c3f4a172..2be31b5db6 100644
--- a/include/mbgl/map/map.hpp
+++ b/include/mbgl/map/map.hpp
@@ -174,6 +174,7 @@ public:
CustomLayerDeinitializeFunction,
void* context,
const char* before = nullptr);
+ void removeCustomLayer(const std::string& id);
// Memory
void setSourceTileCacheSize(size_t);
diff --git a/ios/app/MBXViewController.mm b/ios/app/MBXViewController.mm
index 31b62ca6d0..21a6a2aeac 100644
--- a/ios/app/MBXViewController.mm
+++ b/ios/app/MBXViewController.mm
@@ -25,6 +25,7 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = {
@implementation MBXViewController
{
BOOL _isTouringWorld;
+ BOOL _isShowingCustomStyleLayer;
}
#pragma mark - Setup
@@ -151,7 +152,7 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = {
@"Add Test Shapes",
@"Start World Tour",
@"Remove Annotations",
- @"Insert Custom Style Layer",
+ @"Toggle Custom Style Layer",
nil];
[sheet showFromBarButtonItem:self.navigationItem.leftBarButtonItem animated:YES];
@@ -264,7 +265,14 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = {
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 10)
{
- [self insertCustomStyleLayer];
+ if (_isShowingCustomStyleLayer)
+ {
+ [self removeCustomStyleLayer];
+ }
+ else
+ {
+ [self insertCustomStyleLayer];
+ }
}
}
@@ -310,6 +318,8 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = {
- (void)insertCustomStyleLayer
{
+ _isShowingCustomStyleLayer = YES;
+
static const GLchar *vertexShaderSource = "attribute vec2 a_pos; void main() { gl_Position = vec4(a_pos, 0, 1); }";
static const GLchar *fragmentShaderSource = "void main() { gl_FragColor = vec4(0, 1, 0, 1); }";
@@ -361,6 +371,12 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = {
} belowStyleLayerWithIdentifier:@"housenum-label"];
}
+- (void)removeCustomStyleLayer
+{
+ _isShowingCustomStyleLayer = NO;
+ [self.mapView removeCustomStyleLayerWithIdentifier:@"mbx-custom"];
+}
+
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan)
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java
index e36a08aed3..670e5d473a 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java
@@ -3793,6 +3793,11 @@ public final class MapView extends FrameLayout {
}
@UiThread
+ public void removeCustomLayer(String id) {
+ mNativeMapView.removeCustomLayer(id);
+ }
+
+ @UiThread
public void invalidateCustomLayers() {
mNativeMapView.update();
}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/NativeMapView.java
index c142670775..dd6b9cf7eb 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/NativeMapView.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/NativeMapView.java
@@ -454,6 +454,10 @@ final class NativeMapView {
nativeAddCustomLayer(mNativeMapViewPtr, customLayer, before);
}
+ public void removeCustomLayer(String id) {
+ nativeRemoveCustomLayer(mNativeMapViewPtr, id);
+ }
+
//
// Callbacks
//
@@ -636,4 +640,6 @@ final class NativeMapView {
private native double nativeGetTopOffsetPixelsForAnnotationSymbol(long nativeMapViewPtr, String symbolName);
private native void nativeAddCustomLayer(long nativeMapViewPtr, CustomLayer customLayer, String before);
+
+ private native void nativeRemoveCustomLayer(long nativeMapViewPtr, String id);
}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MainActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MainActivity.java
index c6ec8cbd77..6bfaf8fa60 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MainActivity.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MainActivity.java
@@ -76,6 +76,7 @@ public class MainActivity extends AppCompatActivity {
private int mSelectedStyle = R.id.actionStyleMapboxStreets;
private NavigationView mNavigationView;
private CoordinatorLayout mCoordinatorLayout;
+ private boolean mIsShowingCustomLayer;
// Used for GPS
private FloatingActionButton mLocationFAB;
@@ -397,7 +398,11 @@ public class MainActivity extends AppCompatActivity {
return true;
case R.id.action_custom_layer:
- addCustomLayer();
+ if (mIsShowingCustomLayer) {
+ removeCustomLayer();
+ } else {
+ addCustomLayer();
+ }
return true;
default:
@@ -576,6 +581,7 @@ public class MainActivity extends AppCompatActivity {
}
private void addCustomLayer() {
+ mIsShowingCustomLayer = true;
mMapView.addCustomLayer(
new CustomLayer("custom",
ExampleCustomLayer.createContext(),
@@ -585,6 +591,11 @@ public class MainActivity extends AppCompatActivity {
null);
}
+ private void removeCustomLayer() {
+ mIsShowingCustomLayer = false;
+ mMapView.removeCustomLayer("custom");
+ }
+
// Called when FPS changes
private class MyOnFpsChangedListener implements MapView.OnFpsChangedListener {
diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp
index 8555fbf499..f99b733598 100644
--- a/platform/android/src/jni.cpp
+++ b/platform/android/src/jni.cpp
@@ -1492,6 +1492,12 @@ void JNICALL nativeAddCustomLayer(JNIEnv *env, jobject obj, jlong nativeMapViewP
before ? std_string_from_jstring(env, before).c_str() : nullptr);
}
+void JNICALL nativeRemoveCustomLayer(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jstring id) {
+ mbgl::Log::Debug(mbgl::Event::JNI, "nativeRemoveCustomLayer");
+ assert(nativeMapViewPtr != 0);
+ NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
+ nativeMapView->getMap().removeCustomLayer(std_string_from_jstring(env, id));
+}
}
@@ -2018,6 +2024,8 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
reinterpret_cast<void *>(&nativeGetTopOffsetPixelsForAnnotationSymbol)},
{"nativeAddCustomLayer", "(JLcom/mapbox/mapboxsdk/layers/CustomLayer;Ljava/lang/String;)V",
reinterpret_cast<void *>(&nativeAddCustomLayer)},
+ {"nativeRemoveCustomLayer", "(JLjava/lang/String;)V",
+ reinterpret_cast<void *>(&nativeRemoveCustomLayer)},
};
if (env->RegisterNatives(nativeMapViewClass, methods.data(), methods.size()) < 0) {
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm
index e506ce16fa..98df29fdd4 100644
--- a/platform/ios/src/MGLMapView.mm
+++ b/platform/ios/src/MGLMapView.mm
@@ -3675,6 +3675,11 @@ void MGLFinishCustomStyleLayer(void *context)
context, otherIdentifier.UTF8String);
}
+- (void)removeCustomStyleLayerWithIdentifier:(NSString *)identifier
+{
+ _mbglMap->removeCustomLayer(identifier.UTF8String);
+}
+
- (void)setCustomStyleLayersNeedDisplay
{
_mbglMap->update(mbgl::Update::Repaint);
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index 5e22e8aeef..ff4b2f5c06 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -449,6 +449,10 @@ void Map::addCustomLayer(const std::string& id,
before ? std::string(before) : mapbox::util::optional<std::string>());
}
+void Map::removeCustomLayer(const std::string& id) {
+ context->invoke(&MapContext::removeLayer, id);
+}
+
#pragma mark - Toggles
void Map::setDebug(MapDebugOptions mode) {
diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp
index 96f4a68d9b..75ca188fbb 100644
--- a/src/mbgl/map/map_context.cpp
+++ b/src/mbgl/map/map_context.cpp
@@ -289,6 +289,12 @@ void MapContext::addLayer(std::unique_ptr<StyleLayer> layer, mapbox::util::optio
asyncUpdate.send();
}
+void MapContext::removeLayer(const std::string& id) {
+ style->removeLayer(id);
+ updateFlags |= Update::Classes;
+ asyncUpdate.send();
+}
+
void MapContext::setSourceTileCacheSize(size_t size) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (size != sourceCacheSize) {
diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp
index 554765eac6..24459f270a 100644
--- a/src/mbgl/map/map_context.hpp
+++ b/src/mbgl/map/map_context.hpp
@@ -59,6 +59,7 @@ public:
// Style API
void addLayer(std::unique_ptr<StyleLayer>,
const mapbox::util::optional<std::string> before);
+ void removeLayer(const std::string& id);
void setSourceTileCacheSize(size_t size);
void onLowMemory();