i have distance in a variable of type CLLocationDistance i need to convert it in a integer variable how can i do it
i have use
CLLocationDistance kil
Swift:
var initialLocation :CLLocation?
var updatedUserLocation :CLLocation?
var distanceBetweenLocations: CLLocationDistance?
//MK MapView Delegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
updatedUserLocation = locations.last
distanceBetweenLocations = updatedUserLocation!.distanceFromLocation(initialLocation!)
//convert To Miles
distanceBetweenLocations = Utility.convertCLLocationDistanceToMiles(distanceBetweenLocations)
//Setting Distance Value
distanceLabel.text = String(format: " Distance : %.2f ", distanceBetweenLocations!)
}
Note : I have class named Utility which serves the common class methods across the project . This is for better code reusability and reduction.
// Utility.swift
import UIKit
import Foundation
import CoreLocation
class Utility {
class func convertCLLocationDistanceToMiles (var targetDistance : CLLocationDistance?) -> CLLocationDistance {
targetDistance = targetDistance!*0.00062137
return targetDistance!
}
class func convertCLLocationDistanceToKiloMeters (var targetDistance : CLLocationDistance?) -> CLLocationDistance {
targetDistance = targetDistance!/1000
return targetDistance!
}
}