gpt4 book ai didi

swift - 谷歌地图当前位置 swift

转载 作者:行者123 更新时间:2023-11-30 12:36:05 26 4
gpt4 key购买 nike

我正在尝试使用用户当前位置渲染 Google map ,但无论我做什么,它都只会渲染默认位置(欧洲地区周围)。由于我是 Swift 新手,我不知道如何调试它,想在这里询问我需要做什么。

Swift 代码

import UIKit
import GoogleMaps

class ViewController: UIViewController {

@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
}
extension ViewController: CLLocationManagerDelegate {

private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
}
}

private func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}

最佳答案

使用确实更改了您应该使用的位置管理器的授权委托(delegate)然后开始更新位置在委托(delegate)方法之前删除私钥,并将以下属性放入更新位置委托(delegate)方法中。删除停止更新位置方法。

func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways, .authorizedWhenInUse:
self.locationManager.startUpdatingLocation()
break

case .denied, .restricted:
self.disableLocationRelatedFeatures()
break

// Do nothing otherwise.
default: break
}
}

self.mapView.isMyLocationEnabled = true
self.mapView.settings.myLocationButton = true

关于swift - 谷歌地图当前位置 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42866892/

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