我正在评估从MapKit切换到Mapbox。
除了MKUserTrackingBarButtonItem
之外,我已经找到了所有函数的等价物。
let trackingButton = MKUserTrackingBarButtonItem(mapView: map)
有没有办法为Mapbox iOS SDK创建此按钮?
我在Mapbox文档中找不到任何类似内容,但我无法继续使用此功能,因为它需要MKMapView
。
提前致谢。
答案 0 :(得分:4)
以防万一其他人感兴趣,这是this answer在简单的ViewController
中翻译成Swift。我只是遗漏了.FollowWithCourse
来复制MKUserTrackingBarButtonItem
的行为。
class ViewController: UIViewController, MGLMapViewDelegate {
@IBOutlet weak var map: MGLMapView!
@IBOutlet weak var toolbar: UIToolbar!
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
map.setCenterCoordinate(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 12, animated: false)
let trackingButton = UIBarButtonItem(image: UIImage(named: "TrackingLocationOffMask"), style: UIBarButtonItemStyle.Plain, target: self, action: "trackingButtonChanged")
toolbar.items!.insert(trackingButton, atIndex: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func mapView(mapView: MGLMapView, didChangeUserTrackingMode mode: MGLUserTrackingMode, animated: Bool) {
var image: String = "TrackingLocationOffMask.png"
switch (mode) {
case .Follow:
image = "TrackingLocationMask.png"
break
case .FollowWithHeading:
image = "TrackingHeadingMask.png"
break
default:
break
}
UIView.animateWithDuration(0.25, animations: {
(self.toolbar.items![0] as UIBarButtonItem).image = UIImage(named: image)
})
}
func trackingButtonChanged() {
var mode: MGLUserTrackingMode = .Follow
switch (map.userTrackingMode) {
case .Follow:
mode = .FollowWithHeading
break
case .FollowWithHeading:
mode = .None
break
default:
break
}
map.userTrackingMode = mode
}
}
图片在GitHub上的mapbox-gl-native处是here。
答案 1 :(得分:2)
相当于Mapbox iOS SDK中的MKUserTrackingBarButtonItem
doesn’t yet exist(从v3.1开始),但有一个基本实现in the project’s demo app:
- (void)viewDidLoad
{
...
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"TrackingLocationOffMask.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(locateUser)];
}
- (void)locateUser
{
MGLUserTrackingMode nextMode;
switch (self.mapView.userTrackingMode) {
case MGLUserTrackingModeNone:
nextMode = MGLUserTrackingModeFollow;
break;
case MGLUserTrackingModeFollow:
nextMode = MGLUserTrackingModeFollowWithHeading;
break;
case MGLUserTrackingModeFollowWithHeading:
nextMode = MGLUserTrackingModeFollowWithCourse;
break;
case MGLUserTrackingModeFollowWithCourse:
nextMode = MGLUserTrackingModeNone;
break;
}
self.mapView.userTrackingMode = nextMode;
}
- (void)mapView:(__unused MGLMapView *)mapView didChangeUserTrackingMode:(MGLUserTrackingMode)mode animated:(__unused BOOL)animated
{
UIImage *newButtonImage;
NSString *newButtonTitle;
switch (mode) {
case MGLUserTrackingModeNone:
newButtonImage = [UIImage imageNamed:@"TrackingLocationOffMask.png"];
break;
case MGLUserTrackingModeFollow:
newButtonImage = [UIImage imageNamed:@"TrackingLocationMask.png"];
break;
case MGLUserTrackingModeFollowWithHeading:
newButtonImage = [UIImage imageNamed:@"TrackingHeadingMask.png"];
break;
case MGLUserTrackingModeFollowWithCourse:
newButtonImage = nil;
newButtonTitle = @"Course";
break;
}
self.navigationItem.rightBarButtonItem.title = newButtonTitle;
[UIView animateWithDuration:0.25 animations:^{
self.navigationItem.rightBarButtonItem.image = newButtonImage;
}];
}