gpt4 book ai didi

swift - map View 不以当前用户位置为中心

转载 作者:行者123 更新时间:2023-11-28 07:31:27 25 4
gpt4 key购买 nike

我正在尝试将 map View 集中在用户位置上,但到目前为止它还没有用。我尝试在我的物理设备上测试它,但没有成功,我找不到有效的解决方案,请帮助我。

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
}

最佳答案

您需要在新位置更新相机。

阅读here获取更多信息。

基本上,您需要植入函数 CameraUpdate()。

关于swift - map View 不以当前用户位置为中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com