gpt4 book ai didi

ios - 使用谷歌地图快速获取我的位置

转载 作者:行者123 更新时间:2023-11-30 13:12:10 27 4
gpt4 key购买 nike

我有这个代码:

override func viewDidLoad() {
super.viewDidLoad()

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()

}

和这段代码:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations.last
print(userLocation)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
longitude: userLocation!.coordinate.longitude, zoom: 12.5)

self.mapView.camera = camera
self.mapView.myLocationEnabled = true
// self.mapView.settings.myLocationButton = true

locationManager.stopUpdatingLocation()
}

这在之前是有效的,但现在看来 locationManager 甚至没有被调用,因为 print 语句根本没有返回任何信息。代码中没有错误,我似乎无法弄清楚为什么该函数没有被调用。有人能发现我遗漏的任何明显的东西吗?

谢谢

最佳答案

我刚刚实现了这个,我会告诉你我做了什么

首先,创建一个 locationManager 变量

让 locationManager: CLLocationManager = CLLocationManager()

您必须在viewDidLoad()中请求权限

viewMap.delegate = self
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()

然后,我创建了一个扩展并实现了委托(delegate)

extension MyView: CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
//here you configure the locationManager

locationManager.startUpdatingLocation()

viewMap.isMyLocationEnabled = true
viewMap.settings.myLocationButton = true
}

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

}

}

我希望这对您有帮助,这里工作正常。

关于ios - 使用谷歌地图快速获取我的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38616456/

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