PolylinesViewController.m 3.7 KB

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