Cómo averiguar la distancia entre las coordenadas?


Quiero hacerlo para que muestre la cantidad de distancia entre dos coordenadas de CLLocation. ¿Hay alguna manera de hacer esto sin una fórmula matemática compleja? Si no hay cómo lo harías con una fórmula?

Author: rmaddy, 2015-10-23

5 answers

CLLocation tiene un método distanceFromLocation así que dado dos CLLocations:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];

O en 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

Aquí distancia{[9] {} en[8]}medidor tan 1 millas = 1609 medidor

if(distanceInMeters <= 1609)
 {
 // under 1 mile
 }
 else
{
 // out of 1 mile
 }
 127
Author: Glenn Howes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-20 15:13:44

Prueba esto:

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

De Documentación de Apple :

Valor de retorno: La distancia (en metros) entre las dos ubicaciones.

 5
Author: Abhinav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-23 14:15:21

Para el objetivo-c

Puedes usar distanceFromLocation para encontrar la distancia entre dos coordenadas.

Fragmentos de código:

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

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

CLLocationDistance distance = [loc1 distanceFromLocation:loc2];

Su salida vendrá en metros.

 1
Author: Vijay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-06 11:20:12

Swift 4.1

Importar 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
Author: Mohammed Abunada,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-21 12:46:52
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"

    }
}
 0
Author: Saurabh Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-27 11:44:15