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

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

ภาพรวม

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

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

ดูโค้ด

แจ้งผู้ใช้เพื่อขอสิทธิ์ที่จำเป็น

ก่อนที่จะใช้ 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 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.
   }
 }];

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

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

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

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

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

ตั้งค่ารูปแบบการเดินทาง

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

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

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