StyledMapViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2016 Google Inc. All rights reserved.
  3. *
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. * ANY KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #import "GoogleMapsDemos/Samples/StyledMapViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. static NSString *const kNormalType = @"Normal";
  18. static NSString *const kRetroType = @"Retro";
  19. static NSString *const kGrayscaleType = @"Grayscale";
  20. static NSString *const kNightType = @"Night";
  21. static NSString *const kNoPOIsType = @"No business points of interest, no transit";
  22. @interface StyledMapViewController ()<UIActionSheetDelegate>
  23. @end
  24. @implementation StyledMapViewController {
  25. UIBarButtonItem *_barButtonItem;
  26. GMSMapView *_mapView;
  27. GMSMapStyle *_retroStyle;
  28. GMSMapStyle *_grayscaleStyle;
  29. GMSMapStyle *_nightStyle;
  30. GMSMapStyle *_noPOIsStyle;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Error handling is skipped here for brevity, however it is recommended that you look at the
  35. // error returned from |styleWithContentsOfFileURL:error:| if it returns nil. This error will
  36. // provide information on why your style was not able to be loaded.
  37. NSURL *retroURL = [[NSBundle mainBundle] URLForResource:@"mapstyle-retro"
  38. withExtension:@"json"];
  39. _retroStyle = [GMSMapStyle styleWithContentsOfFileURL:retroURL error:NULL];
  40. NSURL *grayscaleURL = [[NSBundle mainBundle] URLForResource:@"mapstyle-silver"
  41. withExtension:@"json"];
  42. _grayscaleStyle = [GMSMapStyle styleWithContentsOfFileURL:grayscaleURL error:NULL];
  43. NSURL *nightURL = [[NSBundle mainBundle] URLForResource:@"mapstyle-night"
  44. withExtension:@"json"];
  45. _nightStyle = [GMSMapStyle styleWithContentsOfFileURL:nightURL error:NULL];
  46. NSString *noPOIsString = @" [\n"
  47. " {\n"
  48. " \"featureType\": \"poi.business\",\n"
  49. " \"elementType\": \"all\",\n"
  50. " \"stylers\": [\n"
  51. " {\n"
  52. " \"visibility\": \"off\"\n"
  53. " }\n"
  54. " ]\n"
  55. " },\n"
  56. " {\n"
  57. " \"featureType\": \"transit\",\n"
  58. " \"elementType\": \"all\",\n"
  59. " \"stylers\": [\n"
  60. " {\n"
  61. " \"visibility\": \"off\"\n"
  62. " }\n"
  63. " ]\n"
  64. " }\n"
  65. " ]";
  66. _noPOIsStyle = [GMSMapStyle styleWithJSONString:noPOIsString error:NULL];
  67. GMSCameraPosition *camera =
  68. [GMSCameraPosition cameraWithLatitude:-33.868 longitude:151.2086 zoom:12];
  69. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  70. self.view = _mapView;
  71. _mapView.mapStyle = _retroStyle;
  72. UIBarButtonItem *styleButton = [[UIBarButtonItem alloc] initWithTitle:@"Style"
  73. style:UIBarButtonItemStylePlain
  74. target:self
  75. action:@selector(changeMapStyle:)];
  76. self.navigationItem.rightBarButtonItem = styleButton;
  77. self.navigationItem.title = kRetroType;
  78. }
  79. - (void)changeMapStyle:(UIBarButtonItem *)sender {
  80. UIActionSheet *actionSheet = [[UIActionSheet alloc]
  81. initWithTitle:@"Select map style"
  82. delegate:self
  83. cancelButtonTitle:nil
  84. destructiveButtonTitle:nil
  85. otherButtonTitles:kRetroType, kGrayscaleType, kNightType, kNormalType, kNoPOIsType, nil];
  86. [actionSheet showFromBarButtonItem:sender animated:YES];
  87. }
  88. #pragma mark - UIActionSheetDelegate
  89. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  90. switch (buttonIndex) {
  91. case 0:
  92. _mapView.mapStyle = _retroStyle;
  93. self.navigationItem.title = kRetroType;
  94. break;
  95. case 1:
  96. _mapView.mapStyle = _grayscaleStyle;
  97. self.navigationItem.title = kGrayscaleType;
  98. break;
  99. case 2:
  100. _mapView.mapStyle = _nightStyle;
  101. self.navigationItem.title = kNightType;
  102. break;
  103. case 3:
  104. _mapView.mapStyle = nil;
  105. self.navigationItem.title = kNormalType;
  106. break;
  107. case 4:
  108. _mapView.mapStyle = _noPOIsStyle;
  109. self.navigationItem.title = kNoPOIsType;
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. @end