我转换为Swift4。我的tableview从firebase中提取数据并按与用户位置的距离对其进行组织,但该视图已停止工作。问题是我的用户位置一直保持零。我听说最近有人因为转换而在mapkit上遇到了麻烦,但是不确定这是否属于同一类别。因此,显示“让userLocation = CLLocation(latitude:latitude !,经度:经度!)”的行返回nil。谢谢。

ios - Swift 4 CLLocation用户位置经度和纬度-LMLPHP

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar

    locationManager.delegate = self
    locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    locationManager.stopUpdatingLocation()
    locationManager.requestAlwaysAuthorization()

    getTableViewData()

    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresher.addTarget(self, action: #selector(RegisteredLocations.handleRefresh(refreshControl:)), for: UIControlEvents.valueChanged)
    tableView.addSubview(refresher)
}

func getTableViewData() {
    Database.database().reference().child("Businesses").queryOrdered(byChild: "businessName").observe(.childAdded, with: { (snapshot) in

        let key = snapshot.key

        if(key == self.loggedInUser?.uid) {
            print("Same as logged in user, so don't show!")
        } else {
            if let locationValue = snapshot.value as? [String: AnyObject] {
                let lat = Double(locationValue["businessLatitude"] as! String)
                let long = Double(locationValue["businessLongitude"] as! String)
                let businessLocation = CLLocation(latitude: lat!, longitude: long!)

                let latitude = self.locationManager.location?.coordinate.latitude
                let longitude = self.locationManager.location?.coordinate.longitude
                let userLocation = CLLocation(latitude: latitude!, longitude: longitude!)

                let distanceInMeters: Double = userLocation.distance(from: businessLocation)
                let distanceInMiles: Double = distanceInMeters * 0.00062137
                let distanceLabelText = String(format: "%.2f miles away", distanceInMiles)

                var singleChildDictionary = locationValue
                singleChildDictionary["distanceLabelText"] = distanceLabelText as AnyObject
                singleChildDictionary["distanceInMiles"] = distanceInMiles as AnyObject
                self.usersArray.append(singleChildDictionary as NSDictionary)
                self.usersArray = self.usersArray.sorted {
                    !($0?["distanceInMiles"] as! Double > $1?["distanceInMiles"] as! Double)
                }

            }
            //insert the rows
            //self.followUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
            self.followUsersTableView.reloadData()
        }

    }) { (error) in
        print(error.localizedDescription)
    }

}

最佳答案

调用locationManager.stopUpdatingLocation()之后,您将立即调用locationManager.startUpdatingLocation()

这将无法使用,因为您需要留出时间来确定位置。在收到对didUpdateLocations委托方法的调用之前,不要使用位置。

如果您的目标是iOS 9及更高版本,并且只需要一个位置,则应调用requestLocation()而不是startUpdatingLocation(),但仍需要等待对委托方法的回调。

收到位置后,即可加载表格数据。

关于ios - Swift 4 CLLocation用户位置经度和纬度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46480633/

10-12 00:06
查看更多