TileLayerViewController.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/TileLayerViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. @implementation TileLayerViewController {
  18. UISegmentedControl *_switcher;
  19. GMSMapView *_mapView;
  20. GMSTileLayer *_tileLayer;
  21. NSInteger _floor;
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:37.78318
  26. longitude:-122.403874
  27. zoom:18];
  28. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  29. _mapView.buildingsEnabled = NO;
  30. _mapView.indoorEnabled = NO;
  31. self.view = _mapView;
  32. // The possible floors that might be shown.
  33. NSArray *types = @[ @"1", @"2", @"3" ];
  34. // Create a UISegmentedControl that is the navigationItem's titleView.
  35. _switcher = [[UISegmentedControl alloc] initWithItems:types];
  36. _switcher.selectedSegmentIndex = 0;
  37. _switcher.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  38. _switcher.frame =
  39. CGRectMake(0, 0, 300, _switcher.frame.size.height);
  40. self.navigationItem.titleView = _switcher;
  41. // Listen to touch events on the UISegmentedControl, force initial update.
  42. [_switcher addTarget:self action:@selector(didChangeSwitcher)
  43. forControlEvents:UIControlEventValueChanged];
  44. [self didChangeSwitcher];
  45. }
  46. - (void)didChangeSwitcher {
  47. NSString *title =
  48. [_switcher titleForSegmentAtIndex:_switcher.selectedSegmentIndex];
  49. NSInteger floor = [title integerValue];
  50. if (_floor != floor) {
  51. // Clear existing tileLayer, if any.
  52. _tileLayer.map = nil;
  53. // Create a new GMSTileLayer with the new floor choice.
  54. GMSTileURLConstructor urls = ^(NSUInteger x, NSUInteger y, NSUInteger zoom) {
  55. NSString *url = [NSString
  56. stringWithFormat:@"https://www.gstatic.com/io2010maps/tiles/9/L%ld_%lu_%lu_%lu.png",
  57. (long)floor, (unsigned long)zoom, (unsigned long)x, (unsigned long)y];
  58. return [NSURL URLWithString:url];
  59. };
  60. _tileLayer = [GMSURLTileLayer tileLayerWithURLConstructor:urls];
  61. _tileLayer.map = _mapView;
  62. _floor = floor;
  63. }
  64. }
  65. @end