diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index b2e719991..aff6b8581 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog for Mapbox macOS SDK +## master + +* Added `showAnnotations:animated:` and `showAnnotations:edgePadding:animated:`, which moves the map viewport to show the specified annotations. ([#5749](https://github.com/mapbox/mapbox-gl-native/pull/5749)) + ## 0.2.1 * Fixed a crash that occurred when a sprite URL lacks a file extension. See [this comment](https://github.com/mapbox/mapbox-gl-native/issues/5722#issuecomment-233701251) to determine who may be affected by this bug. ([#5723](https://github.com/mapbox/mapbox-gl-native/pull/5723)) diff --git a/platform/macos/app/Base.lproj/MainMenu.xib b/platform/macos/app/Base.lproj/MainMenu.xib index 6b636eb8a..4d8128931 100644 --- a/platform/macos/app/Base.lproj/MainMenu.xib +++ b/platform/macos/app/Base.lproj/MainMenu.xib @@ -1,7 +1,7 @@ - + - + @@ -505,6 +505,12 @@ + + + + + + CA diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index f30b83274..81fd85e15 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -65,6 +65,7 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id 0; } if (menuItem.action == @selector(startWorldTour:)) { @@ -644,9 +655,6 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id ) *)annotations animated:(BOOL)animated; + +/** + Sets the visible region so that the map displays the specified annotations with + the specified amount of padding on each side. + + Calling this method updates the value in the `visibleCoordinateBounds` property + and potentially other properties to reflect the new map region. + + @param annotations The annotations that you want to be visible in the map. + @param insets The minimum padding (in screen points) around the edges of the + map view to keep clear of annotations. + @param animated `YES` if you want the map region change to be animated, or `NO` + if you want the map to display the new region immediately without animations. + */ +- (void)showAnnotations:(NS_ARRAY_OF(id ) *)annotations edgePadding:(NSEdgeInsets)insets animated:(BOOL)animated; + /** Returns the camera that best fits the given coordinate bounds. diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 5785f56e0..7b0aad1db 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -1998,6 +1998,38 @@ public: } } +- (void)showAnnotations:(NS_ARRAY_OF(id ) *)annotations animated:(BOOL)animated { + CGFloat maximumPadding = 100; + CGFloat yPadding = (NSHeight(self.bounds) / 5 <= maximumPadding) ? (NSHeight(self.bounds) / 5) : maximumPadding; + CGFloat xPadding = (NSWidth(self.bounds) / 5 <= maximumPadding) ? (NSWidth(self.bounds) / 5) : maximumPadding; + + NSEdgeInsets edgeInsets = NSEdgeInsetsMake(yPadding, xPadding, yPadding, xPadding); + + [self showAnnotations:annotations edgePadding:edgeInsets animated:animated]; +} + +- (void)showAnnotations:(NS_ARRAY_OF(id ) *)annotations edgePadding:(NSEdgeInsets)insets animated:(BOOL)animated { + if ( ! annotations || ! annotations.count) return; + + mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty(); + + for (id annotation in annotations) + { + if ([annotation conformsToProtocol:@protocol(MGLOverlay)]) + { + bounds.extend(MGLLatLngBoundsFromCoordinateBounds(((id )annotation).overlayBounds)); + } + else + { + bounds.extend(MGLLatLngFromLocationCoordinate2D(annotation.coordinate)); + } + } + + [self setVisibleCoordinateBounds:MGLCoordinateBoundsFromLatLngBounds(bounds) + edgePadding:insets + animated:animated]; +} + /// Returns a popover detailing the annotation. - (NSPopover *)calloutForAnnotation:(id )annotation { NSPopover *callout = [[NSPopover alloc] init];