From 645dd25d0b8fa3ca39c2362083e7822f4a955fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Sun, 22 May 2016 23:37:56 -0700 Subject: [PATCH] [ios, osx] Fixed crash adding mixture of points and shapes Fixes #5096. --- platform/ios/src/MGLMapView.mm | 8 ++++++-- platform/osx/CHANGELOG.md | 1 + platform/osx/src/MGLMapView.mm | 8 ++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 5bd29836493..1b33e379afc 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -2781,7 +2781,9 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations if ( ! annotations) return; [self willChangeValueForKey:@"annotations"]; + NSMutableArray *userPoints = [NSMutableArray array]; std::vector points; + NSMutableArray *userShapes = [NSMutableArray array]; std::vector shapes; NSMutableDictionary *annotationImagesForAnnotation = [NSMutableDictionary dictionary]; @@ -2797,6 +2799,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations if ([annotation isKindOfClass:[MGLMultiPoint class]]) { [(MGLMultiPoint *)annotation addShapeAnnotationObjectToCollection:shapes withDelegate:self]; + [userShapes addObject:annotation]; } else { @@ -2846,6 +2849,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations annotationImagesForAnnotation[annotationValue] = annotationImage; } + [userPoints addObject:annotation]; points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), symbolName.UTF8String ?: ""); } } @@ -2858,7 +2862,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations for (size_t i = 0; i < annotationTags.size(); ++i) { - id annotation = annotations[i]; + id annotation = userPoints[i]; NSValue *annotationValue = [NSValue valueWithNonretainedObject:annotation]; MGLAnnotationContext context; @@ -2890,7 +2894,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations for (size_t i = 0; i < annotationTags.size(); ++i) { MGLAnnotationTag annotationTag = annotationTags[i]; - id annotation = annotations[i]; + id annotation = userPoints[i]; MGLAnnotationContext context; context.annotation = annotation; diff --git a/platform/osx/CHANGELOG.md b/platform/osx/CHANGELOG.md index cc62d3d0377..d4b245b6c70 100644 --- a/platform/osx/CHANGELOG.md +++ b/platform/osx/CHANGELOG.md @@ -3,6 +3,7 @@ ## master * Fixed an issue in which Mapbox.framework was nested inside another folder named Mapbox.framework. ([#4998](https://github.com/mapbox/mapbox-gl-native/pull/4998)) +* Fixed a crash passing a mixture of point and shape annotations into `-[MGLMapView addAnnotations:]`. ([#5097](https://github.com/mapbox/mapbox-gl-native/pull/5097)) ## 0.1.0 diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm index 7a17aa92508..055512eb768 100644 --- a/platform/osx/src/MGLMapView.mm +++ b/platform/osx/src/MGLMapView.mm @@ -1603,7 +1603,9 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations { 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]; @@ -1613,6 +1615,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations { if ([annotation isKindOfClass:[MGLMultiPoint class]]) { // The multipoint knows how to style itself (with the map view’s help). [(MGLMultiPoint *)annotation addShapeAnnotationObjectToCollection:shapes withDelegate:self]; + [userShapes addObject:annotation]; } else { MGLAnnotationImage *annotationImage = nil; if (delegateHasImagesForAnnotations) { @@ -1637,6 +1640,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations { } [annotationImages addObject:annotationImage]; + [userPoints addObject:annotation]; points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), symbolName.UTF8String ?: ""); // Opt into potentially expensive tooltip tracking areas. @@ -1654,7 +1658,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations { MGLAnnotationTag annotationTag = annotationTags[i]; MGLAnnotationImage *annotationImage = annotationImages[i]; annotationImage.styleIconIdentifier = @(points[i].icon.c_str()); - id annotation = annotations[i]; + id annotation = userPoints[i]; MGLAnnotationContext context; context.annotation = annotation; @@ -1674,7 +1678,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations { for (size_t i = 0; i < annotationTags.size(); ++i) { MGLAnnotationTag annotationTag = annotationTags[i]; - id annotation = annotations[i]; + id annotation = userShapes[i]; MGLAnnotationContext context; context.annotation = annotation;