CLLocationDistance conversion

后端 未结 5 874
面向向阳花
面向向阳花 2021-01-12 02:51

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         


        
相关标签:
5条回答
  • 2021-01-12 03:16

    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;
    
    0 讨论(0)
  • 2021-01-12 03:19

    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!
      }
    
    }
    
    0 讨论(0)
  • 2021-01-12 03:29

    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!

    0 讨论(0)
  • 2021-01-12 03:30

    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)
    
    0 讨论(0)
  • 2021-01-12 03:37

    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.

    0 讨论(0)
提交回复
热议问题