gpt4 book ai didi

ios - 获取用户位置和创建 map 不同步

转载 作者:行者123 更新时间:2023-11-28 07:05:14 24 4
gpt4 key购买 nike

以下代码获取用户当前位置并创建 mapbox map 。问题是在获取用户位置之前正在创建 mapbox map 。我怎样才能减慢或同步这个过程?提前谢谢你,

  import UIKit
import CoreLocation
import MapboxGL

class AViewController: UIViewController, CLLocationManagerDelegate {
var manager:CLLocationManager!
var userLocation:CLLocation = CLLocation(latitude: 25.776243, longitude: -80.136509)

override func viewDidLoad() {
super.viewDidLoad()

println("inside viewdidload")
self.getUserLocation()
}//eom

override func viewDidAppear(animated: Bool) {
println("inside viewdidappear")
self.createMapBoxMap()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*** MapBox Functions ************************************************************************/
/*Create preliminary map */
func createMapBoxMap(){
// set your access token
let mapView = MGLMapView(frame: view.bounds, accessToken: "pk.eyJ1IjoiZGFya2ZhZGVyIiwiYSI6IlplVDhfR3MifQ.pPEz732qS8g0WEScdItakg")

mapView.autoresizingMask = .FlexibleWidth | .FlexibleHeight

// set the map's center coordinate
mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: self.userLocation.coordinate.latitude, longitude: self.userLocation.coordinate.longitude),
zoomLevel: 13, animated: false)
view.addSubview(mapView)

//showing the user location on map - blue dot
mapView.showsUserLocation = true
}//eom


/*** location Functions ************************************************************************/
/*getting user current location*/
func getUserLocation(){
self.manager = CLLocationManager()
self.manager.delegate = self
self.manager.desiredAccuracy = kCLLocationAccuracyBest
self.manager.requestWhenInUseAuthorization()
self.manager.startUpdatingLocation()
}//eom

/*location manager 'didUpdateLocations' function */
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
self.manager.stopUpdatingLocation() //stop getting user location
println(locations)

self.userLocation = locations[0] as! CLLocation

}//eom

/* errors occurred */
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
println("Error:" + error.localizedDescription)
}//eom

}//eoc

最佳答案

位置管理器正在异步运行(预期行为),这意味着它会立即返回并在调用创建 map 框方法后立即完成 map 创建。但是,这并不意味着 map 已经找到了位置。我认为实现此目的的最佳方法是将 self.createMapBoxMap() 移动到 didUpdateLocations 内部并尝试这样做。根据我的经验,您希望 View 的创建发生在像这样的异步方法的回调中。不过,您可能想要某种类型的加载 View ,因为用户一开始会感到困惑。

关于ios - 获取用户位置和创建 map 不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30826602/

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