快速获取位置然后加载表

问题描述 投票:1回答:4

[因此,我想在用户首次打开应用程序并然后加载表时询问用户位置。这是我目前的代码:

class LayoutController: UICollectionViewController, UICollectionViewDataSource,
UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, 
APIControllerProtocol, CLLocationManagerDelegate {


    @IBOutlet weak var chainTable: UICollectionView!
    @IBOutlet weak var navLogo: UIButton!
    let locationManager : CLLocationManager = CLLocationManager()

    var lat: String = "lat!"
    var long: String = "long!"


    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.sharedApplication().statusBarStyle = .LightContent
        navLogo.userInteractionEnabled = false

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){
            var currentLocation = locationManager.location
            var longitude = currentLocation.coordinate.longitude
            var latitude = currentLocation.coordinate.latitude

            long = String(stringInterpolationSegment: longitude)
            lat = String(stringInterpolationSegment: latitude)
        }
        // Do any additional setup after loading the view.            
        loadTable(lat, longitude, long)
    }

    func loadTable(latitude: String, longitude: String){
        api = APIController(delegate: self)
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        api!.getPosts(latitude, longitude: longitude)
    }
}

首次启用下载和启用位置后,数据无法正确加载。但是,当您重新启动应用程序时,我确实可以工作。如何在加载前确定表格的位置?谢谢。

((我已经使用if CLLocationManager.authorizationstatus()...块中的loadTable函数进行了尝试,也无济于事。)

swift uicollectionview cllocationmanager uicollectionviewcell cllocation
4个回答
2
投票

如@dimpiax所建议,您正在解决此错误。

位置管理器是异步API。一旦确定您有权使用它,就要求它开始更新您的位置。然后等待它调用您的委托方法locationManager:didUpdateLocations:

甚至您还没有完成。通常,您到达的第一个位置是旧的,而且非常非常错误。 (这是上次激活GPS时读取的位置。)您必须检查该位置上的时间戳,如果时间戳超过几秒钟,则将其忽略。

但是等等,您still还没有完成。一旦丢掉了可能过期的位置,就需要等到GPS稳定下来并开始提供体面的读数。在要求位置更新后的前5秒钟或更长时间内,读数的误差幅度通常非常差。您可以通过检查所获得的位置记录上的水平精度属性来解决此问题。该值实际上是半径。大数字不好,小数字好。您可能希望水平精度为<= 100米的读数。

一旦掌握了这些,就可以重新加载表格视图了。


2
投票

您需要监听CLLocationManagerDelegate的委托方法:

locationManager:didUpdateLocations:

在获得成功结果之后,设置并重新加载表视图的数据。


1
投票

感谢邓肯和this tutorial,我知道了>

func locationManager(管理员:CLLocationManager !, didUpdateLocations位置:[AnyObject]!){

    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in

        if (error != nil){
            println("Error:" + error.localizedDescription)
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as! CLPlacemark

            if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){
                var currentLocation = self.locationManager.location
                var longitude = currentLocation.coordinate.longitude
                var latitude = currentLocation.coordinate.latitude

                self.long = String(stringInterpolationSegment: longitude)
                self.lat = String(stringInterpolationSegment: latitude)
            }

            self.displayLocationInfo(pm)
            self.loadTable(self.lat, longitude: self.long)
        }else{
            println("error with data")
        }

    })

我将其添加到viewDidLoad的正下方,并将该Auth块移至其中。


0
投票

我创建了一个类来获取用户的当前位置并在一个块中返回它。

© www.soinside.com 2019 - 2024. All rights reserved.