How to find out distance between coordinates?

非 Y 不嫁゛ 提交于 2019-11-28 16:21:03
Glenn Howes

CLLocation has a distanceFromLocation method so given two CLLocations:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];

or in 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

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

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

Swift 4.1

import 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))

Try this out:

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

From Apple Documentation:

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

Vijay

For objective-c

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

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.

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"

    }
}

For swift 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"

Swift 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
    }

use to

let distance = calculateDistance("add parameters")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!