如果我想在地图上显示用户位置,并在同一时间记录用户的位置,它是一个好主意,添加一个观察者userLocation.location并记录位置,或者我应该仍然使用CLLocationManager用于记录用户位置和使用方法mapView.showUserLocation显示用户的当前位置(蓝色指示灯)?我想显示MapKit API支持的默认蓝色指示器.
另外,这是一个粗略的示例代码:
- (void)viewDidLoad {
...
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
myMapView.showUserLocation = YES;
[myMapView addObserver:self forKeyPath:@"userLocation.location" options:0 context:nil];
...
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// Record the location information
// ...
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"%s begins.", __FUNCTION__);
// Make sure that the location returned has the desired accuracy
if (newLocation.horizontalAccuracy <= manager.desiredAccuracy)
return;
// Record the …Run Code Online (Sandbox Code Playgroud) 我创建了一个地图,右上角有一个用户跟踪按钮。我想知道如何在加载时默认将状态设置为“followWithHeadlights”,以便它跟随用户当前位置,就像在 GPS 上一样?
负载时的当前行为: 按钮当前行为
加载时所需的行为: 按钮所需的行为
代码片段:
func setUserTrackingButton() {
// Mapkit tracking button
let trackingButton: MKUserTrackingBarButtonItem = MKUserTrackingBarButtonItem.init(mapView: mapView)
trackingButton.customView?.tintColor = UIColor(red:0.01, green:0.81, blue:0.37, alpha:1.0)
trackingButton.customView?.frame.size = CGSize(width: 50, height: 50)
let toolBarFrame = CGRect(origin: CGPoint(x: 0, y: 0) , size: CGSize(width: 50, height: 50))
let toolbar = UIToolbar.init(frame: toolBarFrame)
toolbar.barTintColor = UIColor.white
toolbar.isTranslucent = true
let flex: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbar.items = [flex, trackingButton, flex]
let origin = CGPoint(x: self.view.frame.size.width - 85, …Run Code Online (Sandbox Code Playgroud)