summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
authorFredrik Karlsson <bjorn.fredrik.karlsson@gmail.com>2017-01-04 16:18:17 +0100
committerFredrik Karlsson <bjorn.fredrik.karlsson@gmail.com>2017-01-27 19:41:13 +0100
commit2ab3af4b1b5dabf7087e6fabfb84617f4ff4b55d (patch)
tree179312c48bf929914acec1ad9eafcbb2d7d2bd4e /platform/darwin
parent4d358260140c9d52103cc95631a8519b969bc408 (diff)
downloadqtlocation-mapboxgl-2ab3af4b1b5dabf7087e6fabfb84617f4ff4b55d.tar.gz
[ios, macos] added a distance formatter
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/src/MGLDistanceFormatter.h26
-rw-r--r--platform/darwin/src/MGLDistanceFormatter.m35
-rw-r--r--platform/darwin/test/MGLDistanceFormatterTests.m30
3 files changed, 91 insertions, 0 deletions
diff --git a/platform/darwin/src/MGLDistanceFormatter.h b/platform/darwin/src/MGLDistanceFormatter.h
new file mode 100644
index 0000000000..46aad9a940
--- /dev/null
+++ b/platform/darwin/src/MGLDistanceFormatter.h
@@ -0,0 +1,26 @@
+#import <Foundation/Foundation.h>
+#import <CoreLocation/CoreLocation.h>
+
+#import "MGLFoundation.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ `MGLDistanceFormatter` implements a formatter object meant to be used for
+ geographic distances. The user’s current locale will be used by default
+ but it can be overriden by changing the locale property of the numberFormatter.
+ */
+MGL_EXPORT
+@interface MGLDistanceFormatter : NSLengthFormatter
+
+/**
+ Returns a localized formatted string for the provided distance.
+
+ @param distance The distance, measured in meters.
+ @return A localized formatted distance string including units.
+ */
+- (NSString *)stringFromDistance:(CLLocationDistance)distance;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLDistanceFormatter.m b/platform/darwin/src/MGLDistanceFormatter.m
new file mode 100644
index 0000000000..e77e48b512
--- /dev/null
+++ b/platform/darwin/src/MGLDistanceFormatter.m
@@ -0,0 +1,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 = @50;
+ return [self stringFromValue:feet unit:unit];
+ }
+ } else {
+ return [self stringFromMeters:distance];
+ }
+}
+
+@end
diff --git a/platform/darwin/test/MGLDistanceFormatterTests.m b/platform/darwin/test/MGLDistanceFormatterTests.m
new file mode 100644
index 0000000000..f15ad9d313
--- /dev/null
+++ b/platform/darwin/test/MGLDistanceFormatterTests.m
@@ -0,0 +1,30 @@
+#import <Mapbox/Mapbox.h>
+#import <XCTest/XCTest.h>
+
+@interface MGLDistanceFormatterTests : XCTestCase
+
+@end
+
+@implementation MGLDistanceFormatterTests
+
+- (void)testAbbreviatedMetricUnits {
+ MGLDistanceFormatter *formatter = [[MGLDistanceFormatter alloc] init];
+ formatter.numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_CA"];
+ for (CLLocationDistance distance=0; distance <= 10000; distance+=5) {
+ NSString *unit = [[formatter stringFromDistance:distance] componentsSeparatedByString:@" "][1];
+ NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", @[@"mm", @"cm", @"m", @"km"]];
+ XCTAssert([predicate evaluateWithObject:unit], @"Should only contain metric units");
+ }
+}
+
+- (void)testAbbreviatedImperialUnits {
+ MGLDistanceFormatter *formatter = [[MGLDistanceFormatter alloc] init];
+ formatter.numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
+ for (CLLocationDistance distance=0; distance <= 10000; distance+=5) {
+ NSString *unit = [[formatter stringFromDistance:distance] componentsSeparatedByString:@" "][1];
+ NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", @[@"ft", @"mi"]];
+ XCTAssert([predicate evaluateWithObject:unit], @"Should only contain imperial units");
+ }
+}
+
+@end