【问题标题】:getting user location and creating map not in sync获取用户位置和创建地图不同步
【发布时间】:2015-06-14 06:35:45
【问题描述】:

以下代码获取用户当前位置并创建地图框地图。 问题是在获取用户位置之前正在创建 mapbox 地图。 如何减慢或同步此过程? 提前谢谢你,

  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

【问题讨论】:

    标签: ios swift mapbox


    【解决方案1】:

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

    【讨论】:

    • 我尝试在 viewDidLoad 中移动 self.createMapBoxMap()。但是在获取用户位置之前仍在创建地图。 @rb612
    • @LuAndre 你有没有在 self.userLocation = locations[0] 之后添加它! CLLocation?
    • 我将它移到了位置管理器中,现在可以正常工作了。我把它放在 viewdidload 里面,但不在位置管理器函数里面。那会有多不同?抱歉,我是 IOS 新手。
    • @LuAndre 没问题!很高兴听到你让它工作。您是否将其添加到 didUpdateLocations 以使其正常工作?原因是因为以前,它会这样:“好的,我将尝试获取用户的位置,但是当我这样做时,我将制作地图。”所以它在你的 CLLocation 变量中使用旧值,因为地图创建得太早了。现在,它只会在您存储位置后创建。
    • @LuAndre 作为高级步骤,一旦您开始了解有关 Grand Central Dispatch 和多线程的更多信息,请查看如何向此代码添加回调以更新位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2013-01-16
    • 1970-01-01
    相关资源
    最近更新 更多