【发布时间】:2017-09-29 01:53:56
【问题描述】:
我转换为 Swift 4。从 firebase 提取数据并按与用户位置的距离进行组织的 tableview 已停止工作。问题是我的用户位置一直为零。我听说最近有人因为转换而在使用 mapkit 时遇到了麻烦,但不确定这是否属于同一类别。所以说“让 userLocation = CLLocation(纬度:纬度!,经度:经度!)”的行返回 nil。谢谢。
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)
}
}
【问题讨论】:
-
这些行返回什么: let latitude = self.locationManager.location?.coordinate.latitude let longitude = self.locationManager.location?.coordinate.longitude
-
我刚刚添加了一个断点的屏幕截图以及实际返回的用户位置的经纬度
-
谢谢!您的纬度和经度值看起来很奇怪。当应用程序第一次运行时,它是否要求获得使用该位置的权限。
-
您在拨打
locationManager.startUpdatingLocation()后立即拨打locationManager.stopUpdatingLocation()。您需要留出时间来确定位置,并且在接到didUpdateLocations的电话之前,您不应该使用位置。如果您的目标是 iOS 9 及更高版本并且您只需要一个位置,那么您应该调用requestLocation,但您仍然需要等待委托方法的回调。收到位置后,您就可以加载表格数据了。 -
您在调试器中看到的纬度和经度值是无效的,因为它们尚未分配;可以看到调试器还没有执行
let latitude=...语句
标签: ios swift cllocationmanager cllocation userlocation