このガイドでは、Navigation SDK for iOS を使用して、アプリ内の 1 つのデスティネーションへのルートをプロットします。
概要
- プロジェクトをセットアップするの説明に沿って、Navigation SDK をアプリに統合します。
GMSMapView
を設定します。- 利用規約への同意をユーザーに求め、位置情報サービスとバックグラウンド通知を承認します。
- 1 つ以上のデスティネーションを含む配列を作成します。
ターンバイターン方式のナビゲーションを制御する
GMSNavigator
を定義します。setDestinations
を使用してデスティネーションを追加します。- ナビゲーションを開始するには、
isGuidanceActive
をtrue
に設定します。 simulateLocationsAlongExistingRoute
を使用してルート上の車両の進行状況をシミュレートし、アプリのテスト、デバッグ、デモを行います。
コードの確認
ユーザーに必要な承認を求める
Navigation SDK を使用する前に、ユーザーは利用規約に同意し、ナビゲーションに必要な位置情報サービスの使用を承認する必要があります。アプリをバックグラウンドで実行する場合は、ガイダンス アラート通知の承認をユーザーに求める必要があります。このセクションでは、必要な承認プロンプトを表示する方法について説明します。
位置情報サービスを承認する
Navigation SDK は位置情報サービスを使用しますが、これにはユーザー認証が必要です。位置情報サービスを有効にして認証ダイアログを表示する手順は次のとおりです。
NSLocationAlwaysUsageDescription
キーをInfo.plist
に追加します。値として、アプリが位置情報サービスを必要とする理由の簡単な説明を追加します。例: 「このアプリには、ターンバイターン方式のナビで位置情報サービスを使用する権限が必要です。」
承認ダイアログを表示するには、ロケーション マネージャー インスタンスの
requestAlwaysAuthorization()
を呼び出します。
Swift
self.locationManager.requestAlwaysAuthorization()
Objective-C
[_locationManager requestAlwaysAuthorization];
バックグラウンド ガイダンスのアラート通知を承認する
Navigation SDK には、アプリがバックグラウンドで実行されているときにアラートを通知するためのユーザー権限が必要です。次のコードを追加して、これらの通知を表示する許可をユーザーに求めます。
Swift
UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) {
granted, error in
// Handle denied authorization to display notifications.
if !granted || error != nil {
print("User rejected request to display notifications.")
}
}
Objective-C
// Request authorization for alert notifications.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert;
[center requestAuthorizationWithOptions:options
completionHandler:
^(
BOOL granted,
NSError *_Nullable error) {
if (!error && granted) {
NSLog(@"iOS Notification Permission: newly Granted");
} else {
NSLog(@"iOS Notification Permission: Failed or Denied");
}
}];
利用規約に同意する
次のコードを使用して利用規約のダイアログを表示し、ユーザーが利用規約に同意したときにナビゲーションを有効にします。この例には、前述した位置情報サービスとガイダンス アラート通知のコードが含まれています。
Swift
let companyName = "Ride Sharing Co."
GMSNavigationServices.showTermsAndConditionsDialogIfNeeded(
withCompanyName: companyName) { termsAccepted in
if termsAccepted {
// Enable navigation if the user accepts the terms.
self.mapView.isNavigationEnabled = true
self.mapView.settings.compassButton = true
// Request authorization to use location services.
self.locationManager.requestAlwaysAuthorization()
// Request authorization for alert notifications which deliver guidance instructions
// in the background.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) {
granted, error in
// Handle rejection of notification authorization.
if !granted || error != nil {
print("Authorization to deliver notifications was rejected.")
}
}
} else {
// Handle rejection of terms and conditions.
}
}
Objective-C
NSString *companyName = @"Ride Sharing Co.";
[GMSNavigationServices
showTermsAndConditionsDialogIfNeededWithCompanyName:companyName
callback:^(BOOL termsAccepted) {
if (termsAccepted) {
// Enable navigation if the user accepts the terms.
_mapView.navigationEnabled = YES;
_mapView.settings.compassButton = YES;
// Request authorization to use the current device location.
[_locationManager requestAlwaysAuthorization];
// Request authorization for alert notifications which deliver guidance instructions
// in the background.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert;
[center requestAuthorizationWithOptions:options
completionHandler:
^(
BOOL granted,
NSError *_Nullable error) {
if (!error && granted) {
NSLog(@"iOS Notification Permission: newly Granted");
} else {
NSLog(@"iOS Notification Permission: Failed or Denied");
}
}];
} else {
// Handle rejection of the terms and conditions.
}
}];
ルートを作成して案内を開始する
ルートを描画するには、1 つ以上の目的地(GMSNavigationWaypoint
)を含む配列を指定して Navigator の setDestinations()
メソッドを呼び出します。正常に計算されると、ルートが地図に表示されます。ルートに沿って案内を開始するには、最初のデスティネーションから開始して、コールバックで isGuidanceActive
を true
に設定します。
次の例は、以下の条件に従って表示します。
- 2 つの目的地を持つ新しいルートを作成する。
- ガイダンスを開始しています。
- バックグラウンド ガイダンスの通知を有効にしています。
- ルートに沿った移動のシミュレーション(省略可)。
- カメラモードを「follow」に設定する(省略可)。
Swift
func startNav() {
var destinations = [GMSNavigationWaypoint]()
destinations.append(GMSNavigationWaypoint.init(placeID: "ChIJnUYTpNASkFQR_gSty5kyoUk",
title: "PCC Natural Market")!)
destinations.append(GMSNavigationWaypoint.init(placeID:"ChIJJ326ROcSkFQRBfUzOL2DSbo",
title:"Marina Park")!)
mapView.navigator?.setDestinations(destinations) { routeStatus in
self.mapView.navigator?.isGuidanceActive = true
self.mapView.locationSimulator?.simulateLocationsAlongExistingRoute()
self.mapView.cameraMode = .following
}
}
Objective-C
- (void)startNav {
NSArray<GMSNavigationWaypoint *> *destinations =
@[[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJnUYTpNASkFQR_gSty5kyoUk"
title:@"PCC Natural Market"],
[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJJ326ROcSkFQRBfUzOL2DSbo"
title:@"Marina Park"]];
[_mapView.navigator setDestinations:destinations
callback:^(GMSRouteStatus routeStatus){
[_mapView.locationSimulator simulateLocationsAlongExistingRoute];
_mapView.navigator.guidanceActive = YES;
_mapView.cameraMode = GMSNavigationCameraModeFollowing;
}];
}
場所 ID について詳しくは、場所 ID をご覧ください。
移動手段の設定
取得されるルートの種類とユーザーのコースは、移動手段によって決まります。ルートには、車、自転車、徒歩、タクシーの 4 つの移動手段のいずれかを設定できます。運転モードやタクシーモードでは、ユーザーのコースは移動方向に基づいて決まります。サイクリング モードとウォーキング モードでは、デバイスが向いている方向でコースを表します。
次の例のように、地図ビューの travelMode
プロパティを設定します。
Swift
self.mapView.travelMode = .cycling
Objective-C
_mapView.travelMode = GMSNavigationTravelModeCycling;
回避する道路を設定
ルート沿いの高速道路や有料道路を避けるには、avoidsHighways
および avoidsTolls
BOOL
プロパティを使用します。
Swift
self.mapView.navigator?.avoidsTolls = true
Objective-C
_mapView.navigator.avoidsTolls = YES;
PlaceID ファインダー
PlaceID Finder を使用すると、経路の目的地に使用するプレイス ID を検索できます。GMSNavigationWaypoint
を使用して、placeID
からデスティネーションを追加します。
フローティング テキスト
フローティング テキストは、Google アトリビューションでカバーされていない限り、アプリ内のどこにでも追加できます。Navigation SDK では、テキストを地図上の緯度/経度やラベルに固定することはできません。詳しくは、情報ウィンドウをご覧ください。