ไปยังส่วนต่างๆ

ทำตามคำแนะนำนี้เพื่อวางแผนเส้นทางภายในแอปไปยังปลายทางเดียวโดยใช้ Navigation SDK สำหรับ iOS

ภาพรวม

  1. ผสานรวม Navigation SDK ลงในแอปตามที่อธิบายไว้ ในส่วนตั้งค่าโปรเจ็กต์
  2. กำหนดค่า GMSMapView
  3. แจ้งให้ผู้ใช้ยอมรับข้อกำหนดในการให้บริการ และให้สิทธิ์บริการตำแหน่งและการแจ้งเตือนในเบื้องหลัง
  4. สร้างอาร์เรย์ที่มีปลายทางอย่างน้อย 1 รายการ
  5. กำหนด GMSNavigator เพื่อควบคุมการนำทางแบบเลี้ยวต่อเลี้ยว

    • เพิ่มปลายทางโดยใช้ setDestinations
    • ตั้งค่า isGuidanceActive เป็น true เพื่อเริ่มการนำทาง
    • ใช้ simulateLocationsAlongExistingRoute เพื่อจำลองความคืบหน้าของยานพาหนะบนเส้นทาง สำหรับการทดสอบ การแก้ไขข้อบกพร่อง และการสาธิตแอป

ดูรหัส

แจ้งเตือนผู้ใช้เกี่ยวกับการให้สิทธิ์ที่จำเป็น

ก่อนใช้ Navigation SDK ผู้ใช้ต้องยอมรับข้อกำหนดและเงื่อนไข รวมถึงอนุญาตให้ใช้บริการตำแหน่งซึ่งจำเป็นสำหรับการนำทาง หากแอปจะทำงานอยู่เบื้องหลัง แอปต้องแจ้งให้ผู้ใช้ให้สิทธิ์การแจ้งเตือนเกี่ยวกับคำแนะนำด้วย ส่วนนี้จะแสดง วิธีแสดงข้อความแจ้งการให้สิทธิ์ที่จำเป็น

ให้สิทธิ์บริการตำแหน่ง

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 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.
   }
 }];

สร้างเส้นทางและเริ่มคําแนะนํา

หากต้องการวางแผนเส้นทาง ให้เรียกใช้เมธอด setDestinations() ของ Navigator ด้วยอาร์เรย์ที่มีปลายทางอย่างน้อย 1 แห่ง (GMSNavigationWaypoint) ที่จะเข้าชม หากคำนวณสำเร็จ เส้นทางนั้นจะแสดงบนแผนที่ หากต้องการเริ่มต้นการแนะนำในเส้นทาง ให้เริ่มจากปลายทางแรก ให้ตั้งค่า isGuidanceActive เป็น true ในโค้ดเรียกกลับ

ตัวอย่างต่อไปนี้จะแสดง

  • การสร้างเส้นทางใหม่ที่มีจุดหมาย 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;
                             }];
}

ดูข้อมูลเกี่ยวกับรหัสสถานที่ได้ที่รหัสสถานที่

ตั้งค่าโหมดการเดินทาง

โหมดการเดินทางจะกำหนดว่าจะดึงข้อมูลเส้นทางประเภทใด รวมทั้งวิธีกำหนดเส้นทางของผู้ใช้ คุณสามารถเลือกวิธีการเดินทาง 1 ใน 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;

เครื่องมือค้นหารหัสสถานที่

คุณสามารถใช้เครื่องมือค้นหารหัสสถานที่ เพื่อค้นหารหัสสถานที่เพื่อใช้เป็นปลายทางของเส้นทาง

ข้อความแบบลอย

คุณสามารถเพิ่มข้อความแบบลอยที่ตำแหน่งใดก็ได้ในแอปตราบใดที่ไม่ครอบคลุมการระบุแหล่งที่มาของ Google การนำทาง SDK ไม่รองรับการตรึงข้อความกับละติจูด/ลองจิจูดบนแผนที่หรือกับป้ายกำกับ ดูข้อมูลเพิ่มเติมได้ในหน้าต่างข้อมูล