AnimatedCurrentLocationViewController.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/AnimatedCurrentLocationViewController.h"
  5. @implementation AnimatedCurrentLocationViewController {
  6. CLLocationManager *_manager;
  7. GMSMapView *_mapView;
  8. GMSMarker *_locationMarker;
  9. }
  10. - (void)viewDidLoad {
  11. [super viewDidLoad];
  12. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:38.8879
  13. longitude:-77.0200
  14. zoom:17];
  15. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  16. _mapView.settings.myLocationButton = NO;
  17. _mapView.settings.indoorPicker = NO;
  18. self.view = _mapView;
  19. // Setup location services
  20. if (![CLLocationManager locationServicesEnabled]) {
  21. NSLog(@"Please enable location services");
  22. return;
  23. }
  24. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
  25. NSLog(@"Please authorize location services");
  26. return;
  27. }
  28. _manager = [[CLLocationManager alloc] init];
  29. _manager.delegate = self;
  30. _manager.desiredAccuracy = kCLLocationAccuracyBest;
  31. _manager.distanceFilter = 5.0f;
  32. [_manager startUpdatingLocation];
  33. }
  34. #pragma mark - CLLocationManagerDelegate
  35. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  36. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
  37. NSLog(@"Please authorize location services");
  38. return;
  39. }
  40. NSLog(@"CLLocationManager error: %@", error.localizedFailureReason);
  41. return;
  42. }
  43. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
  44. CLLocation *location = [locations lastObject];
  45. if (_locationMarker == nil) {
  46. _locationMarker = [[GMSMarker alloc] init];
  47. _locationMarker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
  48. // Animated walker images derived from an www.angryanimator.com tutorial.
  49. // See: http://www.angryanimator.com/word/2010/11/26/tutorial-2-walk-cycle/
  50. NSArray *frames = @[[UIImage imageNamed:@"step1"],
  51. [UIImage imageNamed:@"step2"],
  52. [UIImage imageNamed:@"step3"],
  53. [UIImage imageNamed:@"step4"],
  54. [UIImage imageNamed:@"step5"],
  55. [UIImage imageNamed:@"step6"],
  56. [UIImage imageNamed:@"step7"],
  57. [UIImage imageNamed:@"step8"]];
  58. _locationMarker.icon = [UIImage animatedImageWithImages:frames duration:0.8];
  59. _locationMarker.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow
  60. _locationMarker.map = _mapView;
  61. } else {
  62. [CATransaction begin];
  63. [CATransaction setAnimationDuration:2.0];
  64. _locationMarker.position = location.coordinate;
  65. [CATransaction commit];
  66. }
  67. GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
  68. [_mapView animateWithCameraUpdate:move];
  69. }
  70. @end