gpt4 book ai didi

ios - 使用 mapKit Swift 显示与用户的距离(英里/公里)

转载 作者:搜寻专家 更新时间:2023-11-01 07:08:15 26 4
gpt4 key购买 nike

我一直在尝试在 tableView 上显示距离,但无法实现。这个问题来自这个问题:CLLocationDistance conversion .我检查了距离。在我的 Location 类中使用此函数:

// Get distance
func distance(to location: CLLocation) -> CLLocationDistance {
return location.distance(from: self.location)
}

我如何获取用户当前位置:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]

let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapView.setRegion(region, animated: true)

// Add a lastUserLocation to LocationManager and update it every time that the delegate receives a new location
LocationManager.shared.lastUserLocation = locations.last
LocationManager.shared.sortLocationsInPlace()

self.mapView.showsUserLocation = true

}

LocationManager 中的排序函数:

func getSortedLocations(userLocation: CLLocation) -> [Location] {
return locations.sorted { (l1, l2) -> Bool in
return l1.distance(to: userLocation) < l2.distance(to: userLocation)
}
}

func sortLocationsInPlace() {
if let validLocation = lastUserLocation {
locations.sort { (l1, l2) -> Bool in
return l1.distance(to: validLocation) < l2.distance(to: validLocation)
}
}
}

cellForRowAt:

var sortedLocations = [Location]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)


let location = sortedLocations[indexPath.row]

cell.textLabel?.text = location.name
return cell
}

更新

Location 类中:

class Location {

var name: String
var latitude: Double
var longitude: Double
var location:CLLocation {
return CLLocation(latitude: latitude, longitude: longitude)
}

init?(json: JSON) {

guard let name = json["name"] as? String, let latitude = json["latitude"] as? Double, let longitude = json["longitude"] as? Double else { return nil }
self.name = name
self.latitude = latitude
self.longitude = longitude
}

func distance(to location: CLLocation) -> CLLocationDistance {
return location.distance(from: self.location)
}
}

最佳答案

考虑到您的代码,我做了一些假设:

  1. 你的 sortedLocations数组具有您从 JSON 或其他任何文件中提取的不同位置。
  2. 您调用 startUpdatingLocation()或在加载数据之前在某处进行类似操作。
  3. 您在 didUpdateLocations接收更新 .
  4. 你的 LocationManager在名为 locations 的变量中保存所有位置的有序副本,您在里面订购的那个 didUpdateLocations .

考虑到,我知道你想做的是显示你的 sortedLocations根据引用位置排序。

缺少的是更新您的 UITableView收到您的用户位置后的数据。您有两个主要选择:

  1. 只加载您的 UITableView一旦您已经通过 didUpdateLocations 检索到您的第一个用户位置.
  2. 强制UITableView找到新位置后,请调用 tableView.reloadData() 进行更新里面didUpdateLocations .这将在您每次收到位置更新时重新绘制您的列表,并按位置对它们进行排序。

但是,在任何一种情况下,您都需要更换您的 cellForRow文本显示您的距离而不是 location.name :

// Distance in meters
cell.textLabel?.text = String(location.distance(to: LocationManager.shared.lastUserLocation!))

// Distance in miles
cell.textLabel?.text = String(location.distance(to: LocationManager.shared.lastUserLocation!)*0.00062137)

更新您的didUpdateLocations :

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {

let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapView.setRegion(region, animated: true)

// Add a lastUserLocation to LocationManager and update it every time that the delegate receives a new location
LocationManager.shared.lastUserLocation = location
LocationManager.shared.sortLocationsInPlace()

sortedLocations = LocationManager.shared.locations
tableView.reloadData()

self.mapView.showsUserLocation = true
}
}

使用您当前的代码,您正在将所有距离与 self.location 进行比较显然它没有在任何地方初始化的变量。

关于ios - 使用 mapKit Swift 显示与用户的距离(英里/公里),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46761442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com