Swift mapview不以当前用户位置为中心
我试图将地图视图集中在用户位置上,但到目前为止,它不起作用。我试图在我的物理设备上测试它,但运气不好,我找不到一个有效的解决方案,请帮助我Swift mapview不以当前用户位置为中心,swift,swift4,Swift,Swift4,我试图将地图视图集中在用户位置上,但到目前为止,它不起作用。我试图在我的物理设备上测试它,但运气不好,我找不到一个有效的解决方案,请帮助我 class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { var mapView: MKMapView! var locationManager: CLLocationManager! let distanceSpan: Double
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var mapView: MKMapView!
var locationManager: CLLocationManager!
let distanceSpan: Double = 500
override func viewDidLoad() {
super.viewDidLoad()
setupMapView()
}
fileprivate func setupLocationManager() {self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() == true {
if CLLocationManager.authorizationStatus() == .restricted || CLLocationManager.authorizationStatus() == .denied || CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestWhenInUseAuthorization()
}
locationManager.desiredAccuracy = 1.0
locationManager.delegate = self
locationManager.startUpdatingLocation()
} else {
print("PLease turn on location services or GPS")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
self.mapView.setRegion(region, animated: true)
}
fileprivate func setupMapView() {
let mView = MKMapView(frame: CGRect(x: 0, y: 20, width: view.frame.width, height: view.frame.height))
mView.showsUserLocation = true
view.addSubview(mView)
self.mapView = mView
}
您需要在新位置更新相机 阅读了解更多信息
基本上,您需要实现CameraUpdate()函数。您需要在新位置更新相机 阅读了解更多信息
基本上,您需要实现CameraUpdate()函数。您能进一步澄清这个问题吗?那我就可以给你一个更好的指导了。你能更清楚地说明这个问题吗?那我就可以给你更好的指导了。