IndoorMuseumNavigationViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/IndoorMuseumNavigationViewController.h"
  16. @implementation IndoorMuseumNavigationViewController {
  17. GMSMapView *_mapView;
  18. NSArray *_exhibits; // Array of JSON exhibit data.
  19. NSDictionary *_exhibit; // The currently selected exhibit. Will be nil initially.
  20. GMSMarker *_marker;
  21. NSDictionary *_levels; // The levels dictionary is updated when a new building is selected, and
  22. // contains mapping from localized level name to GMSIndoorLevel.
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:38.8879
  27. longitude:-77.0200
  28. zoom:17];
  29. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  30. _mapView.settings.myLocationButton = NO;
  31. _mapView.settings.indoorPicker = NO;
  32. _mapView.delegate = self;
  33. _mapView.indoorDisplay.delegate = self;
  34. self.view = _mapView;
  35. // Load the exhibits configuration from JSON
  36. NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"museum-exhibits" ofType:@"json"];
  37. NSData *data = [NSData dataWithContentsOfFile:jsonPath];
  38. _exhibits = [NSJSONSerialization JSONObjectWithData:data
  39. options:kNilOptions
  40. error:nil];
  41. UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] init];
  42. [segmentedControl setTintColor:[UIColor colorWithRed:0.373f green:0.667f blue:0.882f alpha:1.0f]];
  43. segmentedControl.translatesAutoresizingMaskIntoConstraints = NO;
  44. [segmentedControl addTarget:self
  45. action:@selector(exhibitSelected:)
  46. forControlEvents:UIControlEventValueChanged];
  47. [self.view addSubview:segmentedControl];
  48. for (NSDictionary *exhibit in _exhibits) {
  49. [segmentedControl insertSegmentWithImage:[UIImage imageNamed:exhibit[@"key"]]
  50. atIndex:[_exhibits indexOfObject:exhibit]
  51. animated:NO];
  52. }
  53. NSDictionary *views = NSDictionaryOfVariableBindings(segmentedControl);
  54. [self.view addConstraints:[NSLayoutConstraint
  55. constraintsWithVisualFormat:@"[segmentedControl]-|"
  56. options:kNilOptions
  57. metrics:nil
  58. views:views]];
  59. [self.view addConstraints:[NSLayoutConstraint
  60. constraintsWithVisualFormat:@"V:[segmentedControl]-|"
  61. options:kNilOptions
  62. metrics:nil
  63. views:views]];
  64. }
  65. - (void)moveMarker {
  66. CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([_exhibit[@"lat"] doubleValue],
  67. [_exhibit[@"lng"] doubleValue]);
  68. if (_marker == nil) {
  69. _marker = [GMSMarker markerWithPosition:loc];
  70. _marker.map = _mapView;
  71. } else {
  72. _marker.position = loc;
  73. }
  74. _marker.title = _exhibit[@"name"];
  75. [_mapView animateToLocation:loc];
  76. [_mapView animateToZoom:19];
  77. }
  78. - (void)exhibitSelected:(UISegmentedControl *)segmentedControl {
  79. _exhibit = _exhibits[[segmentedControl selectedSegmentIndex]];
  80. [self moveMarker];
  81. }
  82. #pragma mark - GMSMapViewDelegate
  83. - (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)camera {
  84. if (_exhibit != nil) {
  85. CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([_exhibit[@"lat"] doubleValue],
  86. [_exhibit[@"lng"] doubleValue]);
  87. if ([_mapView.projection containsCoordinate:loc] && _levels != nil) {
  88. [mapView.indoorDisplay setActiveLevel:_levels[_exhibit[@"level"]]];
  89. }
  90. }
  91. }
  92. #pragma mark - GMSIndoorDisplayDelegate
  93. - (void)didChangeActiveBuilding:(GMSIndoorBuilding *)building {
  94. if (building != nil) {
  95. NSMutableDictionary *levels = [NSMutableDictionary dictionary];
  96. for (GMSIndoorLevel *level in building.levels) {
  97. [levels setObject:level forKey:level.shortName];
  98. }
  99. _levels = [NSDictionary dictionaryWithDictionary:levels];
  100. } else {
  101. _levels = nil;
  102. }
  103. }
  104. @end