当用户位置仅在移动时,如何使用setregion()设置区域



我已经尝试删除我的setRegion((,但它仍然存在,不允许平移地图。构建会覆盖用于浏览地图视图的手指手势,并在返回到原始位置之前做出非常短暂的响应。我不确定这个问题。我已经将我的代码改写为一个新项目,没有任何问题,有人能向我解释我哪里出了问题吗?

import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()
let regionInMeters: Double = 15000


override func viewDidLoad() {
super.viewDidLoad()
checkLocationServices()
}

func setupLocationManager(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}

func centerUserLocation(){
if let location = locationManager.location?.coordinate{
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
}
func checkLocationServices(){
if CLLocationManager.locationServicesEnabled(){
setupLocationManager()
checkLocationAuthorization()
} else {
// alert user must turn on
}
}

func checkLocationAuthorization(){
switch CLLocationManager.authorizationStatus(){
case .authorizedWhenInUse:
mapView.showsUserLocation = true
centerUserLocation()
locationManager.startUpdatingLocation()
break
case .denied:
mapView.showsUserLocation = false
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
case .restricted:
//supervisory controls enabled
mapView.showsUserLocation = false
break
case .authorizedAlways:
mapView.showsUserLocation = true
break
}
}

}
extension ViewController: CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {return}
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion.init(center: center, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorization()
}
}

您正在didUpdateLocations中调用setRegion-这将在每次获得位置更新时更改地图区域;大约每秒一次,具有CCD_ 3精度;即使用户没有移动

如果您希望在地图视图上跟踪用户,则应使用内置的userTrackingMode功能。

相关内容

最新更新