GradientPolylinesViewController.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/GradientPolylinesViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @implementation GradientPolylinesViewController {
  7. GMSMapView *mapView_;
  8. GMSPolyline *polyline_;
  9. NSMutableArray *trackData_;
  10. }
  11. - (void)viewDidLoad {
  12. [super viewDidLoad];
  13. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:44.1314
  14. longitude:9.6921
  15. zoom:14.059f
  16. bearing:328.f
  17. viewingAngle:40.f];
  18. mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  19. self.view = mapView_;
  20. [self parseTrackFile];
  21. [polyline_ setSpans:[self gradientSpans]];
  22. }
  23. - (NSArray *)gradientSpans {
  24. NSMutableArray *colorSpans = [NSMutableArray array];
  25. NSUInteger count = [trackData_ count];
  26. UIColor *prevColor;
  27. for (NSUInteger i = 0; i < count; i++) {
  28. double elevation = [[[trackData_ objectAtIndex:i] objectForKey:@"elevation"] doubleValue];
  29. UIColor *toColor = [UIColor colorWithHue:(float)elevation/700
  30. saturation:1.f
  31. brightness:.9f
  32. alpha:1.f];
  33. if (prevColor == nil) {
  34. prevColor = toColor;
  35. }
  36. GMSStrokeStyle *style = [GMSStrokeStyle gradientFromColor:prevColor toColor:toColor];
  37. [colorSpans addObject:[GMSStyleSpan spanWithStyle:style]];
  38. prevColor = toColor;
  39. }
  40. return colorSpans;
  41. }
  42. - (void)parseTrackFile {
  43. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"track" ofType:@"json"];
  44. NSData *data = [NSData dataWithContentsOfFile:filePath];
  45. NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  46. trackData_ = [[NSMutableArray alloc] init];
  47. GMSMutablePath *path = [GMSMutablePath path];
  48. for (NSUInteger i = 0; i < [json count]; i++) {
  49. NSDictionary *info = [json objectAtIndex:i];
  50. NSNumber *elevation = [info objectForKey:@"elevation"];
  51. CLLocationDegrees lat = [[info objectForKey:@"lat"] doubleValue];
  52. CLLocationDegrees lng = [[info objectForKey:@"lng"] doubleValue];
  53. CLLocation *loc = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
  54. [trackData_ addObject:@{@"loc": loc, @"elevation": elevation}];
  55. [path addLatitude:lat longitude:lng];
  56. }
  57. polyline_ = [GMSPolyline polylineWithPath:path];
  58. polyline_.strokeWidth = 6;
  59. polyline_.map = mapView_;
  60. }
  61. @end