StyledMapViewController.m 5.15 KB
Newer Older
Julio Hermosa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/*
 * Copyright 2016 Google LLC. All rights reserved.
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#import "GoogleMapsDemos/Samples/StyledMapViewController.h"

#import <GoogleMaps/GoogleMaps.h>

static NSString *const kNormalType = @"Normal";
static NSString *const kRetroType = @"Retro";
static NSString *const kGrayscaleType = @"Grayscale";
static NSString *const kNightType = @"Night";
static NSString *const kNoPOIsType = @"No business points of interest, no transit";

@implementation StyledMapViewController {
  UIBarButtonItem *_barButtonItem;
  GMSMapView *_mapView;
  GMSMapStyle *_retroStyle;
  GMSMapStyle *_grayscaleStyle;
  GMSMapStyle *_nightStyle;
  GMSMapStyle *_noPOIsStyle;
}

- (void)viewDidLoad {
  [super viewDidLoad];

  // Error handling is skipped here for brevity, however it is recommended that you look at the
  // error returned from |styleWithContentsOfFileURL:error:| if it returns nil. This error will
  // provide information on why your style was not able to be loaded.

  NSURL *retroURL = [[NSBundle mainBundle] URLForResource:@"mapstyle-retro"
                                            withExtension:@"json"];
  _retroStyle = [GMSMapStyle styleWithContentsOfFileURL:retroURL error:NULL];

  NSURL *grayscaleURL = [[NSBundle mainBundle] URLForResource:@"mapstyle-silver"
                                                withExtension:@"json"];
  _grayscaleStyle = [GMSMapStyle styleWithContentsOfFileURL:grayscaleURL error:NULL];

  NSURL *nightURL = [[NSBundle mainBundle] URLForResource:@"mapstyle-night"
                                            withExtension:@"json"];
  _nightStyle = [GMSMapStyle styleWithContentsOfFileURL:nightURL error:NULL];

  NSString *noPOIsString = @" [\n"
      "  {\n"
      "  \"featureType\": \"poi.business\",\n"
      "  \"elementType\": \"all\",\n"
      "  \"stylers\": [\n"
      "              {\n"
      "              \"visibility\": \"off\"\n"
      "              }\n"
      "              ]\n"
      "  },\n"
      "  {\n"
      "  \"featureType\": \"transit\",\n"
      "  \"elementType\": \"all\",\n"
      "  \"stylers\": [\n"
      "              {\n"
      "              \"visibility\": \"off\"\n"
      "              }\n"
      "              ]\n"
      "  }\n"
      "  ]";
  _noPOIsStyle = [GMSMapStyle styleWithJSONString:noPOIsString error:NULL];

  GMSCameraPosition *camera =
      [GMSCameraPosition cameraWithLatitude:-33.868 longitude:151.2086 zoom:12];

  _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  self.view = _mapView;

  _mapView.mapStyle = _retroStyle;

  UIBarButtonItem *styleButton = [[UIBarButtonItem alloc] initWithTitle:@"Style"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(changeMapStyle:)];
  self.navigationItem.rightBarButtonItem = styleButton;
  self.navigationItem.title = kRetroType;
}

- (UIAlertAction *_Nonnull)actionWithTitle:(nonnull NSString *)title
                                     style:(nullable GMSMapStyle *)style {
  __weak __typeof__(self) weakSelf = self;
  return [UIAlertAction actionWithTitle:title
                                  style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction *_Nonnull action) {
                                  __strong __typeof__(self) strongSelf = weakSelf;
                                  if (strongSelf) {
                                    strongSelf->_mapView.mapStyle = style;
                                    strongSelf.navigationItem.title = title;
                                  }
                                }];
}

- (void)changeMapStyle:(UIBarButtonItem *)sender {
  UIAlertController *alert =
      [UIAlertController alertControllerWithTitle:@"Select map style"
                                          message:nil
                                   preferredStyle:UIAlertControllerStyleActionSheet];
  [alert addAction:[self actionWithTitle:kRetroType style:_retroStyle]];
  [alert addAction:[self actionWithTitle:kGrayscaleType style:_grayscaleStyle]];
  [alert addAction:[self actionWithTitle:kNightType style:_nightStyle]];
  [alert addAction:[self actionWithTitle:kNormalType style:nil]];
  [alert addAction:[self actionWithTitle:kNoPOIsType style:_noPOIsStyle]];
  [alert addAction:[UIAlertAction actionWithTitle:@"Cancel"
                                            style:UIAlertActionStyleCancel
                                          handler:nil]];
  alert.popoverPresentationController.barButtonItem = sender;
  [self presentViewController:alert animated:YES completion:nil];
}

@end