Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Add country label language toggle demo #6819

Merged
merged 2 commits into from
Oct 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 74 additions & 6 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) {
MBXSettingsRuntimeStylingUpdateGeoJSONSourceFeatures,
MBXSettingsRuntimeStylingVectorSource,
MBXSettingsRuntimeStylingRasterSource,
MBXSettingsRuntimeStylingCountryLabels,
};

typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
Expand Down Expand Up @@ -105,6 +106,7 @@ @interface MBXViewController () <UITableViewDelegate,
@property (nonatomic) NSInteger styleIndex;
@property (nonatomic) BOOL debugLoggingEnabled;
@property (nonatomic) BOOL customUserLocationAnnnotationEnabled;
@property (nonatomic) BOOL usingLocaleBasedCountryLabels;

@end

Expand Down Expand Up @@ -138,12 +140,7 @@ - (void)viewDidLoad
[self restoreState:nil];

self.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

if ([MGLAccountManager accessToken].length)
{
self.styleIndex = -1;
Expand Down Expand Up @@ -322,6 +319,7 @@ - (void)dismissSettings:(__unused id)sender
@"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:
Expand Down Expand Up @@ -474,6 +472,9 @@ - (void)performActionForSettingAtIndexPath:(NSIndexPath *)indexPath
case MBXSettingsRuntimeStylingRasterSource:
[self styleRasterSource];
break;
case MBXSettingsRuntimeStylingCountryLabels:
[self styleCountryLabelsLanguage];
break;
default:
NSAssert(NO, @"All runtime styling setting rows should be implemented");
break;
Expand Down Expand Up @@ -834,7 +835,6 @@ - (void)styleNumericFilteredFills
});
}


- (void)styleQuery
{
CGRect queryRect = CGRectInset(self.mapView.bounds, 100, 200);
Expand Down Expand Up @@ -1049,6 +1049,66 @@ - (void)styleRasterSource
[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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this flag will get out of sync as soon as the user switches styles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True... 😒

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this in 1533953.

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;
Expand Down Expand Up @@ -1443,4 +1503,12 @@ - (void)mapView:(MGLMapView *)mapView tapOnCalloutForAnnotation:(id <MGLAnnotati
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