PolylinesViewController.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/PolylinesViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @interface GMSPolyline (length)
  7. @property(nonatomic, readonly) double length;
  8. @end
  9. @implementation GMSPolyline (length)
  10. - (double)length {
  11. GMSLengthKind kind = [self geodesic] ? kGMSLengthGeodesic : kGMSLengthRhumb;
  12. return [[self path] lengthOfKind:kind];
  13. }
  14. @end
  15. static CLLocationCoordinate2D kSydneyAustralia = {-33.866901, 151.195988};
  16. static CLLocationCoordinate2D kHawaiiUSA = {21.291982, -157.821856};
  17. static CLLocationCoordinate2D kFiji = {-18, 179};
  18. static CLLocationCoordinate2D kMountainViewUSA = {37.423802, -122.091859};
  19. static CLLocationCoordinate2D kLimaPeru = {-12, -77};
  20. static bool kAnimate = true;
  21. @implementation PolylinesViewController {
  22. NSArray *_styles;
  23. NSArray *_lengths;
  24. NSArray *_polys;
  25. double _pos, _step;
  26. GMSMapView *_mapView;
  27. }
  28. - (void)tick {
  29. for (GMSPolyline *poly in _polys) {
  30. poly.spans =
  31. GMSStyleSpansOffset(poly.path, _styles, _lengths, kGMSLengthGeodesic, _pos);
  32. }
  33. _pos -= _step;
  34. if (kAnimate) {
  35. __weak id weakSelf = self;
  36. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 10),
  37. dispatch_get_main_queue(),
  38. ^{ [weakSelf tick]; });
  39. }
  40. }
  41. - (void)initLines {
  42. if (!_polys) {
  43. NSMutableArray *polys = [NSMutableArray array];
  44. GMSMutablePath *path = [GMSMutablePath path];
  45. [path addCoordinate:kSydneyAustralia];
  46. [path addCoordinate:kFiji];
  47. [path addCoordinate:kHawaiiUSA];
  48. [path addCoordinate:kMountainViewUSA];
  49. [path addCoordinate:kLimaPeru];
  50. [path addCoordinate:kSydneyAustralia];
  51. path = [path pathOffsetByLatitude:-30 longitude:0];
  52. _lengths = @[@([path lengthOfKind:kGMSLengthGeodesic] / 21)];
  53. for (int i = 0; i < 30; ++i) {
  54. GMSPolyline *poly = [[GMSPolyline alloc] init];
  55. poly.path = [path pathOffsetByLatitude:(i * 1.5) longitude:0];
  56. poly.strokeWidth = 8;
  57. poly.geodesic = YES;
  58. poly.map = _mapView;
  59. [polys addObject:poly];
  60. }
  61. _polys = polys;
  62. }
  63. }
  64. - (void)mapView:(GMSMapView *)mapView
  65. didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
  66. [self initLines];
  67. [self tick];
  68. }
  69. - (void)viewDidLoad {
  70. [super viewDidLoad];
  71. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-30
  72. longitude:-175
  73. zoom:3];
  74. GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  75. mapView.accessibilityElementsHidden = YES;
  76. self.view = mapView;
  77. mapView.delegate = self;
  78. _mapView = mapView;
  79. CGFloat alpha = 1;
  80. UIColor *green = [UIColor colorWithRed:0 green:1 blue: 0 alpha:alpha];
  81. UIColor *greenTransp = [UIColor colorWithRed:0 green:1 blue: 0 alpha:0];
  82. UIColor *red = [UIColor colorWithRed:1 green:0 blue: 0 alpha:alpha];
  83. UIColor *redTransp = [UIColor colorWithRed:1 green:0 blue: 0 alpha:0];
  84. GMSStrokeStyle *grad1 = [GMSStrokeStyle gradientFromColor:green toColor:greenTransp];
  85. GMSStrokeStyle *grad2 = [GMSStrokeStyle gradientFromColor:redTransp toColor:red];
  86. _styles = @[
  87. grad1,
  88. grad2,
  89. [GMSStrokeStyle solidColor:[UIColor colorWithWhite:0 alpha:0]],
  90. ];
  91. _step = 50000;
  92. [self initLines];
  93. [self tick];
  94. }
  95. @end