gpt4 book ai didi

ios - 我应该如何在 Mapbox for iOS SDK 中使用 userLocation 找到正确的位置数据? (迅速)

转载 作者:可可西里 更新时间:2023-11-01 03:05:54 28 4
gpt4 key购买 nike

我试图找到用户位置的纬度和经度,以便我可以在 viewdidload 中将 map 以用户为中心。

我已经实现了看似正确的代码,但 userLat(纬度)和 userLon(经度)的值有很大偏差。

注意其他人和我有同样的问题,但他的回答从未得到解决: Mapbox iOS8 Swift mapView.showUsersLocation

import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate {

// let locationManager = CLLocationManager()

@IBOutlet weak var mapView: MGLMapView!

override func viewDidLoad() {
super.viewDidLoad()

// Initalise map's center coordinate as vancouver
mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 49.283382,
longitude: -123.117394),
zoomLevel: 15, animated: false)
view.addSubview(mapView)

// Set the delegate property of our map view to self after instantiating it.
mapView.delegate = self

// User location
mapView.showsUserLocation = true

let userLoc = mapView.userLocation!
userLoc.title = "Hello"
userLoc.subtitle = "I am here!"

let userLat = userLoc.coordinate.latitude
let userLon = userLoc.coordinate.longitude

print(userLat, userLon)

/*
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}*/

}

func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}

}

结果打印:

3.40282346638529e+38 3.40282346638529e+38

奇怪的是,注释工作正常,当我点击我的位置时,我得到了标题和副标题。

最佳答案

将 map 以用户位置为中心的最简单方法是设置 MGLMapView.userTrackingMode = .follow( objective-c 中的 MGLUserTrackingModeFollow)。这将在位置可用时自动移动 map 。

您看到 MGLMapView.userLocation 的虚假数字的原因是用户的位置通常在 viewDidLoad 中不可用。使用 mapView:didUpdateUserLocation:委托(delegate)方法在用户位置可用和更新时收到通知。

关于ios - 我应该如何在 Mapbox for iOS SDK 中使用 userLocation 找到正确的位置数据? (迅速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35234869/

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