MapLayerViewController.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/MapLayerViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. @implementation MapLayerViewController {
  18. GMSMapView *_mapView;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-37.81969
  23. longitude:144.966085
  24. zoom:4];
  25. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  26. self.view = _mapView;
  27. GMSMapView *mapView = _mapView;
  28. dispatch_async(dispatch_get_main_queue(), ^{
  29. mapView.myLocationEnabled = YES;
  30. });
  31. UIBarButtonItem *myLocationButton =
  32. [[UIBarButtonItem alloc] initWithTitle:@"Fly to My Location"
  33. style:UIBarButtonItemStylePlain
  34. target:self
  35. action:@selector(didTapMyLocation)];
  36. self.navigationItem.rightBarButtonItem = myLocationButton;
  37. }
  38. - (void)didTapMyLocation {
  39. CLLocation *location = _mapView.myLocation;
  40. if (!location || !CLLocationCoordinate2DIsValid(location.coordinate)) {
  41. return;
  42. }
  43. _mapView.layer.cameraLatitude = location.coordinate.latitude;
  44. _mapView.layer.cameraLongitude = location.coordinate.longitude;
  45. _mapView.layer.cameraBearing = 0.0;
  46. // Access the GMSMapLayer directly to modify the following properties with a
  47. // specified timing function and duration.
  48. CAMediaTimingFunction *curve =
  49. [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  50. CABasicAnimation *animation;
  51. animation = [CABasicAnimation animationWithKeyPath:kGMSLayerCameraLatitudeKey];
  52. animation.duration = 2.0f;
  53. animation.timingFunction = curve;
  54. animation.toValue = @(location.coordinate.latitude);
  55. [_mapView.layer addAnimation:animation forKey:kGMSLayerCameraLatitudeKey];
  56. animation = [CABasicAnimation animationWithKeyPath:kGMSLayerCameraLongitudeKey];
  57. animation.duration = 2.0f;
  58. animation.timingFunction = curve;
  59. animation.toValue = @(location.coordinate.longitude);
  60. [_mapView.layer addAnimation:animation forKey:kGMSLayerCameraLongitudeKey];
  61. animation = [CABasicAnimation animationWithKeyPath:kGMSLayerCameraBearingKey];
  62. animation.duration = 2.0f;
  63. animation.timingFunction = curve;
  64. animation.toValue = @0.0;
  65. [_mapView.layer addAnimation:animation forKey:kGMSLayerCameraBearingKey];
  66. // Fly out to the minimum zoom and then zoom back to the current zoom!
  67. CGFloat zoom = _mapView.camera.zoom;
  68. NSArray *keyValues = @[@(zoom), @(kGMSMinZoomLevel), @(zoom)];
  69. CAKeyframeAnimation *keyFrameAnimation =
  70. [CAKeyframeAnimation animationWithKeyPath:kGMSLayerCameraZoomLevelKey];
  71. keyFrameAnimation.duration = 2.0f;
  72. keyFrameAnimation.values = keyValues;
  73. [_mapView.layer addAnimation:keyFrameAnimation forKey:kGMSLayerCameraZoomLevelKey];
  74. }
  75. @end