TileLayerViewController.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/TileLayerViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @implementation TileLayerViewController {
  7. UISegmentedControl *_switcher;
  8. GMSMapView *_mapView;
  9. GMSTileLayer *_tileLayer;
  10. NSInteger _floor;
  11. }
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:37.78318
  15. longitude:-122.403874
  16. zoom:18];
  17. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  18. _mapView.buildingsEnabled = NO;
  19. _mapView.indoorEnabled = NO;
  20. self.view = _mapView;
  21. // The possible floors that might be shown.
  22. NSArray *types = @[ @"1", @"2", @"3" ];
  23. // Create a UISegmentedControl that is the navigationItem's titleView.
  24. _switcher = [[UISegmentedControl alloc] initWithItems:types];
  25. _switcher.selectedSegmentIndex = 0;
  26. _switcher.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  27. _switcher.frame =
  28. CGRectMake(0, 0, 300, _switcher.frame.size.height);
  29. self.navigationItem.titleView = _switcher;
  30. // Listen to touch events on the UISegmentedControl, force initial update.
  31. [_switcher addTarget:self action:@selector(didChangeSwitcher)
  32. forControlEvents:UIControlEventValueChanged];
  33. [self didChangeSwitcher];
  34. }
  35. - (void)didChangeSwitcher {
  36. NSString *title =
  37. [_switcher titleForSegmentAtIndex:_switcher.selectedSegmentIndex];
  38. NSInteger floor = [title integerValue];
  39. if (_floor != floor) {
  40. // Clear existing tileLayer, if any.
  41. _tileLayer.map = nil;
  42. // Create a new GMSTileLayer with the new floor choice.
  43. GMSTileURLConstructor urls = ^(NSUInteger x, NSUInteger y, NSUInteger zoom) {
  44. NSString *url = [NSString stringWithFormat:@"http://www.gstatic.com/io2010maps/tiles/9/L%zd_%tu_%tu_%tu.png", floor, zoom, x, y];
  45. return [NSURL URLWithString:url];
  46. };
  47. _tileLayer = [GMSURLTileLayer tileLayerWithURLConstructor:urls];
  48. _tileLayer.map = _mapView;
  49. _floor = floor;
  50. }
  51. }
  52. @end