Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[macos] Add showAnnotations: methods (#5749)
Browse files Browse the repository at this point in the history
SDK
- Ported `showAnnotations:animated:` and `showAnnotations:edgePadding:animated:` from the iOS SDK.

Demo App
- Added "Show All Annotations" debug menu item with ⇧⌘A shortcut key.
- Disabled "Add Animated Annotation" debug menu item when the annotation already shown.
  • Loading branch information
friedbunny authored Jul 21, 2016
1 parent e686d32 commit 4dd734f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
4 changes: 4 additions & 0 deletions platform/macos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
10 changes: 8 additions & 2 deletions platform/macos/app/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11163.2" systemVersion="15F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="10117" systemVersion="15G26a" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11163.2"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10117"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand Down Expand Up @@ -505,6 +505,12 @@
<action selector="drawAnimatedAnnotation:" target="-1" id="CYM-WB-s97"/>
</connections>
</menuItem>
<menuItem title="Show All Annnotations" keyEquivalent="A" id="yMj-uM-8SN">
<modifierMask key="keyEquivalentModifierMask" shift="YES" command="YES"/>
<connections>
<action selector="showAllAnnotations:" target="-1" id="ahr-OR-Em2"/>
</connections>
</menuItem>
<menuItem title="Remove All Annotations" id="6rC-68-vk0">
<string key="keyEquivalent" base64-UTF8="YES">
CA
Expand Down
18 changes: 13 additions & 5 deletions platform/macos/app/MapDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ @implementation MapDocument {
BOOL _randomizesCursorsOnDroppedPins;
BOOL _isTouringWorld;
BOOL _isShowingPolygonAndPolylineAnnotations;
BOOL _isShowingAnimatedAnnotation;
}

#pragma mark Lifecycle
Expand Down Expand Up @@ -329,9 +330,14 @@ - (void)dropOneOfManyPins:(NSTimer *)timer {
}
}

- (IBAction)showAllAnnotations:(id)sender {
[self.mapView showAnnotations:self.mapView.annotations animated:YES];
}

- (IBAction)removeAllAnnotations:(id)sender {
[self.mapView removeAnnotations:self.mapView.annotations];
_isShowingPolygonAndPolylineAnnotations = NO;
_isShowingAnimatedAnnotation = NO;
}

- (IBAction)startWorldTour:(id)sender {
Expand Down Expand Up @@ -409,6 +415,8 @@ - (IBAction)drawAnimatedAnnotation:(id)sender {
DroppedPinAnnotation *annotation = [[DroppedPinAnnotation alloc] init];
[self.mapView addAnnotation:annotation];

_isShowingAnimatedAnnotation = YES;

[NSTimer scheduledTimerWithTimeInterval:1.0/60.0
target:self
selector:@selector(updateAnimatedAnnotation:)
Expand Down Expand Up @@ -632,10 +640,13 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem.action == @selector(dropManyPins:)) {
return YES;
}
if (menuItem.action == @selector(drawPolygonAndPolyLineAnnotations:)) {
return !_isShowingPolygonAndPolylineAnnotations;
}
if (menuItem.action == @selector(drawAnimatedAnnotation:)) {
return YES;
return !_isShowingAnimatedAnnotation;
}
if (menuItem.action == @selector(removeAllAnnotations:)) {
if (menuItem.action == @selector(showAllAnnotations:) || menuItem.action == @selector(removeAllAnnotations:)) {
return self.mapView.annotations.count > 0;
}
if (menuItem.action == @selector(startWorldTour:)) {
Expand All @@ -644,9 +655,6 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem.action == @selector(stopWorldTour:)) {
return _isTouringWorld;
}
if (menuItem.action == @selector(drawPolygonAndPolyLineAnnotations:)) {
return !_isShowingPolygonAndPolylineAnnotations;
}
if (menuItem.action == @selector(addOfflinePack:)) {
NSURL *styleURL = self.mapView.styleURL;
return !styleURL.isFileURL;
Expand Down
29 changes: 29 additions & 0 deletions platform/macos/src/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,35 @@ IB_DESIGNABLE
*/
- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(NSEdgeInsets)insets animated:(BOOL)animated;

/**
Sets the visible region so that the map displays the specified annotations.
Calling this method updates the value in the `visibleCoordinateBounds` property
and potentially other properties to reflect the new map region. A small amount
of padding is reserved around the edges of the map view. To specify a different
amount of padding, use the `-showAnnotations:edgePadding:animated:` method.
@param annotations The annotations that you want to be visible in the map.
@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 <MGLAnnotation>) *)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 <MGLAnnotation>) *)annotations edgePadding:(NSEdgeInsets)insets animated:(BOOL)animated;

/**
Returns the camera that best fits the given coordinate bounds.
Expand Down
32 changes: 32 additions & 0 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,38 @@ - (void)selectAnnotation:(id <MGLAnnotation>)annotation
}
}

- (void)showAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)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 <MGLAnnotation>) *)annotations edgePadding:(NSEdgeInsets)insets animated:(BOOL)animated {
if ( ! annotations || ! annotations.count) return;

mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();

for (id <MGLAnnotation> annotation in annotations)
{
if ([annotation conformsToProtocol:@protocol(MGLOverlay)])
{
bounds.extend(MGLLatLngBoundsFromCoordinateBounds(((id <MGLOverlay>)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 <MGLAnnotation>)annotation {
NSPopover *callout = [[NSPopover alloc] init];
Expand Down

0 comments on commit 4dd734f

Please sign in to comment.