IndoorMuseumNavigationViewController.m 4.2 KB

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