gpt4 book ai didi

swift - 谷歌地图不显示当前位置或定位按钮

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

我将我的谷歌地图 View 嵌入到 TabBar Controller 中,它是选项卡中的第二个项目。 View 加载 map 但不显示当前位置或定位按钮。我已经编辑了该方案以包含供构建使用的位置。

    var locationManager = CLLocationManager()
var didFindMyLocation = false

override func viewDidLoad() {
super.viewDidLoad()

var camera = GMSCameraPosition.cameraWithLatitude(33.7,
longitude: -77.4, zoom: 8.0)
mapView.camera = camera

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)

// Do any additional setup after loading the view.
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.myLocationEnabled = true
}
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
mapView.settings.myLocationButton = true

didFindMyLocation = true
}
}

最佳答案

如果之前设置了位置权限 func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) 可能不会被调用,因为授权状态没有改变。

您也可以只手动执行授权检查,然后在 map 上启用该位置

if CLLocationManager.locationServicesEnabled()
&& CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.myLocationEnabled = true
}

Checking authorization status documentation

关于swift - 谷歌地图不显示当前位置或定位按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285430/

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