I am using the NSLengthFormatter class to format the distance between the user and some destination.
CLLocation *userLocation; //<- Coordinates fetched from CLLocationManager
CLLocation *targetLocation; //<- Some location retrieved from server data
CLLocationDistance distance = [userLocation distanceFromLocation:targetLocation];
NSLengthFormatter *lengthFormatter = [NSLengthFormatter new];
NSString *formattedLength = [lengthFormatter stringFromMeters:distance];
Now, if the length is less than 1000 meters, the formatted distance is always shown in yards or meters (depending on the locale).
Eg. if distance = 450.0, the formatted string will be 492.7 yd or 450 m.
How can I tweak NSLengthFormatter to return the distance strings in miles/kilometers only?
numberFormatter
and playing around with the different properties of NSNumberFormatter – AlidiaNSNumberFormatter
. – Naked