我试图在用户接受位置跟踪时为其设置动画。该代码在他们已经接受位置跟踪然后加载视图时起作用,但是我希望在按位置跟踪接受时重新加载视图。当用户允许位置跟踪时更新位置
override func viewDidLoad() {
super.viewDidLoad()
//Prepare to get User
if CLLocationManager.authorizationStatus() != .authorizedAlways {
// May need to change to support location based notifications
locationManager.requestAlwaysAuthorization()
print("hello")
mapView.setVisibleMapRect(mapView.visibleMapRect, animated: true)
}else {
locationManager.startUpdatingLocation()
}
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
mapView.delegate = self
}
动画用户位置代码
//Get user location
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Show the map at the users current location
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.02,0.02)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
locationManager.stopUpdatingLocation()
}
嘿我添加了代码,但功能似乎不被调用。 –
啊,对不起!我编辑了这篇文章。这是旧的快速。它应该是func locationManager(_ manager:CLLocationManager,didChangeAuthorization status:CLAuthorizationStatus){} –
谢谢!它现在可以工作,但我可以使它对当前位置有动画效果吗? –