gpt4 book ai didi

iphone - 适用于 iOS 的 pretty-print 距离

转载 作者:技术小花猫 更新时间:2023-10-29 10:40:30 25 4
gpt4 key购买 nike

我有两个 CLLocation 对象,我希望以一种漂亮的格式显示它们之间的距离。比如“22 英尺”、“58 码”或“2.3 英里”

CLLocation *location1 = [[CLLocation alloc] initWithLatitude:42.333297 longitude:-71.588858];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:42.353297 longitude:-71.578858];

float target = [location1 getDistanceFrom:location2];

基本上,iOS 库中是否有任何东西可以将距离转换为漂亮的字符串,还是我必须手动执行此操作?

最佳答案

引入了 iOS 7(和 OS X 10.9)MKDistanceFormatter这正是您所需要的:

CLLocationDistance distance = 1320.0;
MKDistanceFormatter *df = [[MKDistanceFormatter alloc]init];
df.unitStyle = MKDistanceFormatterUnitStyleFull;

NSString *prettyString = [df stringFromDistance:distance];

Swift 版本:

let distance = 1320.0
let df = MKDistanceFormatter()
df.unitStyle = .Full

let prettyString = df.stringFromDistance(distance)

swift 5.4.2:

import MapKit

let distance = 1320.0
let df = MKDistanceFormatter()
df.unitStyle = .full
let prettyString = df.string(fromDistance: distance)

关于iphone - 适用于 iOS 的 pretty-print 距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com