gpt4 book ai didi

ios - 无法获取用户当前位置

转载 作者:行者123 更新时间:2023-11-30 11:02:09 25 4
gpt4 key购买 nike

我的应用程序有一个简单的目标,即获取当前坐标,并使用它们在 map View 上添加注释。

我已经尝试了很多来自谷歌结果的解决方案,但它仍然不起作用......

调试区域从未显示“locationManager did UpdateLocation”,这是我在函数中打印的消息......

应用程序似乎从未运行过“did UpdateLocation”函数,甚至调用了 startUpdatingLocation() ?

<小时/>

在 info.plist 中添加位置隐私字符串:完成。

打开 Mac Pro 上的 GPS:完成。

Xcode 版本:10.1

MacOS:10.13.6(High Sierra)

<小时/>
let cloaction = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

MapView.delegate = self
MapView.showsScale = true
MapView.showsPointsOfInterest = true
MapView.showsUserLocation = true

cloaction.requestAlwaysAuthorization()
cloaction.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
print("IN")

cloaction.delegate = self
cloaction.desiredAccuracy = kCLLocationAccuracyBest
cloaction.startUpdatingLocation()
}
}

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

print("locationManager did UpdateLocation")

let location = CLLocationCoordinate2D(latitude: (locations.first?.coordinate.latitude)!, longitude: (locations.first?.coordinate.longitude)!)

currentLat = (locations.first?.coordinate.latitude)!
currentLon = (locations.first?.coordinate.longitude)!

let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)

MapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(currentLat,currentLon), span: span), animated: true)
MapView.showsUserLocation = true

print(locations.first?.coordinate.latitude)
print(locations.first?.coordinate.longitude)

}

最佳答案

实际上原因很简单:您调用 didUpdateLocation 方法,该方法仅在您更改位置时调用。您的 Mac 位于某个位置且不动,这就是它无法工作的原因。

关于ios - 无法获取用户当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53218908/

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