ทำตามคำแนะนำนี้เพื่อวางแผนเส้นทางภายในแอปไปยังจุดหมายเดียว โดยใช้ Navigation SDK สำหรับ iOS
ภาพรวม
- ผสานรวม Navigation SDK เข้ากับแอปตามที่อธิบายไว้ในส่วนตั้งค่าโปรเจ็กต์
- กำหนดค่า
GMSMapView - แจ้งให้ผู้ใช้ยอมรับข้อกำหนดและเงื่อนไข รวมถึงให้สิทธิ์บริการตำแหน่ง และการแจ้งเตือนในเบื้องหลัง
- สร้างอาร์เรย์ที่มีปลายทางอย่างน้อย 1 แห่ง
กำหนด
GMSNavigatorเพื่อควบคุมการนำทางแบบเลี้ยวต่อเลี้ยว- เพิ่มปลายทางโดยใช้
setDestinations - ตั้งค่า
isGuidanceActiveเป็นtrueเพื่อเริ่มการนำทาง - ใช้
simulateLocationsAlongExistingRouteเพื่อจำลองความคืบหน้าของยานพาหนะตามเส้นทางสำหรับการทดสอบ การแก้ไขข้อบกพร่อง และการสาธิตแอป
- เพิ่มปลายทางโดยใช้
ดูรหัส
แจ้งให้ผู้ใช้ให้สิทธิ์ที่จำเป็น
ก่อนใช้ Navigation SDK ผู้ใช้ต้องยอมรับ ข้อกำหนดและเงื่อนไข และให้สิทธิ์การใช้บริการตำแหน่ง ซึ่งเป็นสิ่ง ที่จำเป็นสำหรับการนำทาง หากแอปจะทำงานในเบื้องหลัง แอปจะต้อง แจ้งให้ผู้ใช้ให้สิทธิ์การแจ้งเตือนคำแนะนำด้วย ส่วนนี้แสดง วิธีแสดงข้อความแจ้งการให้สิทธิ์ที่จำเป็น
ให้สิทธิ์บริการตำแหน่ง
Navigation SDK ใช้บริการตำแหน่งซึ่งต้องมีการให้สิทธิ์จากผู้ใช้ หากต้องการเปิดใช้บริการตำแหน่งและแสดงกล่องโต้ตอบการให้สิทธิ์ ให้ทำตามขั้นตอนต่อไปนี้
เปิดใช้ความสามารถด้านตำแหน่งและเบื้องหลังใน Xcode โดยทำดังนี้
- เปิดเป้าหมายใน Xcode แล้วเลือกแท็บการลงนามและความสามารถ โปรดดูรายละเอียดในคำแนะนำของ Apple เกี่ยวกับ การเพิ่มความสามารถให้กับแอป
- เพิ่มความสามารถของตำแหน่ง (ตลอดเวลา) และตำแหน่ง (เมื่อใช้งาน) ในช่องข้อความของแต่ละรายการ ให้เพิ่มคำอธิบายสั้นๆ เกี่ยวกับ เหตุผลที่แอปของคุณต้องใช้บริการตำแหน่ง (เช่น "แอปนี้ ต้องได้รับสิทธิ์ในการใช้บริการตำแหน่งสำหรับการนำทาง แบบเลี้ยวต่อเลี้ยว")
- เพิ่มความสามารถโหมดพื้นหลัง แล้วเลือกช่องทำเครื่องหมายการอัปเดต ตำแหน่ง หากไม่มีความสามารถนี้ แอปจะขัดข้องเมื่อเริ่มการนำทาง ดูรายละเอียดได้ที่คำแนะนำของ Apple เกี่ยวกับ การจัดการการอัปเดตตำแหน่งในเบื้องหลัง
หากต้องการแสดงกล่องโต้ตอบการให้สิทธิ์ ให้เรียกใช้เมธอด
requestAlwaysAuthorization()ของอินสแตนซ์ Location Manager
Swift
self.locationManager.requestAlwaysAuthorization()
Objective-C
[_locationManager requestAlwaysAuthorization];
ดูเอกสารฉบับเต็มของ Apple เกี่ยวกับการให้สิทธิ์บริการตำแหน่ง
ให้สิทธิ์การแจ้งเตือนสำหรับการนำทางเบื้องหลัง
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 ใน
การเรียกกลับ
ตัวอย่างต่อไปนี้แสดง
- การสร้างเส้นทางใหม่ที่มีปลายทางเดียว
- เริ่มคำแนะนำ
- การเปิดใช้การแจ้งเตือนคำแนะนำเบื้องหลัง
- จำลองการเดินทางตามเส้นทาง (ไม่บังคับ)
- ตั้งค่าโหมดกล้องเป็น "ติดตาม" (ไม่บังคับ)
Swift
func startNav() {
var destinations = [GMSNavigationWaypoint]()
destinations.append(GMSNavigationWaypoint.init(placeID: "ChIJnUYTpNASkFQR_gSty5kyoUk",
title: "PCC Natural Market")!)
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"]];
[_mapView.navigator setDestinations:destinations
callback:^(GMSRouteStatus routeStatus){
[_mapView.locationSimulator simulateLocationsAlongExistingRoute];
_mapView.navigator.guidanceActive = YES;
_mapView.cameraMode = GMSNavigationCameraModeFollowing;
}];
}
ดูข้อมูลเกี่ยวกับรหัสสถานที่ได้ที่รหัสสถานที่
สถานการณ์ที่มีหลายจุดแวะพัก
คุณกำหนดค่าจุดอ้างอิงได้สูงสุด 25 จุด
วิธี setDestinations ไม่รองรับการเดินทางแบบหลายป้าย ใช้
continueToNextDestinationWithCompletion()
เพื่อเลื่อนจุดแวะพักไปยังช่วงถัดไปของการเดินทาง
ตั้งค่าโหมดการเดินทาง
โหมดการเดินทางจะกำหนดทั้งประเภทเส้นทางที่จะดึงข้อมูลและวิธีกำหนดเส้นทางของผู้ใช้ คุณตั้งค่าโหมดการเดินทาง 1 ใน 4 โหมดสำหรับเส้นทางได้ ได้แก่ การขับรถ การปั่นจักรยาน การเดิน และแท็กซี่ ในโหมดการขับรถและแท็กซี่ เส้นทางของผู้ใช้จะอิงตามทิศทางการเดินทาง ส่วนในโหมดการปั่นจักรยานและการเดิน เส้นทางจะแสดงด้วยทิศทางที่อุปกรณ์หันไป (ไปทางด้านบนของอุปกรณ์ในโหมดแนวนอน)
ตั้งค่าพร็อพเพอร์ตี้
travelMode
ของมุมมองแผนที่ ดังที่แสดงในตัวอย่างต่อไปนี้
Swift
self.mapView.travelMode = .cycling
Objective-C
_mapView.travelMode = GMSNavigationTravelModeCycling;
ตั้งค่าถนนที่ต้องการหลีกเลี่ยง
ใช้พร็อพเพอร์ตี้ avoidsHighways และ avoidsTolls BOOL เพื่อหลีกเลี่ยง
ทางหลวง ถนนที่มีค่าผ่านทาง หรือทั้ง 2 อย่างตามเส้นทาง
Swift
self.mapView.navigator?.avoidsTolls = true
Objective-C
_mapView.navigator.avoidsTolls = YES;
เครื่องมือค้นหารหัสสถานที่
คุณใช้เครื่องมือค้นหารหัสสถานที่
เพื่อค้นหารหัสสถานที่ที่จะใช้เป็นจุดหมายปลายทางของเส้นทางได้ เพิ่มปลายทางจาก placeID ด้วย GMSNavigationWaypoint
ข้อความลอย
คุณเพิ่มข้อความลอยที่ใดก็ได้ในแอป ตราบใดที่ข้อความระบุแหล่งที่มาของ Google ไม่ถูกบดบัง Navigation SDK ไม่รองรับการยึดข้อความ กับพิกัดที่เฉพาะเจาะจง (ละติจูด ลองจิจูด) บนแผนที่หรือป้ายกำกับ ดูข้อมูลเพิ่มเติมได้ที่หน้าต่างข้อมูล