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 kilometers;
int distance = [kilometers intValue];
but its giving error.
help guys
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 kilometers;
int distance = [kilometers intValue];
but its giving error.
help guys
distanceFromLocation:
Returns the distance (in meters) from the receiver’s location to the specified location.
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Parameters
location The other location.
Return Value The distance (in meters) between the two locations.
return type is double not int. And its not an NSNumber.
As of iOS 10, there's a new Measurement class which makes converting distances a snap.
Here's an extension to convert distances:
extension Double {
func convert(from originalUnit: UnitLength, to convertedUnit: UnitLength) -> Double {
return Measurement(value: self, unit: originalUnit).converted(to: convertedUnit).value
}
}
Example usage:
let miles = 1.0
let metersInAMile = miles.convert(from: .miles, to: .meters)
let mileInFeet = 5280.0
let feetInAMile = mileInFeet.convert(from: .feet, to: .miles)
let marathonInFeet = 138_435.0
let milesInMarathon = marathonInFeet.convert(from: .feet, to: .miles)
let kmInMarathon = marathonInFeet.convert(from: .feet, to: .kilometers)
let inch = 1.0
let centimetersInInch = inch.convert(from: .inches, to: .centimeters)
This will calculate the distance between two location by using the CLLocationDistance. By default it gives the values in meters. To convert to kilometers, divide the CLLocationDistance/1000.0. To get miles multiply the CLLocationDistance*0.62137.
//Pass your current location Latitude and longitude
CLLocation *currentlocation = [[CLLocation alloc] initWithLatitude:13.015370f longitude:80.200393f];
//Pass your destination location Latitude and longitude
CLLocation *destination = [[CLLocation alloc] initWithLatitude:37.420948f longitude:-122.09578499999998f];
CLLocationDistance meters = [currentlocation distanceFromLocation:destination];
//get distance in meters
NSLog(@"meters :%f", meters);
//get distance in kilometers
CLLocationDistance kilometers = meters / 1000.0;
NSLog(@"kilometers :%f", kilometers);
//Get distance in miles
NSLog(@"Miles:%f",kilometers*0.62137);
//convert kilometers to int value
int num=kilometers;
SWIFT 4
Late to this party, but you can also create an extension like this:
CLLocationDistance.swift
import UIKit
import CoreLocation
extension CLLocationDistance {
func inMiles() -> CLLocationDistance {
return self*0.00062137
}
func inKilometers() -> CLLocationDistance {
return self/1000
}
}
You could call it as follow:
distance.inKilometers()
I hope this helps anyone stumbling on this!
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!
}
}
It's so much easier in Swift:
// Use you CLLocationDistance as the value 'kilometers':
let kms = Measurement(value: kilometers, unit: UnitLength.kilometers)
// Then you can convert that to miles if you'd like:
let miles = kms.converted(to: .miles)
// Get the double value:
let doubleValue = miles.value
print("The distances is \(doubleValue) miles")