summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Wray <jason@mapbox.com>2016-10-25 16:35:38 -0700
committerJason Wray <friedbunny@users.noreply.github.com>2016-10-28 18:02:53 -0700
commit2242e5484cc81ef00b3cbf68d98dbcb8cdc16496 (patch)
tree3535cf3208a9dc20802dd62c10636df5cb3f26d0
parentd5af9b680b6b72eea7cd149a4552bd20bb676353 (diff)
downloadqtlocation-mapboxgl-2242e5484cc81ef00b3cbf68d98dbcb8cdc16496.tar.gz
[ios] Add country label language toggle demo
- Select best language based on locale setting and our support.
-rw-r--r--platform/ios/app/MBXViewController.m75
1 files changed, 74 insertions, 1 deletions
diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m
index 74f8b5f28a..a5503f38ed 100644
--- a/platform/ios/app/MBXViewController.m
+++ b/platform/ios/app/MBXViewController.m
@@ -70,6 +70,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) {
MBXSettingsRuntimeStylingUpdateGeoJSONSourceFeatures,
MBXSettingsRuntimeStylingVectorSource,
MBXSettingsRuntimeStylingRasterSource,
+ MBXSettingsRuntimeStylingCountryLabels,
};
typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
@@ -105,6 +106,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
@property (nonatomic) NSInteger styleIndex;
@property (nonatomic) BOOL debugLoggingEnabled;
@property (nonatomic) BOOL customUserLocationAnnnotationEnabled;
+@property (nonatomic) BOOL usingLocaleBasedCountryLabels;
@end
@@ -322,6 +324,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
@"Update GeoJSON Source: Features",
@"Style Vector Source",
@"Style Raster Source",
+ [NSString stringWithFormat:@"Label Countries in %@", (_usingLocaleBasedCountryLabels ? @"Local Language" : [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[self bestLanguageForUser]])],
]];
break;
case MBXSettingsMiscellaneous:
@@ -474,6 +477,9 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
case MBXSettingsRuntimeStylingRasterSource:
[self styleRasterSource];
break;
+ case MBXSettingsRuntimeStylingCountryLabels:
+ [self styleCountryLabelsLanguage];
+ break;
default:
NSAssert(NO, @"All runtime styling setting rows should be implemented");
break;
@@ -834,7 +840,6 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
});
}
-
- (void)styleQuery
{
CGRect queryRect = CGRectInset(self.mapView.bounds, 100, 200);
@@ -1049,6 +1054,66 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
[self.mapView.style addLayer:rasterLayer];
}
+-(void)styleCountryLabelsLanguage
+{
+ NSArray<NSString *> *labelLayers = @[
+ @"country-label-lg",
+ @"country-label-md",
+ @"country-label-sm",
+ ];
+ [self styleLabelLanguageForLayersNamed:labelLayers];
+}
+
+- (void)styleLabelLanguageForLayersNamed:(NSArray<NSString *> *)layers
+{
+ _usingLocaleBasedCountryLabels = !_usingLocaleBasedCountryLabels;
+ NSString *bestLanguageForUser = [NSString stringWithFormat:@"{name_%@}", [self bestLanguageForUser]];
+ NSString *language = _usingLocaleBasedCountryLabels ? bestLanguageForUser : @"{name}";
+
+ for (NSString *layerName in layers) {
+ MGLSymbolStyleLayer *layer = (MGLSymbolStyleLayer *)[self.mapView.style layerWithIdentifier:layerName];
+
+ if ([layer isKindOfClass:[MGLSymbolStyleLayer class]]) {
+ if ([layer.textField isKindOfClass:[MGLStyleConstantValue class]]) {
+ MGLStyleConstantValue *label = (MGLStyleConstantValue<NSString *> *)layer.textField;
+ if ([label.rawValue hasPrefix:@"{name"]) {
+ layer.textField = [MGLStyleValue valueWithRawValue:language];
+ }
+ } else if ([layer.textField isKindOfClass:[MGLStyleFunction class]]) {
+ MGLStyleFunction *function = (MGLStyleFunction<NSString *> *)layer.textField;
+ [function.stops enumerateKeysAndObjectsUsingBlock:^(id zoomLevel, id stop, BOOL *done) {
+ if ([stop isKindOfClass:[MGLStyleConstantValue class]]) {
+ MGLStyleConstantValue *label = (MGLStyleConstantValue<NSString *> *)stop;
+ if ([label.rawValue hasPrefix:@"{name"]) {
+ [function.stops setValue:[MGLStyleValue valueWithRawValue:language] forKey:zoomLevel];
+ }
+ }
+ }];
+ layer.textField = function;
+ }
+ } else {
+ NSLog(@"%@ is not a symbol style layer", layerName);
+ }
+ }
+}
+
+- (NSString *)bestLanguageForUser
+{
+ NSArray *supportedLanguages = @[ @"en", @"es", @"fr", @"de", @"ru", @"zh" ];
+ NSArray<NSString *> *preferredLanguages = [NSLocale preferredLanguages];
+ NSString *bestLanguage;
+
+ for (NSString *language in preferredLanguages) {
+ NSString *thisLanguage = [NSLocale localeWithLocaleIdentifier:language].languageCode;
+ if ([supportedLanguages containsObject:thisLanguage]) {
+ bestLanguage = thisLanguage;
+ break;
+ }
+ }
+
+ return bestLanguage ?: @"en";
+}
+
- (IBAction)startWorldTour
{
_isTouringWorld = YES;
@@ -1443,4 +1508,12 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
point.coordinate = [self.mapView convertPoint:self.mapView.center toCoordinateFromView:self.mapView];
}
+- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style
+{
+ // Default Mapbox styles use {name_en} as their label language, which means
+ // that a device with an English-language locale is already effectively
+ // using locale-based country labels.
+ _usingLocaleBasedCountryLabels = [[self bestLanguageForUser] isEqualToString:@"en"];
+}
+
@end