gpt4 book ai didi

ios - 找不到谷歌地图 mapview.myLocation (iOS SDK)

转载 作者:行者123 更新时间:2023-11-28 21:35:55 33 4
gpt4 key购买 nike

如何使用 Google Maps iOS SDK 在 map 上显示用户的位置?

我试图在 self.myMapView(MKMapView) 中找到 .myLocation 属性,但找不到。

任何人都可以逐步向我解释如何在 map 上显示我的用户位置吗?

最佳答案

像这样为 GMSMapView 和 CLLocationManager 创建属性

@IBOutlet weak var mapView: GMSMapView!

var locationManager = CLLocationManager()

var didFindMyLocation = false

并在 viewdidload 方法中为 map View 添加观察者以进行位置更新,如下所示

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()

let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(48.857165, longitude: 2.354613, zoom: 2.0)
mapView.camera = camera
mapView.delegate = self

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

}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : 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
}
}

//标记:- CLLocationManagerDelegate

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

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true

mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

locationManager.stopUpdatingLocation()
}

}
}

didFindMyLocation 是 bool 属性,也不要忘记将 'NSLocationWhenInUseUsageDescription' 键添加到 Info.plist 文件中

enter image description here

关于ios - 找不到谷歌地图 mapview.myLocation (iOS SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33936598/

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