GradientPolylinesViewController.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/GradientPolylinesViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. @implementation GradientPolylinesViewController {
  18. GMSMapView *_mapView;
  19. GMSPolyline *_polyline;
  20. NSMutableArray *_trackData;
  21. }
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:44.1314
  25. longitude:9.6921
  26. zoom:14.059f
  27. bearing:328.f
  28. viewingAngle:40.f];
  29. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  30. self.view = _mapView;
  31. [self parseTrackFile];
  32. [_polyline setSpans:[self gradientSpans]];
  33. }
  34. - (NSArray *)gradientSpans {
  35. NSMutableArray *colorSpans = [NSMutableArray array];
  36. NSUInteger count = _trackData.count;
  37. UIColor *prevColor;
  38. for (NSUInteger i = 0; i < count; i++) {
  39. NSDictionary *dict = [_trackData objectAtIndex:i];
  40. double elevation = [[dict objectForKey:@"elevation"] doubleValue];
  41. UIColor *toColor = [UIColor colorWithHue:(float)elevation/700
  42. saturation:1.f
  43. brightness:.9f
  44. alpha:1.f];
  45. if (prevColor == nil) {
  46. prevColor = toColor;
  47. }
  48. GMSStrokeStyle *style = [GMSStrokeStyle gradientFromColor:prevColor toColor:toColor];
  49. [colorSpans addObject:[GMSStyleSpan spanWithStyle:style]];
  50. prevColor = toColor;
  51. }
  52. return colorSpans;
  53. }
  54. - (void)parseTrackFile {
  55. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"track" ofType:@"json"];
  56. NSData *data = [NSData dataWithContentsOfFile:filePath];
  57. NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  58. _trackData = [[NSMutableArray alloc] init];
  59. GMSMutablePath *path = [GMSMutablePath path];
  60. for (NSUInteger i = 0; i < json.count; i++) {
  61. NSDictionary *info = [json objectAtIndex:i];
  62. NSNumber *elevation = [info objectForKey:@"elevation"];
  63. CLLocationDegrees lat = [[info objectForKey:@"lat"] doubleValue];
  64. CLLocationDegrees lng = [[info objectForKey:@"lng"] doubleValue];
  65. CLLocation *loc = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
  66. [_trackData addObject:@{@"loc": loc, @"elevation": elevation}];
  67. [path addLatitude:lat longitude:lng];
  68. }
  69. _polyline = [GMSPolyline polylineWithPath:path];
  70. _polyline.strokeWidth = 6;
  71. _polyline.map = _mapView;
  72. }
  73. @end