How to find out distance between coordinates?

前端 未结 10 1186
猫巷女王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: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))
    

提交回复
热议问题