summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLDistanceFormatter.m
blob: a7a2f9c9e17c546a8ec1efa314377ed8960afea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#import "MGLDistanceFormatter.h"

@interface MGLDistanceFormatter()
@end

@implementation MGLDistanceFormatter

static const CLLocationDistance METERS_PER_MILE = 1609.344;
static const double YARDS_PER_MILE = 1760.0;
static const double FEET_PER_MILE = YARDS_PER_MILE * 3.0;

- (NSString *)stringFromDistance:(CLLocationDistance)distance {
    double miles = distance / METERS_PER_MILE;
    double feet = miles * FEET_PER_MILE;
    
    NSLengthFormatterUnit unit = NSLengthFormatterUnitMillimeter;
    [self unitStringFromMeters:distance usedUnit:&unit];
    
    self.numberFormatter.roundingIncrement = @0.25;
    
    if (unit == NSLengthFormatterUnitYard) {
        if (miles > 0.2) {
            unit = NSLengthFormatterUnitMile;
            return [self stringFromValue:miles unit:unit];
        } else {
            unit = NSLengthFormatterUnitFoot;
            self.numberFormatter.roundingIncrement = @1;
            return [self stringFromValue:feet unit:unit];
        }
    } else {
        return [self stringFromMeters:distance];
    }
}

@end