How to find out distance between coordinates?

前端 未结 10 1187
猫巷女王i
猫巷女王i 2020-12-12 15:23

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

相关标签:
10条回答
  • 2020-12-12 16:02
    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))
        }
    }
    
    0 讨论(0)
  • 2020-12-12 16:05

    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")
    
    0 讨论(0)
  • 2020-12-12 16:06
    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 讨论(0)
  • 2020-12-12 16:08
    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
    
    0 讨论(0)
  • 2020-12-12 16:11

    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))
    
    0 讨论(0)
  • 2020-12-12 16:12

    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
     }
    
    0 讨论(0)
提交回复
热议问题