
Why is activity type on the default location manager immutable? Should I create a custom location manager just to set my own activity type?
Mapbox SDK version: 4.9.0
iOS versions: 12.1
Xcode version: 10.1
activityType is mutable. The ? denotes that the MGLLocationManager protocol only includes activityType optionally but doesn鈥檛 require it. So you should be able to get around the error by saying something like:
mapView.locationManager.activityType? = .fitness
activityType is indeed marked @optional in the MGLLocationManager protocol.
However, your suggestion won't work. As far as I know that's not how you set an optional var in Swift. After some research I conclude that there's no easy way to set a variable that is declared optional in an objc protocol. I'm looking at some work-arounds but haven't had any success so far. Any suggestions are greatly appreciated.
I've created a class in objective-C that set's the location manager's activity type.
MapViewHelper.h
````
@import Mapbox;
@interface MapViewHelper : NSObject
@end
````
MapViewHelper.m
#import "MapViewHelper.h"
@interface MapViewHelper()
@property (nonatomic) MGLMapView *mapView;
@end
@implementation MapViewHelper
- (id)initWithMapView:(MGLMapView *)mapView {
self = [super init];
if (self) {
self.mapView = mapView;
}
return self;
}
- (void)setLocationManagerActivityType:(CLActivityType)activityType {
self.mapView.locationManager.activityType = activityType;
}
@end
Now I can call this from my view controller (Swift)
MapViewHelper(mapView: mapView).setLocationManagerActivityType(.fitness)
I really dislike having to add Objective-C to my project just to achieve something so trivial. I welcome every suggestion to solve this using Swift only.
This creates difficulties and increases the load on the CPU to 60% (the iPad 4 for example). It will be great if you adapt the MGLLocationManager protocol for compatibility with Swift!
I really dislike having to add Objective-C to my project just to achieve something so trivial.
Marking as a bug, as I agree with this. Let's look at our options here.
Agreed 鈥斅爋ne might imagine this would be a workaround:
if let locationManager = mapView.locationManager as? CLLocationManager {
locationManager.activityType = .airborne
}
... but that doesn鈥檛 work, since MGLMapView.locationManager is actually MGLCLLocationManager, which merely forwards its calls to MGLCLLocationManager.locationManager.
That being the case, it鈥檚 also not possible to set our default location manager鈥檚 distanceFilter or desiredAccuracy properties in Swift.
I've also tried creating a new protocol (MGLLocationManagerWithActivityType) where activityType is required, and force casting mapView.locationManager to this new protocol. Unfortunately it seems that this cannot be done.
Too bad we can't just simply make activityType required in the existing protocol, since that would be a breaking change for everyone who has created a custom location manager that conforms to MGLLocationManager without implementing activityType.
This is fixed in https://github.com/mapbox/mapbox-gl-native/pull/14477