ทำตามคำแนะนำนี้เพื่อวางแผนเส้นทางภายในแอปไปยังจุดหมายเดียว โดยใช้ Navigation SDK สำหรับ iOS
ภาพรวม
- ผสานรวม Navigation SDK เข้ากับแอปตามที่อธิบายไว้ ในส่วนตั้งค่าโปรเจ็กต์
- กำหนดค่า
GMSMapView
- แจ้งให้ผู้ใช้ยอมรับข้อกำหนดและเงื่อนไข รวมถึงให้สิทธิ์บริการตำแหน่งและการแจ้งเตือนในเบื้องหลัง
- สร้างอาร์เรย์ที่มีปลายทางอย่างน้อย 1 แห่ง
กำหนด
GMSNavigator
เพื่อควบคุมการนำทางแบบเลี้ยวต่อเลี้ยว- เพิ่มปลายทางโดยใช้
setDestinations
- ตั้งค่า
isGuidanceActive
เป็นtrue
เพื่อเริ่มการนำทาง - ใช้
simulateLocationsAlongExistingRoute
เพื่อจำลองความคืบหน้าของยานพาหนะตามเส้นทางสำหรับการทดสอบ การแก้ไขข้อบกพร่อง และการสาธิตแอป
- เพิ่มปลายทางโดยใช้
ดูรหัส
แจ้งให้ผู้ใช้ให้สิทธิ์ที่จำเป็น
ก่อนใช้ Navigation SDK ผู้ใช้ต้องยอมรับ ข้อกำหนดและเงื่อนไข และให้สิทธิ์การใช้บริการตำแหน่ง ซึ่งเป็นสิ่ง ที่จำเป็นสำหรับการนำทาง หากแอปจะทำงานในเบื้องหลัง แอปจะต้อง แจ้งให้ผู้ใช้ให้สิทธิ์การแจ้งเตือนคำแนะนำด้วย ส่วนนี้แสดง วิธีแสดงข้อความแจ้งการให้สิทธิ์ที่จำเป็น
ให้สิทธิ์บริการตำแหน่ง
Navigation SDK ใช้บริการตำแหน่งซึ่งต้องมีการให้สิทธิ์จากผู้ใช้ หากต้องการเปิดใช้บริการตำแหน่งและแสดงกล่องโต้ตอบการให้สิทธิ์ ให้ทำตามขั้นตอนต่อไปนี้
- เพิ่มคีย์
NSLocationAlwaysUsageDescription
ลงในInfo.plist
สำหรับค่า ให้เพิ่มคำอธิบายสั้นๆ ว่าเหตุใดแอปจึงต้องใช้บริการตำแหน่ง เช่น "แอปนี้ต้องมีสิทธิ์ใช้บริการตำแหน่งสำหรับ การนำทางแบบเลี้ยวต่อเลี้ยว"
หากต้องการแสดงกล่องโต้ตอบการให้สิทธิ์ ให้เรียกใช้
requestAlwaysAuthorization()
ของ อินสแตนซ์ Location Manager
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
ใน
การเรียกกลับ
ตัวอย่างต่อไปนี้แสดง
- การสร้างเส้นทางใหม่ที่มี 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;
เครื่องมือค้นหารหัสสถานที่
คุณใช้เครื่องมือค้นหารหัสสถานที่
เพื่อค้นหารหัสสถานที่ที่จะใช้เป็นจุดหมายปลายทางของเส้นทางได้ เพิ่มปลายทางจาก placeID
ด้วย GMSNavigationWaypoint
ข้อความลอย
คุณเพิ่มข้อความลอยได้ทุกที่ในแอป ตราบใดที่การระบุแหล่งที่มาของ Google ไม่ถูกบดบัง Navigation SDK ไม่รองรับการยึดข้อความกับละติจูด/ลองจิจูดบนแผนที่หรือกับป้ายกำกับ ดูข้อมูลเพิ่มเติมได้ที่ หน้าต่างข้อมูล