如何找出坐标之间的距离?

时间:2015-10-23 14:04:57

标签: ios swift cllocation cllocationdistance

我想让它显示两个CLLocation坐标之间的距离。没有复杂的数学公式,有没有办法做到这一点?如果没有,你会怎么做公式?

10 个答案:

答案 0 :(得分:150)

CLLocation有一个distanceFromLocation方法,因此给出了两个CLLocations:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];

或在Swift 4中:

//: Playground - noun: a place where people can play

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
 }

答案 1 :(得分:9)

Swift 4.1

导入CoreLocation

    //My location
    let myLocation = CLLocation(latitude: 59.244696, longitude: 17.813868)

    //My buddy's location
    let myBuddysLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)

    //Measuring my distance to my buddy's (in km)
    let distance = myLocation.distance(from: myBuddysLocation) / 1000

    //Display the result in km
    print(String(format: "The distance to my buddy is %.01fkm", distance))

答案 2 :(得分:6)

试试这个:

distanceInMeters = fromLocation.distanceFromLocation(toLocation)
distanceInMiles = distanceInMeters/1609.344

来自Apple Documentation

  

返回值:两个位置之间的距离(以米为单位)。

答案 3 :(得分:1)

对于objective-c

您可以使用distanceFromLocation查找两个坐标之间的距离。

代码段:

CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];

CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];

CLLocationDistance distance = [loc1 distanceFromLocation:loc2];

您的输出将以米为单位。

答案 4 :(得分:1)

func calculateDistanceInMiles(){

    let coordinate₀ = CLLocation(latitude:34.54545, longitude:56.64646)
    let coordinate₁ = CLLocation(latitude: 28.4646, longitude:76.65464)
    let distanceInMeters = coordinate₀.distance(from: coordinate₁)
    if(distanceInMeters <= 1609)
    {
        let s =   String(format: "%.2f", distanceInMeters)
        self.fantasyDistanceLabel.text = s + " Miles"
    }
    else
    {
        let s =   String(format: "%.2f", distanceInMeters)
        self.fantasyDistanceLabel.text = s + " Miles"

    }
}

答案 5 :(得分:1)

快速4

   let locationOne = CLLocation(latitude: lat, longitude: long)
   let locationTwo = CLLocation(latitude: lat,longitude: long)

  let distance = locationOne.distance(from: locationTwo) * 0.000621371

  distanceLabel.text = "\(Int(round(distance))) mi"

答案 6 :(得分:0)

迅速5。

func calculateDistance(mobileLocationX:Double,mobileLocationY:Double,DestinationX:Double,DestinationY:Double) -> Double {

        let coordinate₀ = CLLocation(latitude: mobileLocationX, longitude: mobileLocationY)
        let coordinate₁ = CLLocation(latitude: DestinationX, longitude:  DestinationY)

        let distanceInMeters = coordinate₀.distance(from: coordinate₁)

        return distanceInMeters
    }

习惯

let distance = calculateDistance("add parameters")

答案 7 :(得分:0)

import UIKit
import CoreLocation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var currentLocation = CLLocation(latitude: 23.1929, longitude: 72.6156)
        var DestinationLocation = CLLocation(latitude: 23.0504, longitude: 72.4991)
        var distance = currentLocation.distance(from: DestinationLocation) / 1000
        print(String(format: "The distance to my buddy is %.01fkm", distance))
    }
}

答案 8 :(得分:0)

您也可以像使用HaversineDistance algorithm一样使用android开发人员,当您在android中有与此类似的其他应用程序时,这很有用,否则以上答案对您来说是正确的。

import UIKit

func haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double {

let haversin = { (angle: Double) -> Double in
    return (1 - cos(angle))/2
}

let ahaversin = { (angle: Double) -> Double in
    return 2*asin(sqrt(angle))
}

// Converts from degrees to radians
let dToR = { (angle: Double) -> Double in
    return (angle / 360) * 2 * .pi
}

let lat1 = dToR(la1)
let lon1 = dToR(lo1)
let lat2 = dToR(la2)
let lon2 = dToR(lo2)

return radius * ahaversin(haversin(lat2 - lat1) + cos(lat1) * cos(lat2) * haversin(lon2 - lon1))
}

let amsterdam = (52.3702, 4.8952)
let newYork = (40.7128, -74.0059)

// Google says it's 5857 km so our result is only off by 2km which could be due to all kinds of things, not sure how google calculates the distance or which latitude and longitude google uses to calculate the distance.
haversineDinstance(la1: amsterdam.0, lo1: amsterdam.1, la2: newYork.0, lo2: newYork.1)

我从refrence链接中选择了上面编写的代码 https://github.com/raywenderlich/swift-algorithm-club/blob/master/HaversineDistance/HaversineDistance.playground/Contents.swift

答案 9 :(得分:0)

import CoreLocation

//My location
let myLocation = CLLocation(latitude: 31.5101892, longitude: 74.3440842)

//My Next Destination
let myNextDestination = CLLocation(latitude: 33.7181584, longitude: 73.071358)

//Finding my distance to my next destination (in km)
let distance = myLocation.distance(from: myNextDestination) / 1000