[ios, android] Add methods to remove a custom layer

This commit is contained in:
John Firebaugh
2015-12-17 14:39:19 -08:00
parent d3c92a8b5c
commit 110f1ec796
11 changed files with 68 additions and 3 deletions

View File

@@ -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

View File

@@ -174,6 +174,7 @@ public:
CustomLayerDeinitializeFunction,
void* context,
const char* before = nullptr);
void removeCustomLayer(const std::string& id);
// Memory
void setSourceTileCacheSize(size_t);

View File

@@ -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)

View File

@@ -3792,6 +3792,11 @@ public final class MapView extends FrameLayout {
mNativeMapView.addCustomLayer(customLayer, before);
}
@UiThread
public void removeCustomLayer(String id) {
mNativeMapView.removeCustomLayer(id);
}
@UiThread
public void invalidateCustomLayers() {
mNativeMapView.update();

View File

@@ -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);
}

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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();