distanceFromLocation - Calculate distance between two points

后端 未结 7 1074
礼貌的吻别
礼貌的吻别 2020-11-29 21:27

Just a quick question on Core Location, I\'m trying to calculate the distance between two points, code is below:

    -(void)locationChange:(CLLocation *)newL         


        
7条回答
  •  抹茶落季
    2020-11-29 21:49

    In Swift

    Let's create a method function that calculates distance between two locations:

     func distanceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
    
            var distanceMeters = source.distanceFromLocation(destination)
            var distanceKM = distanceMeters / 1000
            let roundedTwoDigit = distanceKM.roundedTwoDigit
            return roundedTwoDigit
    
        }
    

    If you want only two digits:

    extension Double{
    
        var roundedTwoDigit:Double{
    
            return Double(round(100*self)/100)
    
            }
        }
    

提交回复
热议问题