AnimatedCurrentLocationViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/AnimatedCurrentLocationViewController.h"
  16. @implementation AnimatedCurrentLocationViewController {
  17. CLLocationManager *_manager;
  18. GMSMapView *_mapView;
  19. GMSMarker *_locationMarker;
  20. }
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:38.8879
  24. longitude:-77.0200
  25. zoom:17];
  26. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  27. _mapView.settings.myLocationButton = NO;
  28. _mapView.settings.indoorPicker = NO;
  29. self.view = _mapView;
  30. // Setup location services
  31. if (![CLLocationManager locationServicesEnabled]) {
  32. NSLog(@"Please enable location services");
  33. return;
  34. }
  35. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
  36. NSLog(@"Please authorize location services");
  37. return;
  38. }
  39. _manager = [[CLLocationManager alloc] init];
  40. if ([_manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
  41. [_manager requestWhenInUseAuthorization];
  42. }
  43. _manager.delegate = self;
  44. _manager.desiredAccuracy = kCLLocationAccuracyBest;
  45. _manager.distanceFilter = 5.0f;
  46. [_manager startUpdatingLocation];
  47. }
  48. #pragma mark - CLLocationManagerDelegate
  49. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  50. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
  51. NSLog(@"Please authorize location services");
  52. return;
  53. }
  54. NSLog(@"CLLocationManager error: %@", error.localizedFailureReason);
  55. return;
  56. }
  57. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
  58. CLLocation *location = [locations lastObject];
  59. if (_locationMarker == nil) {
  60. _locationMarker = [[GMSMarker alloc] init];
  61. _locationMarker.position = location.coordinate;
  62. // Animated walker images derived from an www.angryanimator.com tutorial.
  63. // See: http://www.angryanimator.com/word/2010/11/26/tutorial-2-walk-cycle/
  64. NSArray *frames = @[[UIImage imageNamed:@"step1"],
  65. [UIImage imageNamed:@"step2"],
  66. [UIImage imageNamed:@"step3"],
  67. [UIImage imageNamed:@"step4"],
  68. [UIImage imageNamed:@"step5"],
  69. [UIImage imageNamed:@"step6"],
  70. [UIImage imageNamed:@"step7"],
  71. [UIImage imageNamed:@"step8"]];
  72. _locationMarker.icon = [UIImage animatedImageWithImages:frames duration:0.8];
  73. _locationMarker.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow
  74. _locationMarker.map = _mapView;
  75. } else {
  76. [CATransaction begin];
  77. [CATransaction setAnimationDuration:2.0];
  78. _locationMarker.position = location.coordinate;
  79. [CATransaction commit];
  80. }
  81. GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
  82. [_mapView animateWithCameraUpdate:move];
  83. }
  84. @end