From 0fba70d5a8465499b0dce900e5aa74f7189e4594 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 31 May 2016 17:34:11 -0700 Subject: [all] Rationalize annotation API --- platform/android/src/jni.cpp | 79 +++++++++++++--------------- platform/darwin/src/MGLGeometry_Private.h | 5 ++ platform/darwin/src/MGLMultiPoint_Private.h | 4 +- platform/darwin/src/MGLPolygon.mm | 14 ++--- platform/darwin/src/MGLPolyline.mm | 14 ++--- platform/default/glfw_view.cpp | 40 +++------------ platform/ios/src/MGLMapView.mm | 75 +++++++-------------------- platform/osx/src/MGLMapView.mm | 80 +++++++++-------------------- platform/qt/include/qmapbox.hpp | 2 - platform/qt/include/qmapboxgl.hpp | 4 -- platform/qt/src/qmapboxgl.cpp | 63 +++-------------------- 11 files changed, 117 insertions(+), 263 deletions(-) (limited to 'platform') diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 4c3f77e9da..ed2563cf7b 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -13,8 +13,7 @@ #include #include -#include -#include +#include #include #include #include @@ -681,8 +680,10 @@ void nativeUpdateMarker(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble latitude = jni::GetField(*env, position, *latLngLatitudeId); jdouble longitude = jni::GetField(*env, position, *latLngLongitudeId); - // Because Java only has int, not unsigned int, we need to bump the annotation id up to a long. - nativeMapView->getMap().updatePointAnnotation(markerId, mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), iconId)); + nativeMapView->getMap().updateAnnotation(markerId, mbgl::SymbolAnnotation { + mbgl::Point(longitude, latitude), + iconId + }); } jni::jarray* nativeAddMarkers(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jarray* jarray) { @@ -693,30 +694,30 @@ jni::jarray* nativeAddMarkers(JNIEnv *env, jni::jobject* obj, jlong nativ NullCheck(*env, jarray); std::size_t len = jni::GetArrayLength(*env, *jarray); - std::vector markers; - markers.reserve(len); + std::vector ids; + ids.reserve(len); for (std::size_t i = 0; i < len; i++) { jni::jobject* marker = jni::GetObjectArrayElement(*env, *jarray, i); jni::jobject* position = jni::GetField(*env, marker, *markerPositionId); jni::jobject* icon = jni::GetField(*env, marker, *markerIconId); - jni::DeleteLocalRef(*env, marker); - jni::jstring* jid = reinterpret_cast(jni::GetField(*env, icon, *iconIdId)); - jni::DeleteLocalRef(*env, icon); - - std::string id = std_string_from_jstring(env, jid); - jni::DeleteLocalRef(*env, jid); jdouble latitude = jni::GetField(*env, position, *latLngLatitudeId); jdouble longitude = jni::GetField(*env, position, *latLngLongitudeId); - jni::DeleteLocalRef(*env, position); - markers.emplace_back(mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), id)); + ids.push_back(nativeMapView->getMap().addAnnotation(mbgl::SymbolAnnotation { + mbgl::Point(longitude, latitude), + std_string_from_jstring(env, jid) + })); + + jni::DeleteLocalRef(*env, position); + jni::DeleteLocalRef(*env, jid); + jni::DeleteLocalRef(*env, icon); + jni::DeleteLocalRef(*env, marker); } - std::vector pointAnnotationIDs = nativeMapView->getMap().addPointAnnotations(markers); - return std_vector_uint_to_jobject(env, pointAnnotationIDs); + return std_vector_uint_to_jobject(env, ids); } static mbgl::Color toColor(jint color) { @@ -762,24 +763,23 @@ jni::jarray* nativeAddPolylines(JNIEnv *env, jni::jobject* obj, jlong nat NullCheck(*env, jarray); std::size_t len = jni::GetArrayLength(*env, *jarray); - std::vector shapes; - shapes.reserve(len); + std::vector ids; + ids.reserve(len); for (std::size_t i = 0; i < len; i++) { jni::jobject* polyline = jni::GetObjectArrayElement(*env, *jarray, i); - - mbgl::LineAnnotationProperties lineProperties; - lineProperties.opacity = jni::GetField(*env, polyline, *polylineAlphaId); - lineProperties.color = toColor(jni::GetField(*env, polyline, *polylineColorId)); - lineProperties.width = jni::GetField(*env, polyline, *polylineWidthId); - jni::jobject* points = jni::GetField(*env, polyline, *polylinePointsId); - shapes.emplace_back(toGeometry>(env, points), lineProperties); + + mbgl::LineAnnotation annotation { toGeometry>(env, points) }; + annotation.opacity = jni::GetField(*env, polyline, *polylineAlphaId); + annotation.color = toColor(jni::GetField(*env, polyline, *polylineColorId)); + annotation.width = jni::GetField(*env, polyline, *polylineWidthId); + ids.push_back(nativeMapView->getMap().addAnnotation(annotation)); jni::DeleteLocalRef(*env, polyline); } - return std_vector_uint_to_jobject(env, nativeMapView->getMap().addShapeAnnotations(shapes)); + return std_vector_uint_to_jobject(env, ids); } jni::jarray* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jarray* jarray) { @@ -790,24 +790,23 @@ jni::jarray* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nati NullCheck(*env, jarray); std::size_t len = jni::GetArrayLength(*env, *jarray); - std::vector shapes; - shapes.reserve(len); + std::vector ids; + ids.reserve(len); for (std::size_t i = 0; i < len; i++) { jni::jobject* polygon = jni::GetObjectArrayElement(*env, *jarray, i); - - mbgl::FillAnnotationProperties fillProperties; - fillProperties.opacity = jni::GetField(*env, polygon, *polygonAlphaId); - fillProperties.outlineColor = toColor(jni::GetField(*env, polygon, *polygonStrokeColorId)); - fillProperties.color = toColor(jni::GetField(*env, polygon, *polygonFillColorId)); - jni::jobject* points = jni::GetField(*env, polygon, *polygonPointsId); - shapes.emplace_back(mbgl::Polygon { toGeometry>(env, points) }, fillProperties); + + mbgl::FillAnnotation annotation { mbgl::Polygon { toGeometry>(env, points) } }; + annotation.opacity = jni::GetField(*env, polygon, *polygonAlphaId); + annotation.outlineColor = toColor(jni::GetField(*env, polygon, *polygonStrokeColorId)); + annotation.color = toColor(jni::GetField(*env, polygon, *polygonFillColorId)); + ids.push_back(nativeMapView->getMap().addAnnotation(annotation)); jni::DeleteLocalRef(*env, polygon); } - return std_vector_uint_to_jobject(env, nativeMapView->getMap().addShapeAnnotations(shapes)); + return std_vector_uint_to_jobject(env, ids); } void nativeRemoveAnnotations(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jarray* jarray) { @@ -817,20 +816,14 @@ void nativeRemoveAnnotations(JNIEnv *env, jni::jobject* obj, jlong nativeMapView NullCheck(*env, jarray); std::size_t len = jni::GetArrayLength(*env, *jarray); - - std::vector ids; - ids.reserve(len); - auto elements = jni::GetArrayElements(*env, *jarray); jlong* jids = std::get<0>(elements).get(); for (std::size_t i = 0; i < len; i++) { if(jids[i] == -1L) continue; - ids.push_back(static_cast(jids[i])); + nativeMapView->getMap().removeAnnotation(jids[i]); } - - nativeMapView->getMap().removeAnnotations(ids); } jni::jarray* nativeGetAnnotationsInBounds(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jobject* latLngBounds_) { diff --git a/platform/darwin/src/MGLGeometry_Private.h b/platform/darwin/src/MGLGeometry_Private.h index 0538cd94ea..fc57460128 100644 --- a/platform/darwin/src/MGLGeometry_Private.h +++ b/platform/darwin/src/MGLGeometry_Private.h @@ -6,6 +6,7 @@ #endif #import +#import /// Returns the smallest rectangle that contains both the given rectangle and /// the given point. @@ -15,6 +16,10 @@ NS_INLINE mbgl::LatLng MGLLatLngFromLocationCoordinate2D(CLLocationCoordinate2D return mbgl::LatLng(coordinate.latitude, coordinate.longitude); } +NS_INLINE mbgl::Point MGLPointFromLocationCoordinate2D(CLLocationCoordinate2D coordinate) { + return mbgl::Point(coordinate.longitude, coordinate.latitude); +} + NS_INLINE CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) { return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude); } diff --git a/platform/darwin/src/MGLMultiPoint_Private.h b/platform/darwin/src/MGLMultiPoint_Private.h index f7a7bd4ffd..aa52a02fcb 100644 --- a/platform/darwin/src/MGLMultiPoint_Private.h +++ b/platform/darwin/src/MGLMultiPoint_Private.h @@ -3,7 +3,7 @@ #import "MGLGeometry.h" #import "MGLTypes.h" -#import +#import #import #import @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds; /** Constructs a shape annotation object, asking the delegate for style values. */ -- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id )delegate; +- (mbgl::Annotation)annotationObjectWithDelegate:(id )delegate; @end diff --git a/platform/darwin/src/MGLPolygon.mm b/platform/darwin/src/MGLPolygon.mm index 47be070246..c009d9e3d6 100644 --- a/platform/darwin/src/MGLPolygon.mm +++ b/platform/darwin/src/MGLPolygon.mm @@ -36,19 +36,19 @@ return result; } -- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id )delegate { - mbgl::FillAnnotationProperties fillProperties; - fillProperties.opacity = [delegate alphaForShapeAnnotation:self]; - fillProperties.outlineColor = [delegate strokeColorForShapeAnnotation:self]; - fillProperties.color = [delegate fillColorForPolygonAnnotation:self]; - +- (mbgl::Annotation)annotationObjectWithDelegate:(id )delegate { mbgl::Polygon geometry; geometry.push_back(self.ring); for (MGLPolygon *polygon in self.interiorPolygons) { geometry.push_back(polygon.ring); } - return mbgl::ShapeAnnotation(geometry, fillProperties); + mbgl::FillAnnotation annotation { geometry }; + annotation.opacity = [delegate alphaForShapeAnnotation:self]; + annotation.outlineColor = [delegate strokeColorForShapeAnnotation:self]; + annotation.color = [delegate fillColorForPolygonAnnotation:self]; + + return annotation; } @end diff --git a/platform/darwin/src/MGLPolyline.mm b/platform/darwin/src/MGLPolyline.mm index ebba5c862e..15ea5a0952 100644 --- a/platform/darwin/src/MGLPolyline.mm +++ b/platform/darwin/src/MGLPolyline.mm @@ -13,12 +13,7 @@ return [[self alloc] initWithCoordinates:coords count:count]; } -- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id )delegate { - mbgl::LineAnnotationProperties lineProperties; - lineProperties.opacity = [delegate alphaForShapeAnnotation:self]; - lineProperties.color = [delegate strokeColorForShapeAnnotation:self]; - lineProperties.width = [delegate lineWidthForPolylineAnnotation:self]; - +- (mbgl::Annotation)annotationObjectWithDelegate:(id )delegate { NSUInteger count = self.pointCount; CLLocationCoordinate2D *coordinates = self.coordinates; @@ -28,7 +23,12 @@ geometry.push_back(mbgl::Point(coordinates[i].longitude, coordinates[i].latitude)); } - return mbgl::ShapeAnnotation(geometry, lineProperties); + mbgl::LineAnnotation annotation { geometry }; + annotation.opacity = [delegate alphaForShapeAnnotation:self]; + annotation.color = [delegate strokeColorForShapeAnnotation:self]; + annotation.width = [delegate lineWidthForPolylineAnnotation:self]; + + return annotation; } @end diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 4a04595c33..2280c3f2ae 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -1,6 +1,5 @@ #include -#include -#include +#include #include #include #include @@ -208,14 +207,10 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, } } -mbgl::LatLng GLFWView::makeRandomLatLng() const { +mbgl::Point GLFWView::makeRandomPoint() const { const double x = width * double(std::rand()) / RAND_MAX; const double y = height * double(std::rand()) / RAND_MAX; - return map->latLngForPixel({ x, y }); -} - -mbgl::Point GLFWView::makeRandomPoint() const { - mbgl::LatLng latLng = makeRandomLatLng(); + mbgl::LatLng latLng = map->latLngForPixel({ x, y }); return { latLng.longitude, latLng.latitude }; } @@ -259,53 +254,34 @@ void GLFWView::nextOrientation() { } void GLFWView::addRandomCustomPointAnnotations(int count) { - std::vector points; - for (int i = 0; i < count; i++) { static int spriteID = 1; const auto name = std::string{ "marker-" } + mbgl::util::toString(spriteID++); map->addAnnotationIcon(name, makeSpriteImage(22, 22, 1)); spriteIDs.push_back(name); - points.emplace_back(makeRandomLatLng(), name); + annotationIDs.push_back(map->addAnnotation(mbgl::SymbolAnnotation { makeRandomPoint(), name })); } - - auto newIDs = map->addPointAnnotations(points); - annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); } void GLFWView::addRandomPointAnnotations(int count) { - std::vector points; - for (int i = 0; i < count; i++) { - points.emplace_back(makeRandomLatLng(), "default_marker"); + annotationIDs.push_back(map->addAnnotation(mbgl::SymbolAnnotation { makeRandomPoint(), "default_marker" })); } - - auto newIDs = map->addPointAnnotations(points); - annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); } void GLFWView::addRandomShapeAnnotations(int count) { - std::vector shapes; - - mbgl::FillAnnotationProperties properties; - properties.opacity = .1; - for (int i = 0; i < count; i++) { mbgl::Polygon triangle; triangle.push_back({ makeRandomPoint(), makeRandomPoint(), makeRandomPoint() }); - shapes.emplace_back(triangle, properties); + annotationIDs.push_back(map->addAnnotation(mbgl::FillAnnotation { triangle, .1 })); } - - auto newIDs = map->addShapeAnnotations(shapes); - annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); } void GLFWView::clearAnnotations() { - if (annotationIDs.empty()) { - return; + for (const auto& id : annotationIDs) { + map->removeAnnotation(id); } - map->removeAnnotations(annotationIDs); annotationIDs.clear(); } diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index eacd2f7b74..87d821fba9 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -7,8 +7,7 @@ #import #include -#include -#include +#include #include #include #include @@ -1776,9 +1775,9 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) // but safely updated. if (annotation == [self annotationWithTag:annotationTag]) { - const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate); + const mbgl::Point point = MGLPointFromLocationCoordinate2D(annotation.coordinate); MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; - _mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" }); + _mbglMap->updateAnnotation(annotationTag, mbgl::SymbolAnnotation { point, annotationImage.styleIconIdentifier.UTF8String ?: "" }); if (annotationTag == _selectedAnnotationTag) { [self deselectAnnotation:annotation animated:YES]; @@ -2796,11 +2795,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) if ( ! annotations) return; [self willChangeValueForKey:@"annotations"]; - NSMutableArray *userPoints = [NSMutableArray array]; - std::vector points; - NSMutableArray *userShapes = [NSMutableArray array]; - std::vector shapes; - NSMutableDictionary *annotationImagesForAnnotation = [NSMutableDictionary dictionary]; NSMutableDictionary *annotationViewsForAnnotation = [NSMutableDictionary dictionary]; @@ -2820,8 +2814,11 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) if (!multiPoint.pointCount) { continue; } - shapes.emplace_back([multiPoint shapeAnnotationObjectWithDelegate:self]); - [userShapes addObject:annotation]; + + MGLAnnotationTag annotationTag = _mbglMap->addAnnotation([multiPoint annotationObjectWithDelegate:self]); + MGLAnnotationContext context; + context.annotation = annotation; + _annotationContextsByAnnotationTag[annotationTag] = context; } else if ([annotation isKindOfClass:[MGLMultiPolyline class]] || [annotation isKindOfClass:[MGLMultiPolygon class]] @@ -2877,22 +2874,11 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) annotationImagesForAnnotation[annotationValue] = annotationImage; } - [userPoints addObject:annotation]; - points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), symbolName.UTF8String ?: ""); - } - } + MGLAnnotationTag annotationTag = _mbglMap->addAnnotation(mbgl::SymbolAnnotation { + MGLPointFromLocationCoordinate2D(annotation.coordinate), + symbolName.UTF8String ?: "" + }); - if (points.size()) - { - // refactor this to build contexts above and just associate with tags here - - std::vector annotationTags = _mbglMap->addPointAnnotations(points); - - for (size_t i = 0; i < annotationTags.size(); ++i) - { - id annotation = userPoints[i]; - NSValue *annotationValue = [NSValue valueWithNonretainedObject:annotation]; - MGLAnnotationContext context; context.annotation = annotation; MGLAnnotationImage *annotationImage = annotationImagesForAnnotation[annotationValue]; @@ -2900,13 +2886,11 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) if (annotationImage) { context.imageReuseIdentifier = annotationImage.reuseIdentifier; } - MGLAnnotationView *annotationView = annotationViewsForAnnotation[annotationValue]; if (annotationView) { context.annotationView = annotationView; context.viewReuseIdentifier = annotationView.reuseIdentifier; } - MGLAnnotationTag annotationTag = annotationTags[i]; _annotationContextsByAnnotationTag[annotationTag] = context; if ([annotation isKindOfClass:[NSObject class]]) { NSAssert(![annotation isKindOfClass:[MGLMultiPoint class]], @"Point annotation should not be MGLMultiPoint."); @@ -2915,21 +2899,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) } } - if (shapes.size()) - { - std::vector annotationTags = _mbglMap->addShapeAnnotations(shapes); - - for (size_t i = 0; i < annotationTags.size(); ++i) - { - MGLAnnotationTag annotationTag = annotationTags[i]; - id annotation = userShapes[i]; - - MGLAnnotationContext context; - context.annotation = annotation; - _annotationContextsByAnnotationTag[annotationTag] = context; - } - } - [self updateAnnotationContainerViewWithAnnotationViews:newAnnotationViews]; [self didChangeValueForKey:@"annotations"]; @@ -3072,8 +3041,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { if ( ! annotations) return; - std::vector annotationTagsToRemove; - annotationTagsToRemove.reserve(annotations.count); + [self willChangeValueForKey:@"annotations"]; for (id annotation in annotations) { @@ -3088,8 +3056,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); MGLAnnotationView *annotationView = annotationContext.annotationView; [annotationView removeFromSuperview]; - - annotationTagsToRemove.push_back(annotationTag); if (annotationTag == _selectedAnnotationTag) { @@ -3102,15 +3068,12 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { [(NSObject *)annotation removeObserver:self forKeyPath:@"coordinate" context:(void *)(NSUInteger)annotationTag]; } - } - if ( ! annotationTagsToRemove.empty()) - { - [self willChangeValueForKey:@"annotations"]; - _mbglMap->removeAnnotations(annotationTagsToRemove); - [self didChangeValueForKey:@"annotations"]; - UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil); + _mbglMap->removeAnnotation(annotationTag); } + + [self didChangeValueForKey:@"annotations"]; + UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil); } - (void)addOverlay:(id )overlay @@ -3675,8 +3638,8 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { if ([pair.second.imageReuseIdentifier isEqualToString:reuseIdentifier]) { - const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(pair.second.annotation.coordinate); - _mbglMap->updatePointAnnotation(pair.first, { latLng, iconIdentifier.UTF8String ?: "" }); + const mbgl::Point point = MGLPointFromLocationCoordinate2D(pair.second.annotation.coordinate); + _mbglMap->updateAnnotation(pair.first, mbgl::SymbolAnnotation { point, iconIdentifier.UTF8String ?: "" }); } } } diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm index 019f497649..03b94c25ba 100644 --- a/platform/osx/src/MGLMapView.mm +++ b/platform/osx/src/MGLMapView.mm @@ -18,7 +18,7 @@ #import "MGLMapViewDelegate.h" #import -#import +#import #import #import #import @@ -481,9 +481,9 @@ public: // match a valid annotation tag, the annotation will be unnecessarily // but safely updated. if (annotation == [self annotationWithTag:annotationTag]) { - const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate); + const mbgl::Point point = MGLPointFromLocationCoordinate2D(annotation.coordinate); MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; - _mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" }); + _mbglMap->updateAnnotation(annotationTag, mbgl::SymbolAnnotation { point, annotationImage.styleIconIdentifier.UTF8String ?: "" }); if (annotationTag == _selectedAnnotationTag) { [self deselectAnnotation:annotation]; } @@ -1604,12 +1604,6 @@ public: BOOL delegateHasImagesForAnnotations = [self.delegate respondsToSelector:@selector(mapView:imageForAnnotation:)]; - NSMutableArray *userPoints = [NSMutableArray array]; - std::vector points; - NSMutableArray *userShapes = [NSMutableArray array]; - std::vector shapes; - NSMutableArray *annotationImages = [NSMutableArray arrayWithCapacity:annotations.count]; - for (id annotation in annotations) { NSAssert([annotation conformsToProtocol:@protocol(MGLAnnotation)], @"Annotation does not conform to MGLAnnotation"); @@ -1619,8 +1613,11 @@ public: if (!multiPoint.pointCount) { continue; } - shapes.emplace_back([multiPoint shapeAnnotationObjectWithDelegate:self]); - [userShapes addObject:annotation]; + + MGLAnnotationTag annotationTag = _mbglMap->addAnnotation([multiPoint annotationObjectWithDelegate:self]); + MGLAnnotationContext context; + context.annotation = annotation; + _annotationContextsByAnnotationTag[annotationTag] = context; } else if ([annotation isKindOfClass:[MGLMultiPolyline class]] || [annotation isKindOfClass:[MGLMultiPolygon class]] || [annotation isKindOfClass:[MGLShapeCollection class]]) { @@ -1647,28 +1644,12 @@ public: self.annotationImagesByIdentifier[annotationImage.reuseIdentifier] = annotationImage; [self installAnnotationImage:annotationImage]; } - [annotationImages addObject:annotationImage]; - - [userPoints addObject:annotation]; - points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), symbolName.UTF8String ?: ""); - - // Opt into potentially expensive tooltip tracking areas. - if (annotation.toolTip.length) { - _wantsToolTipRects = YES; - } - } - } - - // Add any point annotations to mbgl and our own index. - if (points.size()) { - std::vector annotationTags = _mbglMap->addPointAnnotations(points); - - for (size_t i = 0; i < annotationTags.size(); ++i) { - MGLAnnotationTag annotationTag = annotationTags[i]; - MGLAnnotationImage *annotationImage = annotationImages[i]; - annotationImage.styleIconIdentifier = @(points[i].icon.c_str()); - id annotation = userPoints[i]; - + + MGLAnnotationTag annotationTag = _mbglMap->addAnnotation(mbgl::SymbolAnnotation { + MGLPointFromLocationCoordinate2D(annotation.coordinate), + symbolName.UTF8String ?: "" + }); + MGLAnnotationContext context; context.annotation = annotation; context.imageReuseIdentifier = annotationImage.reuseIdentifier; @@ -1678,23 +1659,14 @@ public: NSAssert(![annotation isKindOfClass:[MGLMultiPoint class]], @"Point annotation should not be MGLMultiPoint."); [(NSObject *)annotation addObserver:self forKeyPath:@"coordinate" options:0 context:(void *)(NSUInteger)annotationTag]; } + + // Opt into potentially expensive tooltip tracking areas. + if (annotation.toolTip.length) { + _wantsToolTipRects = YES; + } } } - - // Add any shape annotations to mbgl and our own index. - if (shapes.size()) { - std::vector annotationTags = _mbglMap->addShapeAnnotations(shapes); - - for (size_t i = 0; i < annotationTags.size(); ++i) { - MGLAnnotationTag annotationTag = annotationTags[i]; - id annotation = userShapes[i]; - - MGLAnnotationContext context; - context.annotation = annotation; - _annotationContextsByAnnotationTag[annotationTag] = context; - } - } - + [self didChangeValueForKey:@"annotations"]; [self updateAnnotationTrackingAreas]; @@ -1765,16 +1737,14 @@ public: return; } - std::vector annotationTagsToRemove; - annotationTagsToRemove.reserve(annotations.count); - + [self willChangeValueForKey:@"annotations"]; + for (id annotation in annotations) { NSAssert([annotation conformsToProtocol:@protocol(MGLAnnotation)], @"Annotation does not conform to MGLAnnotation"); MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation]; NSAssert(annotationTag != MGLAnnotationTagNotFound, @"No ID for annotation %@", annotation); - annotationTagsToRemove.push_back(annotationTag); - + if (annotationTag == _selectedAnnotationTag) { [self deselectAnnotation:annotation]; } @@ -1788,10 +1758,10 @@ public: ![annotation isKindOfClass:[MGLMultiPoint class]]) { [(NSObject *)annotation removeObserver:self forKeyPath:@"coordinate" context:(void *)(NSUInteger)annotationTag]; } + + _mbglMap->removeAnnotation(annotationTag); } - [self willChangeValueForKey:@"annotations"]; - _mbglMap->removeAnnotations(annotationTagsToRemove); [self didChangeValueForKey:@"annotations"]; [self updateAnnotationTrackingAreas]; diff --git a/platform/qt/include/qmapbox.hpp b/platform/qt/include/qmapbox.hpp index f2cf363ea3..3200da2729 100644 --- a/platform/qt/include/qmapbox.hpp +++ b/platform/qt/include/qmapbox.hpp @@ -20,11 +20,9 @@ typedef quint32 AnnotationID; typedef QList AnnotationIDs; typedef QPair PointAnnotation; -typedef QList PointAnnotations; // FIXME: We need to add support for custom style properties typedef QPair ShapeAnnotation; -typedef QList ShapeAnnotations; enum NetworkMode { Online, // Default diff --git a/platform/qt/include/qmapboxgl.hpp b/platform/qt/include/qmapboxgl.hpp index 3b10dadc9c..8ce6b02f47 100644 --- a/platform/qt/include/qmapboxgl.hpp +++ b/platform/qt/include/qmapboxgl.hpp @@ -165,15 +165,11 @@ public: QStringList getClasses() const; QMapbox::AnnotationID addPointAnnotation(const QMapbox::PointAnnotation &); - QMapbox::AnnotationIDs addPointAnnotations(const QMapbox::PointAnnotations &); - QMapbox::AnnotationID addShapeAnnotation(const QMapbox::ShapeAnnotation &); - QMapbox::AnnotationIDs addShapeAnnotations(const QMapbox::ShapeAnnotations &); void updatePointAnnotation(QMapbox::AnnotationID, const QMapbox::PointAnnotation &); void removeAnnotation(QMapbox::AnnotationID); - void removeAnnotations(const QMapbox::AnnotationIDs &); bool isRotating() const; bool isScaling() const; diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 976da9a1eb..1f8657c044 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1,7 +1,6 @@ #include "qmapboxgl_p.hpp" -#include -#include +#include #include #include #include @@ -376,40 +375,23 @@ QStringList QMapboxGL::getClasses() const return classNames; } -mbgl::PointAnnotation fromPointAnnotation(const PointAnnotation &pointAnnotation) { +mbgl::Annotation fromPointAnnotation(const PointAnnotation &pointAnnotation) { const Coordinate &coordinate = pointAnnotation.first; const QString &icon = pointAnnotation.second; - return { { coordinate.first, coordinate.second }, icon.toStdString() }; + return mbgl::SymbolAnnotation { mbgl::Point { coordinate.second, coordinate.first }, icon.toStdString() }; } AnnotationID QMapboxGL::addPointAnnotation(const PointAnnotation &pointAnnotation) { - return d_ptr->mapObj->addPointAnnotation(fromPointAnnotation(pointAnnotation)); -} - -AnnotationIDs QMapboxGL::addPointAnnotations(const PointAnnotations &pointAnnotations) -{ - std::vector mbglPointAnnotations; - mbglPointAnnotations.reserve(pointAnnotations.size()); - - for (const PointAnnotation &pointAnnotation : pointAnnotations) { - mbglPointAnnotations.emplace_back(fromPointAnnotation(pointAnnotation)); - } - - AnnotationIDs ids; - for (const mbgl::AnnotationID &id : d_ptr->mapObj->addPointAnnotations(mbglPointAnnotations)) { - ids << id; - } - - return ids; + return d_ptr->mapObj->addAnnotation(fromPointAnnotation(pointAnnotation)); } void QMapboxGL::updatePointAnnotation(AnnotationID id, const PointAnnotation &pointAnnotation) { - d_ptr->mapObj->updatePointAnnotation(id, fromPointAnnotation(pointAnnotation)); + d_ptr->mapObj->updateAnnotation(id, fromPointAnnotation(pointAnnotation)); } -mbgl::ShapeAnnotation fromQMapboxGLShapeAnnotation(const ShapeAnnotation &shapeAnnotation) { +mbgl::Annotation fromQMapboxGLShapeAnnotation(const ShapeAnnotation &shapeAnnotation) { const CoordinateSegments &segments = shapeAnnotation.first; const QString &styleLayer = shapeAnnotation.second; @@ -427,29 +409,12 @@ mbgl::ShapeAnnotation fromQMapboxGLShapeAnnotation(const ShapeAnnotation &shapeA polygon.emplace_back(linearRing); } - return { polygon, styleLayer.toStdString() }; + return mbgl::StyleSourcedAnnotation { polygon, styleLayer.toStdString() }; } AnnotationID QMapboxGL::addShapeAnnotation(const ShapeAnnotation &shapeAnnotation) { - return d_ptr->mapObj->addShapeAnnotation(fromQMapboxGLShapeAnnotation(shapeAnnotation)); -} - -AnnotationIDs QMapboxGL::addShapeAnnotations(const ShapeAnnotations &shapeAnnotations) -{ - std::vector mbglShapeAnnotations; - mbglShapeAnnotations.reserve(shapeAnnotations.size()); - - for (const ShapeAnnotation &shapeAnnotation : shapeAnnotations) { - mbglShapeAnnotations.emplace_back(fromQMapboxGLShapeAnnotation(shapeAnnotation)); - } - - AnnotationIDs ids; - for (const mbgl::AnnotationID &id : d_ptr->mapObj->addShapeAnnotations(mbglShapeAnnotations)) { - ids << id; - } - - return ids; + return d_ptr->mapObj->addAnnotation(fromQMapboxGLShapeAnnotation(shapeAnnotation)); } void QMapboxGL::removeAnnotation(AnnotationID annotationID) @@ -457,18 +422,6 @@ void QMapboxGL::removeAnnotation(AnnotationID annotationID) d_ptr->mapObj->removeAnnotation(annotationID); } -void QMapboxGL::removeAnnotations(const AnnotationIDs &annotationIDs) -{ - std::vector mbglAnnotationIds; - mbglAnnotationIds.reserve(annotationIDs.size()); - - for (const AnnotationID annotationID : annotationIDs) { - mbglAnnotationIds.emplace_back(annotationID); - } - - d_ptr->mapObj->removeAnnotations(mbglAnnotationIds); -} - bool QMapboxGL::isRotating() const { return d_ptr->mapObj->isRotating(); -- cgit v1.2.1