SnapshotReadyViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/SnapshotReadyViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. @interface SnapshotReadyViewController ()<GMSMapViewDelegate>
  18. @end
  19. @implementation SnapshotReadyViewController {
  20. GMSMapView *_mapView;
  21. UILabel *_statusLabel;
  22. UIBarButtonItem *_waitButton;
  23. BOOL _isAwaitingSnapshot;
  24. }
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. GMSCameraPosition *camera =
  28. [GMSCameraPosition cameraWithLatitude:-33.868 longitude:151.2086 zoom:6];
  29. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  30. _mapView.delegate = self;
  31. self.view = _mapView;
  32. // Add status label, initially hidden.
  33. _statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
  34. _statusLabel.alpha = 0.0f;
  35. _statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  36. _statusLabel.backgroundColor = [UIColor blueColor];
  37. _statusLabel.textColor = [UIColor whiteColor];
  38. _statusLabel.textAlignment = NSTextAlignmentCenter;
  39. // Add a wait button to signify on the next SnapshotReady event, a screenshot of the map will
  40. // be taken.
  41. _waitButton = [[UIBarButtonItem alloc] initWithTitle:@"Wait for snapshot"
  42. style:UIBarButtonItemStylePlain
  43. target:self
  44. action:@selector(didTapWait)];
  45. self.navigationItem.rightBarButtonItems = @[ _waitButton ];
  46. [_mapView addSubview:_statusLabel];
  47. }
  48. #pragma mark GMSMapViewDelegate
  49. - (void)mapViewSnapshotReady:(GMSMapView *)mapView {
  50. if (_isAwaitingSnapshot) {
  51. _isAwaitingSnapshot = NO;
  52. _waitButton.enabled = YES;
  53. _waitButton.title = @"Wait for snapshot";
  54. [self takeSnapshot];
  55. }
  56. _statusLabel.alpha = 0.8f;
  57. _statusLabel.text = @"Snapshot Ready";
  58. // Remove status label after 1 second.
  59. UILabel *statusLabel = _statusLabel;
  60. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  61. statusLabel.alpha = 0.0f;
  62. });
  63. }
  64. #pragma mark Private
  65. - (void)didTapWait {
  66. _isAwaitingSnapshot = YES;
  67. _waitButton.enabled = NO;
  68. _waitButton.title = @"Waiting";
  69. }
  70. - (void)takeSnapshot {
  71. // Take a snapshot of the map.
  72. UIGraphicsBeginImageContextWithOptions(_mapView.bounds.size, YES, 0);
  73. [_mapView drawViewHierarchyInRect:_mapView.bounds afterScreenUpdates:YES];
  74. UIImage *mapSnapShot = UIGraphicsGetImageFromCurrentImageContext();
  75. UIGraphicsEndImageContext();
  76. // Put snapshot image into an UIImageView and overlay on top of map.
  77. UIImageView *imageView = [[UIImageView alloc] initWithImage:mapSnapShot];
  78. imageView.layer.borderColor = [UIColor redColor].CGColor;
  79. imageView.layer.borderWidth = 10.0f;
  80. [_mapView addSubview:imageView];
  81. // Remove imageView after 1 second.
  82. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  83. [UIView animateWithDuration:1
  84. animations:^{
  85. imageView.alpha = 0.0f;
  86. }
  87. completion:^(BOOL finished) {
  88. [imageView removeFromSuperview];
  89. }];
  90. });
  91. }
  92. @end