Consumer SDK を使用して、オンデマンド配車と配達ソリューションのバックエンド サービスと統合された基本的なコンシューマー アプリを構築して実行できます。有効なルートの表示、ルート更新情報への応答、ルートエラーの処理を行うことができる、移動状況と注文状況のアプリを作成できます。
Consumer SDK はモジュール方式のアーキテクチャを採用しているため、特定のアプリに使用する API の各部分を、独自の API、Fleet Engine が提供するバックエンド サービス、Google Maps Platform の追加の API と統合できます。
最小システム要件
プロジェクト構成
Swift Package Manager
Consumer SDK は Swift Package Manager を使用してインストールできます。この SDK を追加するには、既存の Consumer SDK の依存関係をすべて削除してください。
新規または既存のプロジェクトに SDK を追加する手順は次のとおりです。
-
Xcode
project
またはworkspace
を開き、[File] > [Add Package Dependencies] に移動します。 - URL として「https://github.com/googlemaps/ios-consumer-sdk」と入力し、Enter キーを押してパッケージを取り込み、[パッケージを追加] をクリックします。
-
特定の
version
をインストールするには、[依存関係ルール] フィールドをバージョン ベースのオプションのいずれかに設定します。新しいプロジェクトの場合は、最新バージョンを指定して [正確なバージョン] オプションを使用することをおすすめします。完了したら、[Add Package] をクリックします。 -
[Choose Package Products] ウィンドウで、指定した
main
ターゲットにGoogleRidesharingConsumer
が追加されることを確認します。完了したら、[Add Package] をクリックします。 -
インストールを確認するには、ターゲットの
General
ペインに移動します。[Frameworks, Libraries, and Embedded Content] に、インストールされたパッケージが表示されます。 [Project Navigator] の [Package Dependencies] セクションで、パッケージとそのバージョンを確認することもできます。
既存のプロジェクトの package
を更新する手順は次のとおりです。
9.0.0 より前のバージョンからアップグレードする場合は、アップグレード後に
GoogleMapsBase
、GoogleMapsCore
、GoogleMapsM4B
の依存関係を削除する必要があります。GoogleMaps
の依存関係は削除しないでください。詳細については、バージョン 9.0.0 リリースノートをご覧ください。Xcode のプロジェクト構成で、[Frameworks, Libraries, and Embedded Content] を見つけます。マイナス記号(-)を使用して、次のフレームワークを削除します。
GoogleMapsBase
(9.0.0 より前のバージョンからのアップグレードのみ)GoogleMapsCore
(9.0.0 より前のバージョンからのアップグレードのみ)GoogleMapsM4B
(9.0.0 より前のバージョンからのアップグレードのみ)
- Xcode で、[File] > [Packages] > [Update To Latest Package Versions] に移動します。
- インストールを確認するには、Project Navigator の [Package Dependencies] セクションに移動し、パッケージとそのバージョンを確認します。
CocoaPods
を使用して追加された既存の Consumer SDK 依存関係を削除するには、次の手順を行います。
- Xcode ワークスペースを閉じます。ターミナルを開いて、次のコマンドを実行します。
sudo gem install cocoapods-deintegrate cocoapods-clean pod deintegrate pod cache clean --all
-
Podfile
、Podfile.resolved
、Xcode のworkspace
を CocoaPods 以外に使用していない場合は、削除します。
手動でインストールした既存の Consumer SDK を削除するには、次の手順に沿って操作します。
Xcode プロジェクトの構成設定で、フレームワーク、ライブラリ、埋め込みコンテンツを見つけます。マイナス記号
(-)
を使用して、次のフレームワークを削除します。GoogleRidesharingConsumer.xcframework
Xcode プロジェクトの最上位ディレクトリから、
GoogleRidesharingConsumer
バンドルを削除します。
CocoaPods
CocoaPods を使用して Consumer SDK を構成するには、次の項目が必要です。
CocoaPods ツール: このツールをインストールするには、ターミナルを開いて次のコマンドを実行します。
sudo gem install cocoapods
詳しくは、CocoaPods スタートガイドをご覧ください。
Consumer SDK 用の Podfile を作成し、それを使用して API とその依存関係をインストールします。まずプロジェクトディレクトリに Podfile という名前の ファイルを作成しますこのファイルでプロジェクトの依存関係を定義します。次に、Podfile を編集して依存関係を追加します。依存関係を含む例を次に示します。
source "https://github.com/CocoaPods/Specs.git" target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pod 'GoogleRidesharingConsumer' end
Podfile を保存します。ターミナルを開き、Podfile を含むディレクトリに移動します。
cd <path-to-project>
Pod インストール コマンドを実行します。これにより、Podfile で指定された API が、その依存関係(存在する場合)とともにインストールされます。
pod install
Xcode を閉じ、プロジェクトの .xcworkspace ファイルを開いて(ダブルクリックして)Xcode を起動します。後でプロジェクトを開くには、.xcworkspace ファイルを使用します。
手動インストール
XCFramework は、Consumer SDK のインストールに使用するバイナリ パッケージです。このパッケージは、M1 チップセットを搭載したマシンなど、複数のプラットフォームで使用できます。このガイドでは、Consumer SDK を含む XCFramework をプロジェクトに手動で追加し、Xcode でビルド設定を構成する方法について説明します。
SDK バイナリとリソースをダウンロードします。
圧縮されたファイルを解凍して、XCFramework とリソースにアクセスします。
Xcode を起動し、既存のプロジェクトを開くか、新しいプロジェクトを作成します。iOS を初めて使用する場合は、新しいプロジェクトを作成し、iOS アプリ テンプレートを選択します。
プロジェクト グループにフレームワーク グループがまだ存在しない場合は、作成します。
Consumer SDK をインストールするには、[Frameworks, Libraries, and Embedded Content] の下のプロジェクトに
GoogleRidesharingConsumer.xcframework
ファイルをドラッグします。プロンプトが表示されたら、必要に応じて [Copy items] を選択します。ダウンロードした
GoogleRidesharingConsumer.bundle
を Xcode プロジェクトの最上位ディレクトリにドラッグします。プロンプトが表示されたら、[Copy items if needed
] を選択します。プロジェクト ナビゲータからプロジェクトを選択し、アプリケーションのターゲットを選択します。
[Build Phases] タブを開き、[Link Binary with Libraries] で、次のフレームワークとライブラリを追加します(まだ存在しない場合)。
Accelerate.framework
CoreData.framework
CoreGraphics.framework
CoreImage.framework
CoreLocation.framework
CoreTelephony.framework
CoreText.framework
GLKit.framework
ImageIO.framework
libc++.tbd
libz.tbd
Metal.framework
OpenGLES.framework
QuartzCore.framework
SystemConfiguration.framework
UIKit.framework
特定のターゲットではなくプロジェクトを選択し、[Build Settings] タブを開きます。[Other Linker Flags] セクションで、デバッグとリリースの両方に
-ObjC
を追加します。これらの設定が表示されない場合は、[Build Settings] バーのフィルタを [Basic] から [All] に変更します。
Apple Privacy Manifest ファイルを調べる
Apple が App Store のアプリについては、アプリのプライバシーに関する詳細情報を必要とします。最新情報などについては、Apple App Store のプライバシーの詳細ページをご覧ください。
Apple Privacy Manifest ファイルは、SDK のリソース バンドルに含まれています。プライバシー マニフェスト ファイルが含まれていることを確認し、その内容を検査するには、アプリのアーカイブを作成し、そのアーカイブからプライバシー レポートを生成します。
アプリケーションの統合
認証トークンを指定する
ユーザー アプリが Fleet Engine にルート更新情報をリクエストする場合、そのリクエストに有効なアクセス トークンを含める必要があります。これらのリクエストを承認して認証するために、Consumer SDK は GMTCAuthorization
プロトコルに準拠するオブジェクトを呼び出します。このオブジェクトは、必要なアクセス トークンを提供する役割を担います。
トークンの生成方法は、アプリ デベロッパーが選択します。実装では、次のことができる必要があります。
- HTTPS サーバーからアクセス トークン(JSON 形式など)をフェッチします。
- トークンを解析してキャッシュに保存します。
- 期限切れになったらトークンを更新します。
Fleet Engine サーバーが必要とするトークンの詳細については、承認用の JSON Web Token(JWT)の作成をご覧ください。
プロバイダ ID は Google Cloud プロジェクト ID と同じです。詳細については、Fleet Engine スタートガイドをご覧ください。
次の例では、アクセス トークン プロバイダを実装しています。
Swift
/*
* SampleAccessTokenProvider.swift
*/
import GoogleRidesharingConsumer
private let providerURL = "INSERT_YOUR_TOKEN_PROVIDER_URL"
class SampleAccessTokenProvider: NSObject, GMTCAuthorization {
private struct AuthToken {
// The cached trip token.
let token: String
// Keep track of when the token expires for caching.
let expiration: TimeInterval
// Keep track of the trip ID the cached token is for.
let tripID: String
}
enum AccessTokenError: Error {
case missingAuthorizationContext
case missingData
}
private var authToken: AuthToken?
func fetchToken(
with authorizationContext: GMTCAuthorizationContext?,
completion: @escaping GMTCAuthTokenFetchCompletionHandler
) {
// Get the trip ID from the authorizationContext. This is set by the Consumer SDK.
guard let authorizationContext = authorizationContext else {
completion(nil, AccessTokenError.missingAuthorizationContext)
return
}
let tripID = authorizationContext.tripID
// If appropriate, use the cached token.
if let authToken = authToken,
authToken.expiration > Date.now.timeIntervalSince1970 && authToken.tripID == tripID
{
completion(authToken.token, nil)
return
}
// Otherwise, try to fetch a new token from your server.
let request = URLRequest(url: URL(string: providerURL))
let task = URLSession.shared.dataTask(with: request) { [weak self] data, _, error in
guard let strongSelf = self else { return }
guard error == nil else {
completion(nil, error)
return
}
// Replace the following key values with the appropriate keys based on your
// server's expected response.
let tripTokenKey = "TRIP_TOKEN_KEY"
let tokenExpirationKey = "TOKEN_EXPIRATION"
guard let data = data,
let fetchData = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let token = fetchData[tripTokenKey] as? String,
let expiration = fetchData[tokenExpirationKey] as? Double
else {
completion(nil, AccessTokenError.missingData)
return
}
strongSelf.authToken = AuthToken(token: token, expiration: expiration, tripID: tripID)
completion(token, nil)
}
task.resume()
}
}
Objective-C
/*
* SampleAccessTokenProvider.h
*/
#import <Foundation/Foundation.h>
#import <GoogleRidesharingConsumer/GoogleRidesharingConsumer.h>
NS_ASSUME_NONNULL_BEGIN
@interface SampleAccessTokenProvider : NSObject <GMTCAuthorization>
@end
NS_ASSUME_NONNULL_END
/*
* SampleAccessTokenProvider.m
*/
#import "SampleAccessTokenProvider.h"
#import "GoogleRidesharingConsumer/GoogleRidesharingConsumer.h"
static NSString *const PROVIDER_URL = @"INSERT_YOUR_TOKEN_PROVIDER_URL";
// SampleAccessTokenProvider.m
@implementation SampleAccessTokenProvider {
// The cached token with claims to the current trip.
NSString *_cachedTripToken;
// Keep track of the Trip ID the cached token is for.
NSString *_lastKnownTripID;
// Keep track of when tokens expire for caching.
NSTimeInterval _tokenExpiration;
}
- (void)fetchTokenWithContext:(nullable GMTCAuthorizationContext *)authorizationContext
completion:(nonnull GMTCAuthTokenFetchCompletionHandler)completion {
// Get the trip ID from the authorizationContext. This is set by the Consumer SDK.
NSString *tripID = authorizationContext.tripID;
// Clear cached trip token if trip ID has changed.
if (![_lastKnownTripID isEqual:tripID]) {
_tokenExpiration = 0.0;
_cachedTripToken = nil;
}
_lastKnownTripID = tripID;
// Clear cached tripToken if it has expired.
if ([[NSDate date] timeIntervalSince1970] > _tokenExpiration) {
_cachedTripToken = nil;
}
// If appropriate, use the cached token.
if (_cachedTripToken) {
completion(_cachedTripToken, nil);
return;
}
// Otherwise, try to fetch a new token from your server.
NSURL *requestURL = [NSURL URLWithString:PROVIDER_URL];
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL:requestURL];
request.HTTPMethod = @"GET";
// Replace the following key values with the appropriate keys based on your
// server's expected response.
NSString *tripTokenKey = @"TRIP_TOKEN_KEY";
NSString *tokenExpirationKey = @"TOKEN_EXPIRATION";
__weak typeof(self) weakSelf = self;
void (^handler)(NSData *_Nullable data, NSURLResponse *_Nullable response,
NSError *_Nullable error) =
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
typeof(self) strongSelf = weakSelf;
if (error) {
completion(nil, error);
return;
}
NSError *JSONError;
NSMutableDictionary *JSONResponse =
[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&JSONError];
if (JSONError) {
completion(nil, JSONError);
return;
} else {
// Sample code only. No validation logic.
id expirationData = JSONResponse[tokenExpirationKey];
if ([expirationData isKindOfClass:[NSNumber class]]) {
NSTimeInterval expirationTime = ((NSNumber *)expirationData).doubleValue;
strongSelf->_tokenExpiration = [[NSDate date] timeIntervalSince1970] + expirationTime;
}
strongSelf->_cachedTripToken = JSONResponse[tripTokenKey];
completion(JSONResponse[tripTokenKey], nil);
}
};
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *mainQueueURLSession =
[NSURLSession sessionWithConfiguration:config delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *task = [mainQueueURLSession dataTaskWithRequest:request completionHandler:handler];
[task resume];
}
@end
アプリケーションの初期化
Swift
/*
* AppDelegate.swift
*/
import GoogleRidesharingConsumer
import GoogleMaps
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Register your API key for GMSServices.
GMSServices.provideAPIKey(yourMapsAPIKey)
// Set the instance of the SampleAccessTokenProvider.
GMTCServices.setAccessTokenProvider(SampleAccessTokenProvider(), providerID: yourProviderID)
// Other initialization code ...
return true
}
}
Objective-C
/*
* AppDelegate.m
*/
#import <GoogleMaps/GoogleMaps.h>
#import <GoogleRidesharingConsumer/GoogleRidesharingConsumer.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Register your API key for GMSServices.
[GMSServices provideAPIKey:yourMapsAPIKey];
//Set the instance of the AccessTokenFactory.
[GMTCServices setAccessTokenProvider:[[SampleAccessTokenProvider alloc] init]
providerID:yourProviderID];
// Other initialization code ...
return YES;
}
@end
マップビューの統合
マップビューを初期化する
次の例は、GMTCMapView
を初期化する方法を示しています。
Swift
/*
* MapViewController.swift
*/
class ViewController: UIViewController, GMTCMapViewDelegate {
private var rideSharingMap: GMTCMapView?
override func viewDidLoad() {
super.viewDidLoad()
self.rideSharingMap = GMTCMapView(frame: UIScreen.main.bounds)
self.rideSharingMap.delegate = self
self.rideSharingMap?.settings.myLocationButton = true
self.view.addSubview(self.rideSharingMap!)
...
}
Objective-C
/*
* MapViewController.h
*/
@interface MapViewController : UIViewController<GMTCMapViewDelegate>
...
@end
/*
* MapViewController.m
*/
@implementation MapViewController
- (void)viewDidLoad {
[super viewDidLoad];
...
self.mapView = [[GMTCMapView alloc] initWithFrame:CGRectZero];
self.mapView.settings.myLocationButton = YES;
self.mapView.delegate = self;
...
}
...
@end
マップビュー イベントを処理する
次の例は、お客様の状態のイベントを処理するデリゲートの実装方法を示しています。
Swift
func mapViewDidInitialize(_ mapview: GMTCMapView) {
// Handle the update to the state of the map view to browsing.
}
func mapView(_ mapView: GMSMapView, didTapConsumerMarker mapMarker: GMSMarker, markerType: GMTCMapViewMarkerType) -> Bool {
// Handle the mapView marker was tapped.
}
Objective-C
/*
* MapViewController.m
*/
#pragma mark - GMTCMapViewDelegate implementation
// Handle state update of map view.
- (void)mapViewDidInitializeCustomerState:(GMTCMapView *)mapview {
// Handle the update to the state of the map view to browsing.
}
- (void)mapView:(GMSMapView *)mapView
didTapConsumerMarker:(nonnull GMSMarker *)mapMarker
markerType:(GMTCMapViewMarkerType)markerType {
// Handle the mapView marker was tapped.
}
移動経路の共有
ビューが読み込まれたときに新しいルートを開始する
次の例は、ビューが読み込まれた直後にジャーニーの共有を開始する方法を示しています。降車場所や乗車場所などのすべてのユーザー入力を ViewController
から収集し、新しい ViewController
を作成して乗車を直接共有できます。
Swift
/*
* MapViewController.swift
*/
override func viewDidLoad() {
super.viewDidLoad()
...
self.mapView = GMTCMapView(frame: UIScreen.main.bounds)
self.mapView.delegate = self
self.view.addSubview(self.mapView)
}
func mapViewDidInitializeCustomerState(_: GMTCMapView) {
self.mapView.pickupLocation = self.selectedPickupLocation
self.mapView.dropoffLocation = self.selectedDropoffLocation
self.startConsumerMatchWithLocations(
pickupLocation: self.mapView.pickupLocation!,
dropoffLocation: self.mapView.dropoffLocation!
) { [weak self] (tripName, error) in
guard let strongSelf = self else { return }
if error != nil {
// print error message.
return
}
let tripService = GMTCServices.shared().tripService
// Create a tripModel instance for listening the update of the trip
// specified by this trip name.
let tripModel = tripService.tripModel(forTripName: tripName)
// Create a journeySharingSession instance based on the tripModel
let journeySharingSession = GMTCJourneySharingSession(tripModel: tripModel)
// Add the journeySharingSession instance on the mapView for UI updating.
strongSelf.mapView.show(journeySharingSession)
// Register for the trip update events.
tripModel.register(strongSelf)
strongSelf.currentTripModel = tripModel
strongSelf.currentJourneySharingSession = journeySharingSession
strongSelf.hideLoadingView()
}
self.showLoadingView()
}
Objective-C
/*
* MapViewController.m
*/
- (void)viewDidLoad {
[super viewDidLoad];
...
self.mapView = [[GMTCMapView alloc] initWithFrame:CGRectZero];
self.mapView.delegate = self;
[self.view addSubview:self.mapView];
}
// Handle the callback when the GMTCMapView did initialized.
- (void)mapViewDidInitializeCustomerState:(GMTCMapView *)mapview {
self.mapView.pickupLocation = self.selectedPickupLocation;
self.mapView.dropoffLocation = self.selectedDropoffLocation;
__weak __typeof(self) weakSelf = self;
[self startTripBookingWithPickupLocation:self.selectedPickupLocation
dropoffLocation:self.selectedDropoffLocation
completion:^(NSString *tripName, NSError *error) {
__typeof(self) strongSelf = weakSelf;
GMTCTripService *tripService = [GMTCServices sharedServices].tripService;
// Create a tripModel instance for listening to updates to the trip specified by this trip name.
GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];
// Create a journeySharingSession instance based on the tripModel.
GMTCJourneySharingSession *journeySharingSession =
[[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];
// Add the journeySharingSession instance on the mapView for updating the UI.
[strongSelf.mapView showMapViewSession:journeySharingSession];
// Register for trip update events.
[tripModel registerSubscriber:self];
strongSelf.currentTripModel = tripModel;
strongSelf.currentJourneySharingSession = journeySharingSession;
[strongSelf hideLoadingView];
}];
[self showLoadingView];
}
実施中のルートをキャンセルする
次の例は、現在アクティブなルートをリセットする方法を示しています。
Swift
/*
* MapViewController.swift
*/
func cancelCurrentActiveTrip() {
// Stop the tripModel
self.currentTripModel.unregisterSubscriber(self)
// Remove the journey sharing session from the mapView's UI stack.
self.mapView.hide(journeySharingSession)
}
Objective-C
/*
* MapViewController.m
*/
- (void)cancelCurrentActiveTrip {
// Stop the tripModel
[self.currentTripModel unregisterSubscriber:self];
// Remove the journey sharing session from the mapView's UI stack.
[self.mapView hideMapViewSession:journeySharingSession];
}
ルート更新情報を聞く
次の例は、tripModel
コールバックを登録する方法を示しています。
Swift
/*
* MapViewController.swift
*/
override func viewDidLoad() {
super.viewDidLoad()
// Register for trip update events.
self.currentTripModel.register(self)
}
Objective-C
/*
* MapViewController.m
*/
- (void)viewDidLoad {
[super viewDidLoad];
// Register for trip update events.
[self.currentTripModel registerSubscriber:self];
...
}
次の例は、tripModel
コールバックの登録をキャンセルする方法を示しています。
Swift
/*
* MapViewController.swift
*/
deinit {
self.currentTripModel.unregisterSubscriber(self)
}
Objective-C
/*
* MapViewController.m
*/
- (void)dealloc {
[self.currentTripModel unregisterSubscriber:self];
...
}
次の例は、GMTCTripModelSubscriber
プロトコルを実装して、ルート状態が更新された場合にコールバックを処理する方法を示しています。
Swift
/*
* MapViewController.swift
*/
func tripModel(_: GMTCTripModel, didUpdate trip: GMTSTrip?, updatedPropertyFields: GMTSTripPropertyFields) {
// Update the UI with the new `trip` data.
self.updateUI(with: trip)
}
func tripModel(_: GMTCTripModel, didUpdate tripStatus: GMTSTripStatus) {
// Handle trip status did change.
}
func tripModel(_: GMTCTripModel, didUpdateActiveRouteRemainingDistance activeRouteRemainingDistance: Int32) {
// Handle remaining distance of active route did update.
}
func tripModel(_: GMTCTripModel, didUpdateActiveRoute activeRoute: [GMTSLatLng]?) {
// Handle trip active route did update.
}
func tripModel(_: GMTCTripModel, didUpdate vehicleLocation: GMTSVehicleLocation?) {
// Handle vehicle location did update.
}
func tripModel(_: GMTCTripModel, didUpdatePickupLocation pickupLocation: GMTSTerminalLocation?) {
// Handle pickup location did update.
}
func tripModel(_: GMTCTripModel, didUpdateDropoffLocation dropoffLocation: GMTSTerminalLocation?) {
// Handle drop off location did update.
}
func tripModel(_: GMTCTripModel, didUpdatePickupETA pickupETA: TimeInterval) {
// Handle the pickup ETA did update.
}
func tripModel(_: GMTCTripModel, didUpdateDropoffETA dropoffETA: TimeInterval) {
// Handle the drop off ETA did update.
}
func tripModel(_: GMTCTripModel, didUpdateRemaining remainingWaypoints: [GMTSTripWaypoint]?) {
// Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}
func tripModel(_: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
// Handle the error.
}
func tripModel(_: GMTCTripModel, didUpdateIntermediateDestinations intermediateDestinations: [GMTSTerminalLocation]?) {
// Handle the intermediate destinations being updated.
}
func tripModel(_: GMTCTripModel, didUpdateActiveRouteTraffic activeRouteTraffic: GMTSTrafficData?) {
// Handle trip active route traffic being updated.
}
Objective-C
/*
* MapViewController.m
*/
#pragma mark - GMTCTripModelSubscriber implementation
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateTrip:(nullable GMTSTrip *)trip
updatedPropertyFields:(enum GMTSTripPropertyFields)updatedPropertyFields {
// Update the UI with the new `trip` data.
[self updateUIWithTrip:trip];
...
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdateTripStatus:(enum GMTSTripStatus)tripStatus {
// Handle trip status did change.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateActiveRouteRemainingDistance:(int32_t)activeRouteRemainingDistance {
// Handle remaining distance of active route did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateActiveRoute:(nullable NSArray<GMTSLatLng *> *)activeRoute {
// Handle trip active route did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateVehicleLocation:(nullable GMTSVehicleLocation *)vehicleLocation {
// Handle vehicle location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdatePickupLocation:(nullable GMTSTerminalLocation *)pickupLocation {
// Handle pickup location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateDropoffLocation:(nullable GMTSTerminalLocation *)dropoffLocation {
// Handle drop off location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdatePickupETA:(NSTimeInterval)pickupETA {
// Handle the pickup ETA did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateRemainingWaypoints:(nullable NSArray<GMTSTripWaypoint *> *)remainingWaypoints {
// Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdateDropoffETA:(NSTimeInterval)dropoffETA {
// Handle the drop off ETA did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(nullable NSError *)error {
// Handle the error.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateIntermediateDestinations:
(nullable NSArray<GMTSTerminalLocation *> *)intermediateDestinations {
// Handle the intermediate destinations being updated.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateActiveRouteTraffic:(nullable GMTSTrafficData *)activeRouteTraffic {
// Handle trip active route traffic being updated.
}
エラー処理
tripModel を登録したときにエラーが発生した場合は、デリゲート メソッド tripModel(_:didFailUpdateTripWithError:)
を実装して tripModel のコールバックを取得できます。Fleet Engine は Google Cloud のエラー標準に
従ったエラーメッセージを生成します詳細なエラー メッセージの定義とすべてのエラーコードについては、Google Cloud エラーのドキュメントをご覧ください。
具体的には、ルート監視の場合、有効な認証トークンを指定する必要があります。401 UNAUTHENTICATED
は、有効な認証情報がない場合(トークンの期限切れなど)に送出されます。403 PERMISSION_DENIED
は、呼び出し元に特定の API を呼び出す権限がない場合(コンシューマのロールを持つユーザーが updateTrip を呼び出そうとした場合など)か、リクエストに JWT トークンに有効な vehicle_id/trip_id がない場合に送出されます。
詳細については、コンシューマ SDK のエラー処理をご覧ください。
UI のカスタマイズ
カスタム ポリラインの UI オプションを取得して設定する
次の例は、ポリラインのカスタム UI オプションを設定する方法を示しています。
Swift
/** MapViewController.swift */
func updatePolylineUIOptions() {
// The polyline type that you would like to set custom UI options for.
let customizablePolylineType = GMTCPolylineType.activeRoute
let polylineStyleOptions = GMTCMutablePolylineStyleOptions()
polylineStyleOptions.strokeWidth = 8.0
polylineStyleOptions.strokeColor = .blue
polylineStyleOptions.isVisible = true
polylineStyleOptions.zIndex = 1000
polylineStyleOptions.isGeodesic = true
let coordinator = self.mapView.consumerMapStyleCoordinator
coordinator.setPolylineStyleOptions(polylineStyleOptions, polylineType:customizablePolylineType)
}
Objective-C
/** MapViewController.m */
- (void)updatePolylineUIOptions {
// The polyline type that you would like to set custom UI options for.
GMTCPolylineType customizablePolylineType = GMTCPolylineTypeActiveRoute;
GMTCMutablePolylineStyleOptions *polylineStyleOptions =
[[GMTCMutablePolylineStyleOptions alloc] init];
polylineStyleOptions.strokeWidth = 8.0;
polylineStyleOptions.strokeColor = [UIColor blueColor];
polylineStyleOptions.isVisible = YES;
polylineStyleOptions.zIndex = 1000;
polylineStyleOptions.isGeodesic = YES;
[[_mapView consumerMapStyleCoordinator] setPolylineStyleOptions:polylineStyleOptions
polylineType:customizablePolylineType];
}
カスタム マーカーの UI オプションを取得、設定する
次の例は、マーカーのカスタム UI オプションを設定する方法を示しています。
Swift
/** MapViewController.swift */
func updateMarkerUIOptions() {
let customizableMarkerType = GMTCCustomizableMarkerType.tripVehicle
let markerStyleOptions = GMTCMutableMarkerStyleOptions()
markerStyleOptions.groundAnchor = groundAnchor
markerStyleOptions.isVisible = true
markerStyleOptions.icon = icon
markerStyleOptions.zIndex = 100
markerStyleOptions.isFlat = false
let coordinator = self.mapView.consumerMapStyleCoordinator
coordinator.setMarkerStyleOptions(markerStyleOptions, markerType: customizableMarkerType)
}
Objective-C
/** MapViewController.m */
- (void)updateMarkerUIOptions {
// The marker type that you would like to set custom UI options for.
GMTCCustomizableMarkerType customizableMarkerType = GMTCCustomizableMarkerTypeTripVehicle;
GMTCMutableMarkerStyleOptions *markerStyleOptions =
[[GMTCMutableMarkerStyleOptions alloc] init];
markerStyleOptions.groundAnchor = groundAnchor;
markerStyleOptions.isVisible = YES;
markerStyleOptions.icon = icon;
markerStyleOptions.zIndex = 100;
markerStyleOptions.isFlat = NO;
[[_mapView consumerMapStyleCoordinator] setMarkerStyleOptions:markerStyleOptions markerType:customizableMarkerType];
}
カメラのズームの調整
Maps SDK for iOS の現在地ボタンを使用すると、デバイスの位置にカメラを合わせます。
アクティブな移動経路の共有セッションがある場合は、デバイスの位置情報だけでなく、移動経路にカメラの中心を合わせることができます。
Consumer SDK には、デフォルトで有効になっている自動カメラ機能があります。移動経路の共有経路と次の停車地点にフォーカスが合わせ、カメラがズームします。
カメラの動作をより細かく制御する必要がある場合は、isAllowCameraAutoUpdate
プロパティを使用して自動カメラ機能を無効または有効にできます。
カメラの詳細なカスタマイズについては、Maps SDK for iOS カメラを移動するをご覧ください。