開始使用
請務必先設定開發環境,再試用程式碼範例。詳情請參閱 Maps SDK for iOS 程式碼範例。
查看程式碼
Swift
import GoogleMaps import UIKit class GestureControlViewController: UIViewController { private let holderHeight: CGFloat = 60 private let zoomLabelInset: CGFloat = 16 private lazy var mapView: GMSMapView = { let camera = GMSCameraPosition(latitude: -25.5605, longitude: 133.605097, zoom: 3) return GMSMapView(frame: .zero, camera: camera) }() private lazy var zoomSwitch: UISwitch = UISwitch(frame: .zero) override func viewDidLoad() { super.viewDidLoad() view.addSubview(mapView) let holder = UIView(frame: .zero) holder.backgroundColor = UIColor(white: 1, alpha: 0.8) view.addSubview(holder) let zoomLabel = UILabel(frame: .zero) zoomLabel.text = "Zoom gestures" zoomLabel.font = .boldSystemFont(ofSize: 18) holder.addSubview(zoomLabel) // Control zooming. holder.addSubview(zoomSwitch) zoomSwitch.addTarget(self, action: #selector(toggleZoom), for: .valueChanged) zoomSwitch.isOn = true [mapView, holder, zoomLabel, zoomSwitch].forEach({ $0.translatesAutoresizingMaskIntoConstraints = false }) NSLayoutConstraint.activate([ mapView.leftAnchor.constraint(equalTo: view.leftAnchor), mapView.rightAnchor.constraint(equalTo: view.rightAnchor), mapView.topAnchor.constraint(equalTo: view.topAnchor), mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor), holder.heightAnchor.constraint(equalToConstant: holderHeight), holder.centerXAnchor.constraint(equalTo: view.centerXAnchor), holder.widthAnchor.constraint(equalTo: view.widthAnchor), zoomLabel.leftAnchor.constraint(equalTo: holder.leftAnchor, constant: zoomLabelInset), zoomLabel.centerYAnchor.constraint(equalTo: holder.centerYAnchor), zoomSwitch.rightAnchor.constraint(equalTo: holder.rightAnchor, constant: -zoomLabelInset), zoomSwitch.centerYAnchor.constraint( equalTo: holder.centerYAnchor), ]) NSLayoutConstraint.activate([ holder.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor) ]) } @objc func toggleZoom() { mapView.settings.zoomGestures = zoomSwitch.isOn } }
Objective-C
#import "GoogleMapsDemos/Samples/GestureControlViewController.h" #import <GoogleMaps/GoogleMaps.h> @implementation GestureControlViewController { GMSMapView *_mapView; UISwitch *_zoomSwitch; } - (void)viewDidLoad { [super viewDidLoad]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-25.5605 longitude:133.605097 zoom:3]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _mapView.accessibilityIdentifier = @"gestureControlDemoMapView"; self.view = [[UIView alloc] initWithFrame:CGRectZero]; [self.view addSubview:_mapView]; UIView *holder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 59)]; holder.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; holder.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8f]; [self.view addSubview:holder]; // Zoom label. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 200, 29)]; label.text = @"Zooming?"; label.font = [UIFont boldSystemFontOfSize:18.0f]; label.textAlignment = NSTextAlignmentLeft; label.backgroundColor = [UIColor clearColor]; label.layer.shadowColor = [[UIColor whiteColor] CGColor]; label.layer.shadowOffset = CGSizeMake(0.0f, 1.0f); label.layer.shadowOpacity = 1.0f; label.layer.shadowRadius = 0.0f; [holder addSubview:label]; // Control zooming. _zoomSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(-90, 16, 0, 0)]; _zoomSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; [_zoomSwitch addTarget:self action:@selector(didChangeZoomSwitch) forControlEvents:UIControlEventValueChanged]; _zoomSwitch.on = YES; [holder addSubview:_zoomSwitch]; } - (void)didChangeZoomSwitch { _mapView.settings.zoomGestures = _zoomSwitch.isOn; } @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 存取您的位置,請選擇「允許」。