Mapbox 中的 MKUserTrackingBarButtonItem
MKUserTrackingBarButtonItem in Mapbox
我正在评估从 MapKit 到 Mapbox 的转换。
除了 MKUserTrackingBarButtonItem
.
,我已经找到了所有功能的等价物
let trackingButton = MKUserTrackingBarButtonItem(mapView: map)
有没有办法为 Mapbox iOS SDK 创建这个按钮?
我在 Mapbox 文档中没有找到任何类似的东西,我不能继续使用这个功能,因为它需要一个 MKMapView
。
提前致谢。
相当于MKUserTrackingBarButtonItem
doesn’t yet exist in the Mapbox iOS SDK (as of v3.1), but there is a basic implementation 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;
}];
}
以防万一其他人有兴趣,这是 翻译成 Swift 的简单 ViewController
。我只是遗漏了 .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
}
}
图像 here 位于 GitHub 上的 mapbox-gl-native。
我正在评估从 MapKit 到 Mapbox 的转换。
除了 MKUserTrackingBarButtonItem
.
let trackingButton = MKUserTrackingBarButtonItem(mapView: map)
有没有办法为 Mapbox iOS SDK 创建这个按钮?
我在 Mapbox 文档中没有找到任何类似的东西,我不能继续使用这个功能,因为它需要一个 MKMapView
。
提前致谢。
相当于MKUserTrackingBarButtonItem
doesn’t yet exist in the Mapbox iOS SDK (as of v3.1), but there is a basic implementation 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;
}];
}
以防万一其他人有兴趣,这是 ViewController
。我只是遗漏了 .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
}
}
图像 here 位于 GitHub 上的 mapbox-gl-native。