MarkerLayerViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/MarkerLayerViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @interface CoordsList : NSObject
  7. @property(nonatomic, readonly, copy) GMSPath *path;
  8. @property(nonatomic, readonly) NSUInteger target;
  9. - (id)initWithPath:(GMSPath *)path;
  10. - (CLLocationCoordinate2D)next;
  11. @end
  12. @implementation CoordsList
  13. - (id)initWithPath:(GMSPath *)path {
  14. if ((self = [super init])) {
  15. _path = [path copy];
  16. _target = 0;
  17. }
  18. return self;
  19. }
  20. - (CLLocationCoordinate2D)next {
  21. ++_target;
  22. if (_target == [_path count]) {
  23. _target = 0;
  24. }
  25. return [_path coordinateAtIndex:_target];
  26. }
  27. @end
  28. @implementation MarkerLayerViewController {
  29. GMSMapView *mapView_;
  30. GMSMarker *fadedMarker_;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. mapView_ = [[GMSMapView alloc] init];
  35. mapView_.camera = [GMSCameraPosition cameraWithLatitude:50.6042 longitude:3.9599 zoom:5];
  36. mapView_.delegate = self;
  37. self.view = mapView_;
  38. GMSMutablePath *coords;
  39. GMSMarker *marker;
  40. // Create a plane that flies to several airports around western Europe.
  41. coords = [GMSMutablePath path];
  42. [coords addLatitude:52.310683 longitude:4.765121];
  43. [coords addLatitude:51.471386 longitude:-0.457148];
  44. [coords addLatitude:49.01378 longitude:2.5542943];
  45. [coords addLatitude:50.036194 longitude:8.554519];
  46. marker = [GMSMarker markerWithPosition:[coords coordinateAtIndex:0]];
  47. marker.icon = [UIImage imageNamed:@"aeroplane"];
  48. marker.groundAnchor = CGPointMake(0.5f, 0.5f);
  49. marker.flat = YES;
  50. marker.map = mapView_;
  51. marker.userData = [[CoordsList alloc] initWithPath:coords];
  52. [self animateToNextCoord:marker];
  53. // Create a boat that moves around the Baltic Sea.
  54. coords = [GMSMutablePath path];
  55. [coords addLatitude:57.598335 longitude:11.290512];
  56. [coords addLatitude:55.665193 longitude:10.741196];
  57. [coords addLatitude:55.065787 longitude:11.083488];
  58. [coords addLatitude:54.699234 longitude:10.863762];
  59. [coords addLatitude:54.482805 longitude:12.061272];
  60. [coords addLatitude:55.819802 longitude:16.148186]; // final point
  61. [coords addLatitude:54.927142 longitude:16.455803]; // final point
  62. [coords addLatitude:54.482805 longitude:12.061272]; // and back again
  63. [coords addLatitude:54.699234 longitude:10.863762];
  64. [coords addLatitude:55.065787 longitude:11.083488];
  65. [coords addLatitude:55.665193 longitude:10.741196];
  66. marker = [GMSMarker markerWithPosition:[coords coordinateAtIndex:0]];
  67. marker.icon = [UIImage imageNamed:@"boat"];
  68. marker.map = mapView_;
  69. marker.userData = [[CoordsList alloc] initWithPath:coords];
  70. [self animateToNextCoord:marker];
  71. }
  72. - (void)animateToNextCoord:(GMSMarker *)marker {
  73. CoordsList *coords = marker.userData;
  74. CLLocationCoordinate2D coord = [coords next];
  75. CLLocationCoordinate2D previous = marker.position;
  76. CLLocationDirection heading = GMSGeometryHeading(previous, coord);
  77. CLLocationDistance distance = GMSGeometryDistance(previous, coord);
  78. // Use CATransaction to set a custom duration for this animation. By default, changes to the
  79. // position are already animated, but with a very short default duration. When the animation is
  80. // complete, trigger another animation step.
  81. [CATransaction begin];
  82. [CATransaction setAnimationDuration:(distance / (50 * 1000))]; // custom duration, 50km/sec
  83. __weak MarkerLayerViewController *weakSelf = self;
  84. [CATransaction setCompletionBlock:^{
  85. [weakSelf animateToNextCoord:marker];
  86. }];
  87. marker.position = coord;
  88. [CATransaction commit];
  89. // If this marker is flat, implicitly trigger a change in rotation, which will finish quickly.
  90. if (marker.flat) {
  91. marker.rotation = heading;
  92. }
  93. }
  94. - (void)fadeMarker:(GMSMarker *)marker {
  95. fadedMarker_.opacity = 1.0f; // reset previous faded marker
  96. // Fade this new marker.
  97. fadedMarker_ = marker;
  98. fadedMarker_.opacity = 0.5f;
  99. }
  100. #pragma mark - GMSMapViewDelegate
  101. - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
  102. [self fadeMarker:marker];
  103. return YES;
  104. }
  105. - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
  106. [self fadeMarker:nil];
  107. }
  108. @end