Swift MKMapItems距用户位置的距离

Swift MKMapItems距用户位置的距离,swift,mapkit,cllocationmanager,mkmapitem,mkplacemark,Swift,Mapkit,Cllocationmanager,Mkmapitem,Mkplacemark,有人能帮我理解我需要添加什么来获得用户位置和地点标记之间的距离吗?我只是不确定如何更改distanceLabel.text中的内容,以便将数字更改为每个位置标记。谢谢大家! import UIKit import MapKit class ListedMapTableViewController: UITableViewController, CLLocationManagerDelegate { var mapItems: [MKMapItem]! var userLocation = C

有人能帮我理解我需要添加什么来获得用户位置和地点标记之间的距离吗?我只是不确定如何更改distanceLabel.text中的内容,以便将数字更改为每个位置标记。谢谢大家!

import UIKit
import MapKit

class ListedMapTableViewController: UITableViewController, CLLocationManagerDelegate {

var mapItems: [MKMapItem]!
var userLocation = CLLocationManager()
let distanceFormatter = MKDistanceFormatter()


override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mapItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath) as! ListedTableViewCell

    // Configure the cell...
    let row = indexPath.row
    let item = mapItems[row]
    cell.nameLabel.text = item.name
    cell.detailLabel.text = item.phoneNumber
    let distanceInMeters : Double = self.userLocation.location!.distance(from: mapItems[row].placemark.location!)
    let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137)
    cell.distanceLabel.text = "\(distanceInMiles.string(2)) miles away"

    return cell
}

 }


extension String {
var doubleValue: Double {
    return (self as NSString).doubleValue
}
}

//formats a double's decimal places
extension Double {
func string(_ fractionDigits:Int) -> String {
    let formatter = NumberFormatter()
    formatter.minimumFractionDigits = fractionDigits
    formatter.maximumFractionDigits = fractionDigits
    return formatter.string(from: NSNumber(value: fractionDigits))!
}
}

CLLocation具有distanceFromLocation方法,因此给定了两个CLLocation:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];
或在Swift 3中:

//当前位置操场名词:人们可以玩耍的地方

import CoreLocation


let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate₁ = CLLocation(latitude: 5.0, longitude: 3.0)

let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters
这里的距离以米为单位,所以1英里=1609米

if(distanceInMeters <= 1609)
 {
 // under 1 mile
 }
 else
{
 // out of 1 mile
 }
if(distanceInMeters只需更改NSNumber(值:self))!