cou*_*ron 11 iphone objective-c core-location cllocationmanager ios
我有两个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];
Run Code Online (Sandbox Code Playgroud)
基本上,iOS库中有什么东西可以将距离转换为漂亮的字符串,还是我必须手动执行此操作?
Kla*_*aas 44
iOS 7(和OS X 10.9)引入了MKDistanceFormatter,它可以满足您的需求:
CLLocationDistance distance = 1320.0;
MKDistanceFormatter *df = [[MKDistanceFormatter alloc]init];
df.unitStyle = MKDistanceFormatterUnitStyleFull;
NSString *prettyString = [df stringFromDistance:distance];
Run Code Online (Sandbox Code Playgroud)
Swift版本:
let distance = 1320.0
let df = MKDistanceFormatter()
df.unitStyle = .Full
let prettyString = df.stringFromDistance(distance)
Run Code Online (Sandbox Code Playgroud)
Ale*_*der 18
我只想注意一下,就像iOS8的发布一样,NSFormatters这种问题将会出现新的问题.
将会有NSLengthFormatter,NSMassFormatter并且NSEnergyFormatter非常容易使用 - 请参阅此iOS8 NSFormatter教程.下面是使用带有swift的LengthFormatter的示例:
let lengthFormatter = LengthFormatter()
print("Kilometer: ", lengthFormatter.string(fromValue:1000, unit: .kilometer)) //Kilometer: 1,000 km
Run Code Online (Sandbox Code Playgroud)
与DateFormatter一样,您不再需要使用NSLocale打印设备本身.
Nic*_*idt 12
不幸的是,您必须手动执行此操作.
如果您这样做,请不要忘记检查:
[[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue]
Run Code Online (Sandbox Code Playgroud)
世界其他国家都会欣赏.
| 归档时间: |
|
| 查看次数: |
4523 次 |
| 最近记录: |