[ios] Display map on connected displays

Whenever an external display is connected, show a map synchronized with the main map on the main display.
This commit is contained in:
Minh Nguyễn
2019-01-09 23:25:40 -08:00
committed by Minh Nguyễn
parent 6960cb13f7
commit 6ca2f6c97f

View File

@@ -205,6 +205,7 @@ CLLocationCoordinate2D randomWorldCoordinate() {
@property (nonatomic) BOOL frameTimeGraphEnabled;
@property (nonatomic) BOOL shouldLimitCameraChanges;
@property (nonatomic) BOOL randomWalk;
@property (nonatomic) NSMutableArray<UIWindow *> *helperWindows;
@end
@@ -289,6 +290,32 @@ CLLocationCoordinate2D randomWorldCoordinate() {
}
}
[self.mapView addGestureRecognizer:singleTap];
// Display a secondary map on any connected external display.
// https://developer.apple.com/documentation/uikit/windows_and_screens/displaying_content_on_a_connected_screen?language=objc
self.helperWindows = [NSMutableArray array];
[[NSNotificationCenter defaultCenter] addObserverForName:UIScreenDidConnectNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
UIScreen *helperScreen = note.object;
UIWindow *helperWindow = [[UIWindow alloc] initWithFrame:helperScreen.bounds];
helperWindow.screen = helperScreen;
UIViewController *helperViewController = [[UIViewController alloc] init];
MGLMapView *helperMapView = [[MGLMapView alloc] initWithFrame:helperWindow.bounds styleURL:MGLStyle.satelliteStreetsStyleURL];
helperMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
helperMapView.camera = self.mapView.camera;
helperMapView.compassView.hidden = YES;
helperViewController.view = helperMapView;
helperWindow.rootViewController = helperViewController;
helperWindow.hidden = NO;
[self.helperWindows addObject:helperWindow];
}];
[[NSNotificationCenter defaultCenter] addObserverForName:UIScreenDidDisconnectNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
UIScreen *helperScreen = note.object;
for (UIWindow *window in self.helperWindows) {
if (window.screen == helperScreen) {
[self.helperWindows removeObject:window];
}
}
}];
}
- (void)saveState:(__unused NSNotification *)notification
@@ -2214,6 +2241,7 @@ CLLocationCoordinate2D randomWorldCoordinate() {
- (void)mapViewRegionIsChanging:(MGLMapView *)mapView
{
[self updateHUD];
[self updateHelperMapViews];
}
- (void)mapView:(MGLMapView *)mapView regionDidChangeWithReason:(MGLCameraChangeReason)reason animated:(BOOL)animated
@@ -2223,12 +2251,20 @@ CLLocationCoordinate2D randomWorldCoordinate() {
}
[self updateHUD];
[self updateHelperMapViews];
}
- (void)mapView:(MGLMapView *)mapView didUpdateUserLocation:(MGLUserLocation *)userLocation {
[self updateHUD];
}
- (void)updateHelperMapViews {
for (UIWindow *window in self.helperWindows) {
MGLMapView *mapView = (MGLMapView *)window.rootViewController.view;
mapView.camera = self.mapView.camera;
}
}
- (void)updateHUD {
if (!self.reuseQueueStatsEnabled && !self.mapInfoHUDEnabled) return;