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

Commit

Permalink
[ios] Add MGLMapView.showsScale to control scale bar visibility
Browse files Browse the repository at this point in the history
- Fixes scale bar not being visible until a camera change event.
- Adds IBInspectable for scale bar visibility.
  • Loading branch information
friedbunny committed Feb 28, 2018
1 parent ab27e24 commit a554425
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
2 changes: 2 additions & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
* Long-pressing the attribution button causes the SDK’s version number to be displayed in the action sheet that appears. ([#10650](https://github.com/mapbox/mapbox-gl-native/pull/10650))
* Reduced offline download size for styles with symbol layers that render only icons, and no text. ([#11055](https://github.com/mapbox/mapbox-gl-native/pull/11055))
* Added haptic feedback that occurs when the user rotates the map to due north, configurable via `MGLMapView.hapticFeedbackEnabled`. ([#10847](https://github.com/mapbox/mapbox-gl-native/pull/10847))
* Added `MGLMapView.showsScale` as the recommended way to show the scale bar. This property can be set directly in Interface Builder. ([#11335](https://github.com/mapbox/mapbox-gl-native/pull/11335))
* Fixed an issue where the scale bar would not appear until the map had moved. ([#11335](https://github.com/mapbox/mapbox-gl-native/pull/11335))

## 3.7.5 - February 16, 2018

Expand Down
2 changes: 1 addition & 1 deletion platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ - (void)viewDidLoad
[self restoreState:nil];

self.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"];
self.mapView.scaleBar.hidden = NO;
self.mapView.showsScale = YES;
self.mapView.showsUserHeadingIndicator = YES;
if ([UIFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
self.hudLabel.titleLabel.font = [UIFont monospacedDigitSystemFontOfSize:10 weight:UIFontWeightRegular];
Expand Down
1 change: 1 addition & 0 deletions platform/ios/src/MGLMapView+IBAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) IBInspectable BOOL allowsTilting;
@property (nonatomic) IBInspectable BOOL showsUserLocation;
@property (nonatomic) IBInspectable BOOL showsHeading;
@property (nonatomic) IBInspectable BOOL showsScale;

@end

Expand Down
11 changes: 10 additions & 1 deletion platform/ios/src/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,18 @@ MGL_EXPORT IB_DESIGNABLE
*/
- (IBAction)reloadStyle:(id)sender;

/**
A Boolean value indicating whether the map may display scale information.
The scale bar may not be shown at all zoom levels. The view controlled by this
property is available at `scaleBar`. The default value of this property is
`NO`.
*/
@property (nonatomic, assign) BOOL showsScale;

/**
A control indicating the scale of the map. The scale bar is positioned in the
upper-left corner. The scale bar is hidden by default.
upper-left corner. Enable the scale bar via `showsScale`.
*/
@property (nonatomic, readonly) UIView *scaleBar;

Expand Down
32 changes: 26 additions & 6 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,8 @@ - (void)glkView:(__unused GLKView *)view drawInRect:(__unused CGRect)rect
- (void)layoutSubviews
{
// Calling this here instead of in the scale bar itself because if this is done in the
// scale bar instance, it triggers a call to this this `layoutSubviews` method that
// calls `_mbglMap->setSize()` just below that triggers rendering update which triggers
// scale bar instance, it triggers a call to this `layoutSubviews` method that calls
// `_mbglMap->setSize()` just below that triggers rendering update which triggers
// another scale bar update which causes a rendering update loop and a major performace
// degradation. The only time the scale bar's intrinsic content size _must_ invalidated
// is here as a reaction to this object's view dimension changes.
Expand Down Expand Up @@ -2362,6 +2362,17 @@ - (void)setPitchEnabled:(BOOL)pitchEnabled
self.twoFingerDrag.enabled = pitchEnabled;
}

- (void)setShowsScale:(BOOL)showsScale
{
_showsScale = showsScale;
self.scaleBar.hidden = !showsScale;

if (showsScale)
{
[self updateScaleBar];
}
}

#pragma mark - Accessibility -

- (NSString *)accessibilityValue
Expand Down Expand Up @@ -5413,10 +5424,7 @@ - (void)cameraIsChanging {
}

[self updateCompass];

if (!self.scaleBar.hidden) {
[(MGLScaleBar *)self.scaleBar setMetersPerPoint:[self metersPerPointAtLatitude:self.centerCoordinate.latitude]];
}
[self updateScaleBar];

if ([self.delegate respondsToSelector:@selector(mapView:regionIsChangingWithReason:)])
{
Expand All @@ -5434,6 +5442,7 @@ - (void)cameraDidChangeAnimated:(BOOL)animated {
}

[self updateCompass];
[self updateScaleBar];

if ( ! [self isSuppressingChangeDelimiters])
{
Expand Down Expand Up @@ -5878,6 +5887,17 @@ - (void)updateCompass
}
}

- (void)updateScaleBar
{
// Use the `hidden` property (instead of `self.showsScale`) so that we don't
// break developers who still rely on the <4.0.0 approach of directly
// setting this property.
if ( ! self.scaleBar.hidden)
{
[(MGLScaleBar *)self.scaleBar setMetersPerPoint:[self metersPerPointAtLatitude:self.centerCoordinate.latitude]];
}
}

+ (UIImage *)resourceImageNamed:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName
Expand Down

0 comments on commit a554425

Please sign in to comment.