diff options
Diffstat (limited to 'ios/app/MBXViewController.mm')
-rw-r--r-- | ios/app/MBXViewController.mm | 153 |
1 files changed, 90 insertions, 63 deletions
diff --git a/ios/app/MBXViewController.mm b/ios/app/MBXViewController.mm index 98b337f927..c9973bcb7d 100644 --- a/ios/app/MBXViewController.mm +++ b/ios/app/MBXViewController.mm @@ -1,14 +1,17 @@ #import "MBXViewController.h" -#import <mbgl/ios/MGLMapView.h> +#import <mbgl/ios/MapboxGL.h> #import <mbgl/platform/darwin/settings_nsuserdefaults.hpp> #import <CoreLocation/CoreLocation.h> +#import "MBXAnnotation.h" + static UIColor *const kTintColor = [UIColor colorWithRed:0.120 green:0.550 blue:0.670 alpha:1.000]; static NSArray *const kStyleNames = @[ + @"Emerald", @"Bright", @"Basic", @"Outdoors", @@ -18,10 +21,9 @@ static NSArray *const kStyleNames = @[ static NSString *const kStyleVersion = @"v7"; -@interface MBXViewController () <UIActionSheetDelegate, CLLocationManagerDelegate> +@interface MBXViewController () <UIActionSheetDelegate, MGLMapViewDelegate> @property (nonatomic) MGLMapView *mapView; -@property (nonatomic) CLLocationManager *locationManager; @end @@ -63,9 +65,10 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds accessToken:accessToken]; self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - [self.view addSubview:self.mapView]; - self.mapView.viewControllerForLayoutGuides = self; + self.mapView.showsUserLocation = YES; + self.mapView.delegate = self; + [self.view addSubview:self.mapView]; self.view.tintColor = kTintColor; self.navigationController.navigationBar.tintColor = kTintColor; @@ -104,6 +107,8 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; settings->zoom = self.mapView.zoomLevel; settings->bearing = self.mapView.direction; settings->debug = self.mapView.isDebugActive; + settings->userTrackingMode = self.mapView.userTrackingMode; + settings->showsUserLocation = self.mapView.showsUserLocation; settings->save(); } } @@ -114,6 +119,8 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; settings->load(); [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(settings->latitude, settings->longitude) zoomLevel:settings->zoom animated:NO]; self.mapView.direction = settings->bearing; + self.mapView.userTrackingMode = settings->userTrackingMode; + self.mapView.showsUserLocation = settings->showsUserLocation; [self.mapView setDebugActive:settings->debug]; } } @@ -133,7 +140,14 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil - otherButtonTitles:@"Reset North", @"Reset Position", @"Toggle Debug", nil]; + otherButtonTitles:@"Reset North", + @"Reset Position", + @"Toggle Debug", + @"Add 100 Points", + @"Add 1,000 Points", + @"Add 10,000 Points", + @"Remove Points", + nil]; [sheet showFromBarButtonItem:self.navigationItem.leftBarButtonItem animated:YES]; } @@ -152,6 +166,67 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; { [self.mapView toggleDebug]; } + else if (buttonIndex == actionSheet.firstOtherButtonIndex + 3) + { + [self parseFeaturesAddingCount:100]; + } + else if (buttonIndex == actionSheet.firstOtherButtonIndex + 4) + { + [self parseFeaturesAddingCount:1000]; + } + else if (buttonIndex == actionSheet.firstOtherButtonIndex + 5) + { + [self parseFeaturesAddingCount:10000]; + } + else if (buttonIndex == actionSheet.firstOtherButtonIndex + 6) + { + [self.mapView removeAnnotations:self.mapView.annotations]; + } +} + +- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount +{ + [self.mapView removeAnnotations:self.mapView.annotations]; + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ + { + NSData *featuresData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"features" ofType:@"geojson"]]; + + id features = [NSJSONSerialization JSONObjectWithData:featuresData + options:0 + error:nil]; + + if ([features isKindOfClass:[NSDictionary class]]) + { + NSMutableArray *annotations = [NSMutableArray array]; + + for (NSDictionary *feature in features[@"features"]) + { + CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([feature[@"geometry"][@"coordinates"][1] doubleValue], + [feature[@"geometry"][@"coordinates"][0] doubleValue]); + NSString *title = feature[@"properties"][@"NAME"]; + + MBXAnnotation *annotation = [MBXAnnotation annotationWithLocation:coordinate + title:title + subtitle:nil]; + + [annotations addObject:annotation]; + + if (annotations.count == featuresCount) break; + } + + dispatch_async(dispatch_get_main_queue(), ^ + { + [self.mapView addAnnotations:annotations]; + + [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(38.904722, -77.016389) + zoomLevel:10 + animated:NO]; + + [self.mapView setDirection:0]; + }); + } + }); } - (void)cycleStyles @@ -181,40 +256,17 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; - (void)locateUser { - if ( ! self.locationManager) + if (self.mapView.userTrackingMode == MGLUserTrackingModeNone) { - self.locationManager = [CLLocationManager new]; - self.locationManager.delegate = self; + self.mapView.userTrackingMode = MGLUserTrackingModeFollow; } - - if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) + else if (self.mapView.userTrackingMode == MGLUserTrackingModeFollow) { - [[[UIAlertView alloc] initWithTitle:@"Authorization Denied" - message:@"Please enable location services for this app in Privacy settings." - delegate:nil - cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show]; + self.mapView.userTrackingMode = MGLUserTrackingModeFollowWithHeading; } else { -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) - { - if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) - { - [self.locationManager startUpdatingLocation]; - } - else - { - [_locationManager requestWhenInUseAuthorization]; - } - } - else - { - [self.locationManager startUpdatingLocation]; - } -#else - [self.locationManager startUpdatingLocation]; -#endif + self.mapView.userTrackingMode = MGLUserTrackingModeNone; } } @@ -232,39 +284,14 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; } } -#pragma mark - Location - -- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status -{ - switch (status) - { - case kCLAuthorizationStatusAuthorizedAlways: -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - case kCLAuthorizationStatusAuthorizedWhenInUse: -#endif - { - [manager startUpdatingLocation]; - break; - } - default: - { - } - } -} - #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-parameter" -- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations -{ - CLLocation *latestLocation = locations.lastObject; - - if ([latestLocation distanceFromLocation:[[CLLocation alloc] initWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude]] > 100) - { - [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(latestLocation.coordinate.latitude, latestLocation.coordinate.longitude) zoomLevel:17 animated:YES]; - } +#pragma mark - MGLMapViewDelegate - [self.locationManager stopUpdatingLocation]; +- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation +{ + return YES; } #pragma clang diagnostic pop |