gpt4 book ai didi

ios - MapKit View 未放大到用户位置

转载 作者:行者123 更新时间:2023-11-28 08:05:12 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个 MKMapKit View ,当我的应用程序启动时,如果用户允许位置服务,我希望 map 放大到用户的位置。我写的代码是:

override func viewDidLoad() {
super.viewDidLoad()

mapKitView.delegate = self
mapKitView.showsUserLocation = true
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self

if (CLLocationManager.locationServicesEnabled()) {

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
}

locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}

DispatchQueue.main.async {
self.locationManager.startUpdatingLocation()
}
}


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

let noLocation = CLLocationCoordinate2D()
let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
mapKitView.setRegion(viewRegion, animated: false)
}

在我的应用中,它显示了用户的位置,但没有动画和放大。

最佳答案

使用用户的位置来计算 View 区域并将animated参数更改为true:

if let location = mapKitView.userLocation.location {
let viewRegion = MKCoordinateRegionMakeWithDistance(location, 200, 200)
mapKitView.setRegion(viewRegion, animated: true)
}

关于ios - MapKit View 未放大到用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384410/

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