9

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

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
Manish Jain
  • 865
  • 3
  • 13
  • 28

6 Answers6

15

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.

0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
  • To clarify, since it's a Double (an intrinsic type, not an object) there is no [intValue] method to run. Since you're willing to lose accuracy, just assign the value. If you want to round it, just add .5 and then assign it. – KevinDTimm Feb 03 '11 at 16:01
13

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)
Adrian
  • 16,233
  • 18
  • 112
  • 180
11

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;
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Kamalkumar.E
  • 254
  • 2
  • 11
  • 1
    Please add an explanation to improve the answer; code-only answers are discouraged! Thanks! – namezero Aug 10 '15 at 10:10
  • 1
    This to calculate distance between two latitude and longitude by using the CLLocationDistance by default it gives the values in meters. To convert to kilometer divide the CLLocationDistance/1000.0.To get miles multiply the CLLocationDistance*0.62137 – Kamalkumar.E Aug 10 '15 at 10:35
7

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!

Jeremie
  • 2,241
  • 2
  • 17
  • 25
4

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!
  }

}
Alvin George
  • 14,148
  • 92
  • 64
0

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")
Joshua Hart
  • 772
  • 1
  • 21
  • 31