ルートを移動する

このガイドに沿って、アプリ内で 1 つのデスティネーションへのルートをプロットします。 ナビゲーション SDK for iOS を使用します。

概要

  1. Navigation SDK をアプリに統合する (プロジェクトの設定セクション)をクリックします。
  2. GMSMapView を設定します。
  3. 利用規約への同意と位置情報の承認をユーザーに求める バックグラウンド通知を管理できます。
  4. 1 つ以上のデスティネーションを含む配列を作成します。
  5. GMSNavigator を定義する ターンバイターン方式ナビを操作します。

    • setDestinations を使用してデスティネーションを追加します。
    • isGuidanceActive を設定します。 ナビを開始するには、trueまで。
    • simulateLocationsAlongExistingRoute を使用する テストのためにルート上の車両の 進捗状況をシミュレートしたり デバッグ、アプリのデモを行うのに役立ちます。
で確認できます。

コードの確認

ユーザーに必要な承認を求める

Navigation SDK を使用する前に、ユーザーは以下に同意する必要があります。 使用を許諾するとともに、Google の提供する位置情報サービス、 必要があります。アプリをバックグラウンドで実行する場合は、 ガイダンス アラート通知を承認するようユーザーに促します。このセクションの内容 必要な承認プロンプトの表示方法を説明します。

位置情報サービスを承認する

Navigation SDK は位置情報サービスを使用します。位置情報サービスには、 あります。位置情報サービスを有効にして承認画面を表示する 手順は次のとおりです。

  1. NSLocationAlwaysUsageDescription キーを Info.plist に追加します。
  2. 値として、アプリが位置情報を必要とする理由について簡単な説明を追加してください 提供します。例: 「このアプリは、以下の目的で位置情報サービスを使用する権限が必要です。 ターンバイターン方式のナビを利用できます。」

  3. 承認ダイアログを表示するには、次の 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 termsAndConditionsOptions = GMSNavigationTermsAndConditionsOptions(companyName: "Ride Sharing Co.")

  GMSNavigationServices.showTermsAndConditionsDialogIfNeeded(
    with: termsAndConditionsOptions) { 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

GMSNavigationTermsAndConditionsOptions *termsAndConditionsOptions = [[GMSNavigationTermsAndConditionsOptions alloc] initWithCompanyName:@"Ride Sharing Co."];

[GMSNavigationServices
  showTermsAndConditionsDialogIfNeededWithOptions:termsAndConditionsOptions
  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.
   }
 }];

ルートを作成して案内を開始する

ルートをプロットするには、Navigator の setDestinations() メソッドを配列で呼び出します。 訪問する目的地(GMSNavigationWaypoint)を 1 つ以上含む。成功した場合 ルートは地図上に表示されますルート上でガイダンスを開始するには、 最初のデスティネーションから、isGuidanceActivetrue 呼び出すことができます。

次の例は、以下の条件に従って表示します。

  • 2 つの目的地を持つ新しいルートを作成する。
  • ガイダンスを開始しています。
  • バックグラウンド ガイダンスの通知を有効にしています。
  • ルートに沿った移動のシミュレーション(省略可)。
  • カメラモードを「フォロー」に設定する(省略可)。

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 種類の移動手段のいずれかを route: 車、自転車、徒歩、タクシー運転モードとタクシーモードでは、 移動の方向に基づきます。サイクリングモードとウォーキングモードでは コースは 方向で表されます。

travelMode を設定します。 プロパティを使用します。

Swift

self.mapView.travelMode = .cycling

Objective-C

_mapView.travelMode = GMSNavigationTravelModeCycling;

回避する道路を設定

avoidsHighwaysavoidsTollsBOOL プロパティを使用して、 高速道路や有料道路です

Swift

self.mapView.navigator?.avoidsTolls = true

Objective-C

_mapView.navigator.avoidsTolls = YES;

PlaceID ファインダー

PlaceID Finder を使用できます。 を使用して、経路の目的地に使用するプレイス ID を見つけます。GMSNavigationWaypoint を使用して、placeID からデスティネーションを追加します。

フローティング テキスト

フローティング テキストは、Google アトリビューションは対象外ですNavigation SDK は、 緯度と経度、またはラベルに追加できます。詳しくは 情報ウィンドウをご覧ください。