Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationDistance conversion

Tags:

iphone

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

like image 968
Manish Jain Avatar asked Feb 03 '11 15:02

Manish Jain


2 Answers

https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html

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.

like image 110
0x8badf00d Avatar answered Nov 13 '22 17:11

0x8badf00d


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)
like image 44
Adrian Avatar answered Nov 13 '22 17:11

Adrian