CLLocationDistance 转换

Posted

技术标签:

【中文标题】CLLocationDistance 转换【英文标题】:CLLocationDistance conversion 【发布时间】:2011-02-03 15:40:42 【问题描述】:

我在 CLLocationDistance 类型的变量中有距离,我需要将其转换为整数变量我该怎么做

我用过

CLLocationDistance kilometers;

int distance = [kilometers intValue];

但它给出错误。

帮助大家

【问题讨论】:

您收到的错误可能对他人有帮助。请包括它。 【参考方案1】:

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

distanceFromLocation:

返回从接收器位置到指定位置的距离(以米为单位)。

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

参数

位置另一个位置。

返回值 两个位置之间的距离(以米为单位)。

返回类型是double 而不是int。而且它不是NSNumber

【讨论】:

澄清一下,因为它是一个 Double (一种内在类型,而不是一个对象),所以没有 [intValue] 方法可以运行。由于您愿意失去准确性,因此只需分配值。如果要四舍五入,只需添加 0.5 然后赋值即可。【参考方案2】:

ios 10 开始,有一个新的 Measurement 类可以让转换距离变得轻而易举。

这是一个转换距离的扩展:

extension Double 
  func convert(from originalUnit: UnitLength, to convertedUnit: UnitLength) -> Double 
    return Measurement(value: self, unit: originalUnit).converted(to: convertedUnit).value
  

示例用法:

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)

【讨论】:

【参考方案3】:

这将使用CLLocationDistance 计算两个位置之间的距离。默认情况下,它以米为单位给出值。要转换为公里,请除以CLLocationDistance/1000.0。要获得里程,请乘以 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;

【讨论】:

请添加解释以改进答案;不鼓励仅使用代码的答案!谢谢! 默认情况下使用 CLLocationDistance 计算两个纬度和经度之间的距离,它以米为单位给出值。要转换为公里,除以 CLLocationDistance/1000.0。要获得英里乘以 CLLocationDistance*0.62137【参考方案4】:

SWIFT 4

迟到了,但你也可以像这样创建一个扩展:

CLLocationDistance.swift

import UIKit
import CoreLocation

extension CLLocationDistance 
    func inMiles() -> CLLocationDistance 
        return self*0.00062137
    

    func inKilometers() -> CLLocationDistance 
        return self/1000
    

你可以这样称呼它:

distance.inKilometers()

我希望这可以帮助任何遇到此问题的人!

【讨论】:

【参考方案5】:

斯威夫特:

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

  

注意:我有一个名为 Utility 的类,它为项目中的通用类方法提供服务。这是为了更好的代码可重用性和减少。

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


【讨论】:

以上是关于CLLocationDistance 转换的主要内容,如果未能解决你的问题,请参考以下文章

向 CLLocationDistance 添加一些计算

CLLocationDistance,两个纬度和经度之间的英里和英尺给我iOS的错误值?

如何使用iPhone方法 - (CLLocationDistance)distanceFromLocation:(const CLLocation *)的位置

ddd

从 reverseGeocodeLocation 获取位置名称

CoreLocation:LocationManager.stopUpdatingLocation() 不工作