如何求坐标之间的距离?

时间:2021-11-26 15:24:09

I want to make it so that it will show the amount of distance between two CLLocation coordinates. Is there someway to do this without a complex math formula? If there isn't how would you do it with a formula?

我想让它表示两个CLLocation坐标之间的距离。有没有什么方法可以不用复杂的数学公式来做呢?如果没有公式怎么做呢?

5 个解决方案

#1


118  

CLLocation has a distanceFromLocation method so given two CLLocations:

CLLocation有一个距离的位置方法,所以有两个CLLocation:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];

or in Swift 4:

或者在斯威夫特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

you get here distance in meter so 1 miles = 1609 meter

这里的距离是米1英里= 1609米

if(distanceInMeters <= 1609)
 {
 // under 1 mile
 }
 else
{
 // out of 1 mile
 }

#2


5  

Try this out:

试试这个:

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

From Apple Documentation:

从苹果公司文档:

Return Value: The distance (in meters) between the two locations.

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

#3


1  

For objective-c

为objective - c

You can use distanceFromLocation to find the distance between two coordinates.

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

Code Snippets:

代码片段:

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

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

CLLocationDistance distance = [loc1 distanceFromLocation:loc2];

Your output will come in meters.

你的输出将以米为单位。

#4


0  

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


0  

Swift 4.1

斯威夫特4.1

import CoreLocation

进口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))

#1


118  

CLLocation has a distanceFromLocation method so given two CLLocations:

CLLocation有一个距离的位置方法,所以有两个CLLocation:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];

or in Swift 4:

或者在斯威夫特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

you get here distance in meter so 1 miles = 1609 meter

这里的距离是米1英里= 1609米

if(distanceInMeters <= 1609)
 {
 // under 1 mile
 }
 else
{
 // out of 1 mile
 }

#2


5  

Try this out:

试试这个:

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

From Apple Documentation:

从苹果公司文档:

Return Value: The distance (in meters) between the two locations.

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

#3


1  

For objective-c

为objective - c

You can use distanceFromLocation to find the distance between two coordinates.

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

Code Snippets:

代码片段:

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

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

CLLocationDistance distance = [loc1 distanceFromLocation:loc2];

Your output will come in meters.

你的输出将以米为单位。

#4


0  

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


0  

Swift 4.1

斯威夫特4.1

import CoreLocation

进口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))