Stay organized with collections Save and categorize content based on your preferences.
Displays a map of the south Pacific Ocean with an animation of red and green polylines changing their position and size.
Get startedBefore you can try the sample code, you must configure your development environment. For more information, see Maps SDK for iOS code samples.
View the code Swiftimport GoogleMaps import UIKit final class PolylinesViewController: UIViewController { private lazy var styles: [GMSStrokeStyle] = { let greenStyle = GMSStrokeStyle.gradient(from: .green, to: UIColor.green.withAlphaComponent(0)) let redStyle = GMSStrokeStyle.gradient(from: UIColor.red.withAlphaComponent(0), to: .red) return [greenStyle, redStyle, GMSStrokeStyle.solidColor(UIColor(white: 0, alpha: 0))] }() private var pathLength: Double = 0 private var pos: Double = 0 private var polylines: [GMSPolyline] = [] private lazy var mapView: GMSMapView = { let camera = GMSCameraPosition(latitude: -30, longitude: -175, zoom: 3) return GMSMapView(frame: .zero, camera: camera) }() override func loadView() { view = mapView mapView.accessibilityElementsHidden = true } override func viewDidLoad() { super.viewDidLoad() var path = GMSMutablePath() path.addLatitude(-33.866901, longitude: 151.195988) path.addLatitude(-18, longitude: 179) path.addLatitude(21.291982, longitude: -157.821856) path.addLatitude(37.423802, longitude: -122.091859) path.addLatitude(-12, longitude: -77) path.addLatitude(-33.866901, longitude: 151.195988) path = path.pathOffset(byLatitude: -30, longitude: 0) pathLength = path.length(of: .geodesic) / 21 for i in 0..<30 { let polyline = GMSPolyline(path: path.pathOffset(byLatitude: Double(i) * 1.5, longitude: 0)) polyline.strokeWidth = 8 polyline.geodesic = true polyline.map = mapView polylines.append(polyline) } animatePath() } // Updates the path style every 0.1 seconds. private func animatePath() { polylines.forEach { if let path = $0.path { $0.spans = GMSStyleSpansOffset(path, styles, [NSNumber(value: pathLength)], .geodesic, pos) } } pos -= 50000 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { self.animatePath() } } }Objective-C
#import "GoogleMapsDemos/Samples/PolylinesViewController.h" #import <GoogleMaps/GoogleMaps.h> static CLLocationCoordinate2D kSydneyAustralia = {-33.866901, 151.195988}; static CLLocationCoordinate2D kHawaiiUSA = {21.291982, -157.821856}; static CLLocationCoordinate2D kFiji = {-18, 179}; static CLLocationCoordinate2D kMountainViewUSA = {37.423802, -122.091859}; static CLLocationCoordinate2D kLimaPeru = {-12, -77}; static bool kAnimate = true; @implementation PolylinesViewController { NSArray *_styles; NSArray *_lengths; NSArray *_polys; double _pos, _step; GMSMapView *_mapView; } - (void)tick { for (GMSPolyline *poly in _polys) { poly.spans = GMSStyleSpansOffset(poly.path, _styles, _lengths, kGMSLengthGeodesic, _pos); } _pos -= _step; if (kAnimate) { __weak id weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 10), dispatch_get_main_queue(), ^{ [weakSelf tick]; }); } } - (void)initLines { if (!_polys) { NSMutableArray *polys = [NSMutableArray array]; GMSMutablePath *path = [GMSMutablePath path]; [path addCoordinate:kSydneyAustralia]; [path addCoordinate:kFiji]; [path addCoordinate:kHawaiiUSA]; [path addCoordinate:kMountainViewUSA]; [path addCoordinate:kLimaPeru]; [path addCoordinate:kSydneyAustralia]; path = [path pathOffsetByLatitude:-30 longitude:0]; _lengths = @[ @([path lengthOfKind:kGMSLengthGeodesic] / 21) ]; for (int i = 0; i < 30; ++i) { GMSPolyline *poly = [[GMSPolyline alloc] init]; poly.path = [path pathOffsetByLatitude:(i * 1.5) longitude:0]; poly.strokeWidth = 8; poly.geodesic = YES; poly.map = _mapView; [polys addObject:poly]; } _polys = polys; } } - (void)viewDidLoad { [super viewDidLoad]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-30 longitude:-175 zoom:3]; GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; mapView.accessibilityElementsHidden = YES; self.view = mapView; _mapView = mapView; CGFloat alpha = 1; UIColor *green = [UIColor colorWithRed:0 green:1 blue:0 alpha:alpha]; UIColor *greenTransp = [UIColor colorWithRed:0 green:1 blue:0 alpha:0]; UIColor *red = [UIColor colorWithRed:1 green:0 blue:0 alpha:alpha]; UIColor *redTransp = [UIColor colorWithRed:1 green:0 blue:0 alpha:0]; GMSStrokeStyle *grad1 = [GMSStrokeStyle gradientFromColor:green toColor:greenTransp]; GMSStrokeStyle *grad2 = [GMSStrokeStyle gradientFromColor:redTransp toColor:red]; _styles = @[ grad1, grad2, [GMSStrokeStyle solidColor:[UIColor colorWithWhite:0 alpha:0]], ]; _step = 50000; [self initLines]; [self tick]; } @endRun the full sample app locally
The Maps SDK for iOS sample app is available as a download archive from GitHub. Follow these steps to install and try the Maps SDK for iOS sample app.
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git
to clone the samples repository into a local directory.Open a terminal window, navigate to the directory where you cloned the sample files, and drill down into the GoogleMaps directory:
Swiftcd maps-sdk-for-ios-samples-main/GoogleMaps-SwiftObjective-Cpod install
open GoogleMapsSwiftDemos.xcworkspace
cd maps-sdk-for-ios-samples-main/GoogleMapspod install
open GoogleMapsDemos.xcworkspace
SDKConstants.swift
file for Swift orSDKDemoAPIKey.h
file for Objective-C.SDKConstants.swift
file for Swift orSDKDemoAPIKey.h
file for Objective-C and paste your API key into the definition of either the apiKey
or kAPIKey
constant. For example: Swift
static let apiKey = "YOUR_API_KEY"Objective-C
static NSString *const kAPIKey = @"YOUR_API_KEY";
SDKConstants.swift
file (Swift) orSDKDemoAPIKey.h
file (Objective-C), remove the following line, because it's used to register the user-defined issue: Swift
#error (Register for API Key and insert here. Then delete this line.)Objective-C
#error Register for API Key and insert here.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-23 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-23 UTC."],[[["This sample demonstrates animating the style of a polyline on a map using the Google Maps SDK for iOS."],["The animation is achieved by dynamically updating the `GMSStyleSpansOffset` of the polylines based on position and style."],["The sample code provides implementations in both Swift and Objective-C."],["You can clone the sample app repository, install dependencies via CocoaPods, build the project, and run it on an iOS simulator."],["Ensure to obtain and configure your API key before building and running the sample app."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4