開始使用
請務必先設定開發環境,再試用程式碼範例。詳情請參閱「Maps SDK for iOS 程式碼範例」一文。
查看程式碼
Swift
import 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]; } @end
在本機執行完整的範例應用程式
您可以從 GitHub 下載封存檔,取得 Maps SDK for iOS 範例應用程式。請按照下列步驟安裝及試用 Maps SDK for iOS 範例應用程式。
- 執行
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git
將範例存放區複製到本機目錄。 開啟終端機視窗,前往複製範例檔案的目錄,然後深入 GoogleMaps 目錄:
Swift
cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift
pod install
open GoogleMapsSwiftDemos.xcworkspace
Objective-C
cd maps-sdk-for-ios-samples-main/GoogleMaps
pod install
open GoogleMapsDemos.xcworkspace
- 在 Xcode 中按下編譯按鈕,即可使用目前的配置建構應用程式。建構作業會產生錯誤,提示您在
SDKConstants.swift
檔案 (Swift) 或SDKDemoAPIKey.h
檔案 (Objective-C) 中輸入 API 金鑰。 - 如果您還沒有 API 金鑰,請按照操作說明在 Google Cloud 控制台中設定專案並取得 API 金鑰。在 Cloud 控制台中設定金鑰時,您可以限制金鑰,只允許範例應用程式的軟體包 ID 使用金鑰,確保只有您的應用程式可以使用金鑰。SDK 範例應用程式的預設軟體包 ID 為
com.example.GoogleMapsDemos
。 - 編輯 Swift 的
SDKConstants.swift
檔案或 Objective-C 的SDKDemoAPIKey.h
檔案,然後將 API 金鑰貼到apiKey
或kAPIKey
常數的定義中。例如:Swift
static let apiKey = "YOUR_API_KEY"
Objective-C
static NSString *const kAPIKey = @"YOUR_API_KEY";
- 在
SDKConstants.swift
檔案 (Swift) 或SDKDemoAPIKey.h
檔案 (Objective-C) 中,移除下列行,因為這會用來註冊使用者定義的問題:Swift
#error (Register for API Key and insert here. Then delete this line.)
Objective-C
#error Register for API Key and insert here.
- 建構並執行專案。系統會隨即顯示 iOS 模擬工具視窗,並在其中顯示 Maps SDK 試用版清單。
- 選擇其中一個顯示的選項,即可試用 Maps SDK for iOS 的功能。
- 如果系統提示您允許 GoogleMapsDemos 存取您的位置,請選擇「允許」。