اتّبِع هذا الدليل لتخطيط مسار داخل تطبيقك إلى وجهة واحدة باستخدام حزمة تطوير البرامج للتنقّل على أجهزة iOS.
نظرة عامة
- أدمِج حزمة تطوير البرامج (SDK) لنظام التنقّل في تطبيقك على النحو الموضّح في قسم إعداد مشروعك.
- ضبط
GMSMapView
- اطلب من المستخدم قبول الأحكام والشروط وتفويض خدمات تحديد الموقع الجغرافي والإشعارات التي تعمل في الخلفية.
- أنشئ صفيفًا يحتوي على وجهة واحدة أو أكثر.
حدِّد
GMSNavigator
للتحكّم في التنقّل باتّجاهات مفصّلة.- أضِف وجهات باستخدام
setDestinations
. - اضبط
isGuidanceActive
علىtrue
لبدء التنقّل. - استخدِم
simulateLocationsAlongExistingRoute
لمحاكاة تقدّم المركبة على طول المسار، واختبار تطبيقك وتصحيح الأخطاء وعرض ميزاته.
- أضِف وجهات باستخدام
الاطّلاع على الرمز
إظهار/إخفاء رمز Swift الخاص بعنصر التحكّم في عرض التنقّل
/* * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import GoogleNavigation import UIKit class ViewController: UIViewController { var mapView: GMSMapView! var locationManager: CLLocationManager! override func loadView() { locationManager = CLLocationManager() // Set up the map view. let camera = GMSCameraPosition.camera(withLatitude: 47.67, longitude: -122.20, zoom: 14) mapView = GMSMapView.map(withFrame: .zero, camera: camera) // Show the terms and conditions. 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 denied authorization to display notifications. if !granted || error != nil { print("User rejected request to display notifications.") } } } else { // Handle rejection of terms and conditions. } } view = mapView makeButton() } // Create a route and start guidance. 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 guard routeStatus == .OK else { print("Handle route statuses that are not OK.") return } self.mapView.navigator?.isGuidanceActive = true self.mapView.locationSimulator?.simulateLocationsAlongExistingRoute() self.mapView.cameraMode = .following } } // Add a button to the view. func makeButton() { // A button to start navigation. let navButton = UIButton(frame: CGRect(x: 5, y: 150, width: 200, height: 35)) navButton.backgroundColor = .blue navButton.alpha = 0.5 navButton.setTitle("Start navigation", for: .normal) navButton.addTarget(self, action: #selector(startNav), for: .touchUpInside) self.mapView.addSubview(navButton) } }
إظهار/إخفاء رمز Objective-C الخاص بعنصر التحكّم في عرض التنقّل
/* * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #import "ViewController.h" @import GoogleNavigation; @interface ViewController () @end @implementation ViewController { GMSMapView *_mapView; CLLocationManager *_locationManager; } - (void)loadView { _locationManager = [[CLLocationManager alloc] init]; // Set up the map view. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.67 longitude:-122.20 zoom:14]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; // Show the terms and conditions. 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. } }]; self.view = _mapView; [self makeButton]; } // Create a route and initiate navigation. - (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.navigator.sendsBackgroundNotifications = YES; _mapView.cameraMode = GMSNavigationCameraModeFollowing; }]; } // Add a button to the view. - (void)makeButton { // A button to start navigation. UIButton *navButton = [UIButton buttonWithType:UIButtonTypeCustom]; [navButton addTarget:self action:@selector(startNav) forControlEvents:UIControlEventTouchUpInside]; [navButton setTitle:@"Navigate" forState:UIControlStateNormal]; [navButton setBackgroundColor:[UIColor blueColor]]; navButton.frame = CGRectMake(5.0, 150.0, 100.0, 35.0); [_mapView addSubview:navButton]; } @end
مطالبة المستخدم بالحصول على الأذونات اللازمة
قبل استخدام حزمة تطوير البرامج (SDK) لميزة التنقّل، على المستخدم الموافقة على الأحكام والشروط وتفويض استخدام خدمات الموقع الجغرافي، وهو أمر مطلوب للتنقّل. إذا كان تطبيقك سيتم تشغيله في الخلفية، يجب عليه أيضًا طلب تفويض المستخدم لإرسال إشعارات التنبيهات الإرشادية. يوضّح هذا القسم كيفية عرض طلبات التفويض المطلوبة.
تفويض "خدمات الموقع الجغرافي"
تستخدم حزمة تطوير البرامج (SDK) لميزة التنقّل خدمات الموقع الجغرافي، ما يتطلّب تفويض المستخدم. لتفعيل خدمات الموقع الجغرافي وعرض مربّع حوار التفويض، اتّبِع الخطوات التالية:
- أضِف مفتاح
NSLocationAlwaysUsageDescription
إلىInfo.plist
. بالنسبة إلى القيمة، أضِف شرحًا موجزًا لسبب احتياج تطبيقك إلى خدمات تحديد الموقع الجغرافي. على سبيل المثال: "يحتاج هذا التطبيق إلى إذن لاستخدام خدمات الموقع الجغرافي لتحديد المسارات بالتفصيل".
لعرض مربّع حوار التفويض، اتصل بـ
requestAlwaysAuthorization()
من مثيل "مدير الموقع الجغرافي".
self.locationManager.requestAlwaysAuthorization()
[_locationManager requestAlwaysAuthorization];
تفويض إشعارات التنبيهات للحصول على إرشادات في الخلفية
تحتاج حزمة تطوير البرامج (SDK) لنظام التنقّل إلى إذن المستخدم لتقديم تنبيهات عند تشغيل التطبيق في الخلفية. أضِف الرمز التالي لطلب الإذن من المستخدم لعرض هذه الإشعارات:
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.")
}
}
// 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");
}
}];
قبول الأحكام والشروط
استخدِم الرمز التالي لعرض مربّع حوار الأحكام والشروط وتفعيل التنقّل عندما يقبل المستخدم الأحكام. يُرجى العِلم أنّ هذا المثال يتضمّن رمز خدمات الموقع الجغرافي وإشعارات التنبيهات الإرشادية (المعروضة سابقًا).
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.
}
}
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 مع صفيف
يحتوي على وجهة واحدة أو أكثر (GMSNavigationWaypoint
) للزيارة. في حال تم احتساب المسار بنجاح، يتم عرضه على الخريطة. لبدء التوجيه على طول المسار،
بدءًا من الوجهة الأولى، اضبط isGuidanceActive
على true
في
الاستدعاء.
يوضّح المثال التالي ما يلي:
- إنشاء مسار جديد يتضمّن وجهتَين
- إرشادات حول البدء
- تفعيل إشعارات الإرشادات في الخلفية
- محاكاة التنقّل على طول المسار (اختياري)
- ضبط وضع الكاميرا على "تتبُّع" (اختياري)
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
}
}
- (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;
}];
}
للتعرّف على معرّفات الأماكن، يُرجى الاطّلاع على معرّفات الأماكن.
ضبط وضع السفر
يحدِّد وضع التنقّل نوع المسار الذي سيتم استرجاعه وطريقة تحديد مسار المستخدِم. يمكنك ضبط أحد أوضاع السفر الأربعة لمسار معيّن: القيادة وركوب الدراجات والمشي وسيارات الأجرة. في وضع القيادة ووضع التاكسي، يستند مسار المستخدم إلى اتجاه التنقّل. وفي وضع ركوب الدرّاجة ووضع المشي، يتم تمثيل المسار بالاتجاه الذي يواجهه الجهاز.
اضبط السمة travelMode
لعرض الخريطة، كما هو موضّح في المثال التالي:
self.mapView.travelMode = .cycling
_mapView.travelMode = GMSNavigationTravelModeCycling;
ضبط الطرق التي يجب تجنُّبها
استخدِم السمتَين avoidsHighways
وavoidsTolls
BOOL
لتجنُّب
الطرق السريعة و/أو الطرق التي تفرض رسومًا على طول مسار معيّن.
self.mapView.navigator?.avoidsTolls = true
_mapView.navigator.avoidsTolls = YES;
أداة البحث عن أرقام تعريف الأماكن
يمكنك استخدام أداة البحث عن معرّف المكان
للعثور على معرّفات الأماكن لاستخدامها في وجهات المسار. أضِف وجهة من placeID
باستخدام GMSNavigationWaypoint
.
النص العائم
يمكنك إضافة نص عائم في أي مكان في تطبيقك، شرط ألا يتم حجب مصدر Google. لا تتيح حزمة تطوير البرامج Navigation SDK تثبيت النص على خط عرض/خط طول على الخريطة أو على تصنيف. لمزيد من المعلومات، اطّلِع على نوافذ المعلومات.