在App上开始获取iOS中的位置

时间:2017-03-17 21:27:49

标签: ios swift swift3

以下是我的情景。

在应用程序启动时我第一次需要用户位置,以获取最接近他们的地点列表。

如果他们允许位置访问,那么我想使用他们的位置向他们展示地点列表。

我打电话来更新didUpdateLocation中的列表,但即使在第一次允许访问后,列表也始终为空。当我第二次打开应用程序时,该位置可用,我有坐标,可以继续显示有效的列表。

我还尝试运行代码来填充这两个委托方法中的列表:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus){ 
  // switch statements then          
  fectchPlacesAndPopulateList()
}


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        print("locationManager")
        Utils.creatErrorMessage(title: "aaa", message: "aaaa", vc: self)

        let userLocation:CLLocation = locations[0] as! CLLocation
        let lng = userLocation.coordinate.longitude;
        let lat = userLocation.coordinate.latitude;
        let coordinates = "\(lat),\(lng)"
        print("update locations = \(coordinates)")
        fectchPlacesAndPopulateList()
    }

 var locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()


 if CLLocationManager.locationServicesEnabled() {


                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
                locationManager.startUpdatingLocation()
}
}

1 个答案:

答案 0 :(得分:1)

对于Swift 3,didUpdateLocations的签名是错误的,请注意前导下划线,经理的(非可选)类型和返回类型

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])