我的英里距离计算在 swift 3 中似乎是错误的

My distance in miles calculation seems wrong in swift 3

我有用户的当前位置和特定位置。我正在尝试以英里为单位计算距离。我的英里距离似乎不对,我在我的应用程序上得到了 1.2 英里,然后我在 google 地图中查看了英里数。我在 google 地图上行驶了 2.1 英里。这是我的代码,我不确定为什么我的代码不准确。请帮忙。

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    map.delegate = self
    map.userTrackingMode = MKUserTrackingMode.follow

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    // set location
    let latitude: CLLocationDegrees = 32.5850
    let longitude: CLLocationDegrees = -85.4904
    let latDelta: CLLocationDegrees = 0.05
    let lonDelta: CLLocationDegrees = 0.05
    let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
    let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    let region = MKCoordinateRegion(center: coordinates, span: span)

    map.setRegion(region, animated: true)

    // add annotation 
    let annotation = MKPointAnnotation()
    annotation.title = "1100 S College St"
    annotation.coordinate = coordinates

    map.addAnnotation(annotation)

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

    let userLocation: CLLocation = locations[0]
    let latitude = userLocation.coordinate.latitude
    let longitude = userLocation.coordinate.longitude

    let location1 = CLLocation(latitude: 32.5850, longitude: -85.4904) // set location

    let location2 = CLLocation(latitude: latitude, longitude: longitude) // user's current location

    let distanceInMeters : CLLocationDistance = location1.distance(from: location2) // distance in meters

    let distanceInMiles = distanceInMeters/1609.344  // distance in miles

    let roundUp = String(format:"%.1f", distanceInMiles) // round up

    distanceLabel.text = "\(roundUp) mi"

}

这部分是正确的:

let distanceInMiles = distanceInMeters/1609.344  // distance in miles

检查距离(以米为单位)并与 Google 地图进行比较。 如果您在 iOS 10 中,您可以像这样使用新的 Measurement 和相关的 类:

let meters = 1000.0

let distanceMeters = Measurement(value: meters, unit: UnitLength.meters)

let distanceMiles = distanceMeters.converted(to: UnitLength.miles)
let miles = distanceMeters.converted(to: UnitLength.miles).value

此代码产生以下结果:

Swift 4 , XCode9

extension CLLocation {

    func myDistance(coordinate: CLLocation)->String{

        let distanceInMeters = self.distance(from: coordinate)

        if distanceInMeters < 1000 {
            return distanceInMeters.description + " M"
        }else{
            let distanceInKilometer = distanceInMeters /  1000
            return String(format:"%.1f", distanceInKilometer) + " KM"
        }

    }

}