我試圖在用戶接受位置跟蹤時爲其設置動畫。該代碼在他們已經接受位置跟蹤然後加載視圖時起作用,但是我希望在按位置跟蹤接受時重新加載視圖。當用戶允許位置跟蹤時更新位置
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){} –
謝謝!它現在可以工作,但我可以使它對當前位置有動畫效果嗎? –