इस गाइड का इस्तेमाल करके, अपने ऐप्लिकेशन को अलग-अलग इवेंट सुनने और उनका जवाब देने की सुविधा दें. ये इवेंट, किसी रास्ते पर नेविगेट करने के दौरान बदलते रहते हैं. इस गाइड में, रास्ते को तय करने के बारे में नहीं बताया गया है. इसमें सिर्फ़ रास्ते पर होने वाले इवेंट के जवाब देने के बारे में बताया गया है.
खास जानकारी
iOS के लिए नेविगेशन SDK टूल, आपको उपयोगकर्ता की जगह की जानकारी और रास्ते की स्थितियों के साथ-साथ, समय और दूरी से जुड़ा अहम डेटा देता है. मैप के व्यू कंट्रोलर पर, आपके ऐप्लिकेशन को इन लिसनर के लिए प्रोटोकॉल अपनाने होंगे:
GMSRoadSnappedLocationProviderListener
और
GMSNavigatorListener
.
इस सूची में, नेविगेशन इवेंट के लिए उपलब्ध लिसनर के तरीके दिखाए गए हैं:
GMSNavigatorListener.didArriveAtWaypoint
, किसी डेस्टिनेशन पर पहुंचने पर ट्रिगर होता है.GMSNavigatorListener.navigatorDidChangeRoute
, रास्ता बदलने पर ट्रिगर होता है.GMSNavigatorListener.didUpdateRemainingTime
, मार्गदर्शन की सुविधा चालू होने पर, अगले डेस्टिनेशन तक पहुंचने में लगने वाला समय बदलने पर, बार-बार कॉल किया जाता है.GMSNavigatorListener.didUpdateRemainingDistance
, मार्गदर्शन चालू होने पर, अगली मंज़िल की दूरी बदलने पर बार-बार कॉल किया जाता है.GMSNavigatorListener.didUpdateDelayCategory
, मार्गदर्शन चालू होने पर, अगले डेस्टिनेशन तक पहुंचने में लगने वाले समय की कैटगरी में बदलाव होने पर कॉल किया जाता है.GMSNavigatorListener.didChangeSuggestedLightingMode
, रोशनी की अनुमानित स्थितियों के अपडेट होने पर ट्रिगर होता है. उदाहरण के लिए, जब उपयोगकर्ता की मौजूदा जगह पर रात होती है, तो रोशनी बदल जाती है.GMSNavigatorListener.didUpdateSpeedingPercentage
, यह ट्रिगर तब होता है, जब ड्राइवर की रफ़्तार, तय सीमा से ज़्यादा हो.GMSRoadSnappedLocationProviderListener.didUpdateLocation
, उपयोगकर्ता की जगह बदलने पर बार-बार कॉल किया जाता है.
कोड देखना
किसी इवेंट लिसनर के लिए, Swift कोड दिखाएं/छिपाएं.
/* * Copyright 2020 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, GMSNavigatorListener, GMSRoadSnappedLocationProviderListener { var mapView: GMSMapView! var locationManager: CLLocationManager! override func loadView() { locationManager = CLLocationManager() let camera = GMSCameraPosition.camera(withLatitude: 47.67, longitude: -122.20, zoom: 14) mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) // Add listeners for GMSNavigator and GMSRoadSnappedLocationProvider. mapView.navigator?.add(self) mapView.roadSnappedLocationProvider?.add(self) // Set the time update threshold (seconds) and distance update threshold (meters). mapView.navigator?.timeUpdateThreshold = 10 mapView.navigator?.distanceUpdateThreshold = 100 // 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 // 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("Authorization to deliver notifications was rejected.") } } } else { // Handle the case when the user rejects the terms and conditions. } } view = mapView makeButton() } // Create a route and start guidance. @objc 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.cameraMode = .following self.mapView.locationSimulator?.simulateLocationsAlongExistingRoute() } mapView.roadSnappedLocationProvider?.startUpdatingLocation() } // Listener to handle continuous location updates. func locationProvider( _ locationProvider: GMSRoadSnappedLocationProvider, didUpdate location: CLLocation ) { print("Location: \(location.description)") } // Listener to handle speeding events. func navigator( _ navigator: GMSNavigator, didUpdateSpeedingPercentage percentageAboveLimit: CGFloat ) { print("Speed is \(percentageAboveLimit) above the limit.") } // Listener to handle arrival events. func navigator(_ navigator: GMSNavigator, didArriveAt waypoint: GMSNavigationWaypoint) { print("You have arrived at: \(waypoint.title)") mapView.navigator?.continueToNextDestination() mapView.navigator?.isGuidanceActive = true } // Listener for route change events. func navigatorDidChangeRoute(_ navigator: GMSNavigator) { print("The route has changed.") } // Listener for time to next destination. func navigator(_ navigator: GMSNavigator, didUpdateRemainingTime time: TimeInterval) { print("Time to next destination: \(time)") } // Delegate for distance to next destination. func navigator( _ navigator: GMSNavigator, didUpdateRemainingDistance distance: CLLocationDistance ) { let miles = distance * 0.00062137 print("Distance to next destination: \(miles) miles.") } // Delegate for traffic updates to next destination func navigator( _ navigator: GMSNavigator, didUpdate delayCategory: GMSNavigationDelayCategory ) { print("Delay category to next destination: \(String(describing: delayCategory)).") } // Delegate for suggested lighting mode changes. func navigator( _ navigator: GMSNavigator, didChangeSuggestedLightingMode lightingMode: GMSNavigationLightingMode ) { print("Suggested lighting mode has changed: \(String(describing: lightingMode))") // Change to the suggested lighting mode. mapView.lightingMode = lightingMode } // Add a button to the view. func makeButton() { // 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 2020 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 () <GMSNavigatorListener, GMSRoadSnappedLocationProviderListener> @end @implementation ViewController { GMSMapView *_mapView; CLLocationManager *_locationManager; } - (void)loadView { _locationManager = [[CLLocationManager alloc] init]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.67 longitude:-122.20 zoom:14]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; // Add listeners for GMSNavigator and GMSRoadSnappedLocationProvider. [_mapView.navigator addListener:self]; [_mapView.roadSnappedLocationProvider addListener:self]; // Set the time update threshold (seconds) and distance update threshold (meters). _mapView.navigator.timeUpdateThreshold = 10; _mapView.navigator.distanceUpdateThreshold = 100; // 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; // Request authorization to use location services. [_locationManager requestAlwaysAuthorization]; } else { // Handle the case when the user rejects 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.navigator.guidanceActive = YES; _mapView.navigator.sendsBackgroundNotifications = YES; _mapView.cameraMode = GMSNavigationCameraModeFollowing; [_mapView.locationSimulator simulateLocationsAlongExistingRoute]; }]; [_mapView.roadSnappedLocationProvider startUpdatingLocation]; } #pragma mark - GMSNavigatorListener // Listener for continuous location updates. - (void)locationProvider:(GMSRoadSnappedLocationProvider *)locationProvider didUpdateLocation:(CLLocation *)location { NSLog(@"Location: %@", location.description); } // Listener to handle speeding events. - (void)navigator:(GMSNavigator *)navigator didUpdateSpeedingPercentage:(CGFloat)percentageAboveLimit { NSLog(@"Speed is %f percent above the limit.", percentageAboveLimit); } // Listener to handle arrival events. - (void)navigator:(GMSNavigator *)navigator didArriveAtWaypoint:(GMSNavigationWaypoint *)waypoint { NSLog(@"You have arrived at: %@", waypoint.title); [_mapView.navigator continueToNextDestination]; _mapView.navigator.guidanceActive = YES; } // Listener for route change events. - (void)navigatorDidChangeRoute:(GMSNavigator *)navigator { NSLog(@"The route has changed."); } // Listener for time to next destination. - (void)navigator:(GMSNavigator *)navigator didUpdateRemainingTime:(NSTimeInterval)time { NSLog(@"Time to next destination: %f", time); } // Listener for distance to next destination. - (void)navigator:(GMSNavigator *)navigator didUpdateRemainingDistance:(CLLocationDistance)distance { double miles = distance * 0.00062137; NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]); } // Listener for traffic updates for next destination - (void)navigator:(GMSNavigator *)navigator didUpdateDelayCategory:(GMSNavigationDelayCategory)delayCategory { NSLog(@"Delay category to next destination: %ld.", delayCategory); } // Listener for suggested lighting mode changes. -(void)navigator:(GMSNavigator *)navigator didChangeSuggestedLightingMode:(GMSNavigationLightingMode)lightingMode { NSLog(@"Suggested lighting mode has changed: %ld", (long)lightingMode); // Change to the suggested lighting mode. _mapView.lightingMode = lightingMode; } #pragma mark - Programmatic UI elements // Add a button to the view. - (void)makeButton { // 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 setAlpha:0.5]; navButton.frame = CGRectMake(5.0, 150.0, 100.0, 35.0); [_mapView addSubview:navButton]; } @end
ज़रूरी प्रोटोकॉल का पालन करने का एलान करना
नेविगेशन के तरीकों को लागू करने से पहले, व्यू कंट्रोलर को ये प्रोटोकॉल अपनाने होंगे:
class ViewController: UIViewController, GMSNavigatorListener,
GMSRoadSnappedLocationProviderListener {
@interface ViewController () <GMSNavigatorListener,
GMSRoadSnappedLocationProviderListener>
@end
नेविगेशन प्रोटोकॉल अपनाने के बाद, व्यू कंट्रोलर पर लिसनर सेट करें. उदाहरण के लिए, viewDidLoad()
method में यह कोड जोड़ा जा सकता है.
mapView.navigator?.add(self) mapView.roadSnappedLocationProvider?.add(self)
[_mapView.navigator addListener:self]; [_mapView.roadSnappedLocationProvider
addListener:self];
जगह की जानकारी के अपडेट पाना या उन्हें रोकना
मैप पर उपयोगकर्ता की प्रोग्रेस दिखाने के लिए, जगह की जानकारी अपडेट करना ज़रूरी है.
location
इंस्टेंस, ये प्रॉपर्टी दिखाता है:
लोकेशन प्रॉपर्टी | ब्यौरा |
---|---|
ऊंचाई | मौजूदा ऊंचाई. |
coordinate.latitude | सड़क के हिसाब से तय किया गया मौजूदा अक्षांश निर्देशांक. |
coordinate.longitude | सड़क के हिसाब से तय किया गया मौजूदा देशांतर निर्देशांक. |
कोर्स | डिग्री में मौजूदा बियरिंग. |
गति | मौजूदा स्पीड. |
timestamp | मौजूदा रीडिंग की तारीख/समय. |
जगह की जानकारी के लगातार अपडेट पाने के लिए, mapView.roadSnappedLocationProvider.startUpdatingLocation
को कॉल करें. साथ ही, didUpdateLocation
इवेंट को मैनेज करने के लिए, GMSRoadSnappedLocationProviderListener
का इस्तेमाल करें.
यहां दिए गए उदाहरण में, startUpdatingLocation
को कॉल करने का तरीका बताया गया है:
mapView.roadSnappedLocationProvider.startUpdatingLocation()
[_mapView.roadSnappedLocationProvider startUpdatingLocation];
नीचे दिया गया कोड, GMSRoadSnappedLocationProviderListener
बनाता है, जो didUpdateLocation
इवेंट को मैनेज करता है.
func locationProvider(_ locationProvider: GMSRoadSnappedLocationProvider,
didUpdate location: CLLocation) { print("Location: \(location.description)") }
- (void)locationProvider:(GMSRoadSnappedLocationProvider *)locationProvider
didUpdateLocation:(CLLocation *)location { NSLog(@"Location: %@",
location.description); }
ऐप्लिकेशन के बैकग्राउंड में होने पर, जगह की जानकारी के अपडेट पाने के लिए, allowsBackgroundLocationUpdates
को 'सही है' पर सेट करें:
mapView.roadSnappedLocationProvider.allowsBackgroundLocationUpdates = true
_mapView.roadSnappedLocationProvider.allowsBackgroundLocationUpdates = YES;
आने के समय का पता लगाना
आपका ऐप्लिकेशन, didArriveAtWaypoint
इवेंट का इस्तेमाल करके यह पता लगाता है कि किसी डेस्टिनेशन पर पहुंचा गया है या नहीं. continueToNextDestination()
को दबाकर, अगले वेपॉइंट पर जाने के लिए, निर्देशों को फिर से शुरू किया जा सकता है. इसके बाद, निर्देशों की सुविधा को फिर से चालू करें. आपके ऐप्लिकेशन को continueToNextDestination()
को कॉल करने के बाद, निर्देशों की सुविधा फिर से चालू करनी होगी.
ऐप्लिकेशन के continueToNextDestination
को कॉल करने के बाद, नेविगेटर के पास पिछले डेस्टिनेशन का डेटा नहीं होता. अगर आपको किसी रास्ते के हिस्से की जानकारी का विश्लेषण करना है, तो आपको continueToNextDestination()
को कॉल करने से पहले, नेविगेटर से यह जानकारी हासिल करनी होगी.
यहां दिए गए कोड के उदाहरण में, didArriveAtWaypoint
इवेंट को मैनेज करने का तरीका बताया गया है:
func navigator(_ navigator: GMSNavigator, didArriveAt waypoint:
GMSNavigationWaypoint) { print("You have arrived at: \(waypoint.title)")
mapView.navigator?.continueToNextDestination()
mapView.navigator?.isGuidanceActive = true }
- (void)navigator:(GMSNavigator *)navigator
didArriveAtWaypoint:(GMSNavigationWaypoint *)waypoint { NSLog(@"You have
arrived at: %@", waypoint.title); [_mapView.navigator
continueToNextDestination]; _mapView.navigator.guidanceActive = YES; }
रास्ते में हुए बदलावों के अपडेट पाना
रास्ता बदलने पर सूचना पाने के लिए, navigatorDidChangeRoute
इवेंट को मैनेज करने का तरीका बनाएं. GMSNavigator
की routeLegs
और currentRouteLeg
प्रॉपर्टी का इस्तेमाल करके, नया रूट ऐक्सेस किया जा सकता है.
func navigatorDidChangeRoute(_ navigator: GMSNavigator) { print("The route has
changed.") }
- (void)navigatorDidChangeRoute:(GMSNavigator *)navigator { NSLog(@"The route
has changed."); }
डेस्टिनेशन तक पहुंचने में लगने वाले समय के अपडेट पाना
डेस्टिनेशन तक पहुंचने में लगने वाले समय के बारे में लगातार अपडेट पाने के लिए, didUpdateRemainingTime
इवेंट को हैंडल करने का तरीका बनाएं. time
पैरामीटर, अगले डेस्टिनेशन तक पहुंचने में लगने वाले समय का अनुमानित समय, सेकंड में बताता है.
func navigator(_ navigator: GMSNavigator, didUpdateRemainingTime time:
TimeInterval) { print("Time to next destination: \(time)") }
- (void)navigator:(GMSNavigator *)navigator
didUpdateRemainingTime:(NSTimeInterval)time { NSLog(@"Time to next
destination: %f", time); }
अगले डेस्टिनेशन तक पहुंचने में लगने वाले अनुमानित समय में कम से कम बदलाव करने के लिए, timeUpdateThreshold
प्रॉपर्टी को GMSNavigator
पर सेट करें. इसकी वैल्यू सेकंड में दी जाती है. अगर यह प्रॉपर्टी सेट नहीं है, तो सेवाएं एक सेकंड की डिफ़ॉल्ट वैल्यू का इस्तेमाल करती हैं.
navigator?.timeUpdateThreshold = 10
navigator.timeUpdateThreshold = 10;
मंज़िल की दूरी के अपडेट पाना
डेस्टिनेशन की दूरी के बारे में लगातार अपडेट पाने के लिए, didUpdateRemainingDistance
इवेंट को हैंडल करने का तरीका बनाएं. distance
पैरामीटर, अगले डेस्टिनेशन तक की अनुमानित दूरी मीटर में दिखाता है.
func navigator(_ navigator: GMSNavigator, didUpdateRemainingDistance distance:
CLLocationDistance) { let miles = distance * 0.00062137 print("Distance to next
destination: \(miles) miles.") }
- (void)navigator:(GMSNavigator *)navigator
didUpdateRemainingDistance:(CLLocationDistance)distance { double miles =
distance * 0.00062137; NSLog(@"%@", [NSString stringWithFormat:@"Distance to
next destination: %.2f.", miles]); }
अगले डेस्टिनेशन की अनुमानित दूरी में कम से कम बदलाव सेट करने के लिए, distanceUpdateThreshold
प्रॉपर्टी को GMSNavigator
पर सेट करें. वैल्यू, मीटर में दी जाती है. अगर यह प्रॉपर्टी सेट नहीं है, तो सेवाएं एक मीटर की डिफ़ॉल्ट वैल्यू का इस्तेमाल करती हैं.
navigator?.distanceUpdateThreshold = 100
navigator.distanceUpdateThreshold = 100;
ट्रैफ़िक से जुड़े अपडेट पाना
बाकी रास्ते के लिए ट्रैफ़िक फ़्लो के लगातार अपडेट पाने के लिए,
didUpdateDelayCategory
इवेंट को हैंडल करने का तरीका बनाएं. delayCategoryToNextDestination
को कॉल करने पर GMSNavigationDelayCategory
दिखता है, जो 0 से 3 तक की वैल्यू दिखाता है. कैटगरी में होने वाले अपडेट, ऐप्लिकेशन के उपयोगकर्ता की मौजूदा स्थिति के आधार पर होते हैं. अगर ट्रैफ़िक डेटा उपलब्ध नहीं है, तो GMSNavigationDelayCategory
0 दिखाता है. 1 से 3 की संख्याएं, हल्के से भारी ट्रैफ़िक में बढ़ोतरी को दिखाती हैं.
func navigator(_ navigator: GMSNavigator, didUpdate delayCategory:
GMSNavigationDelayCategory) { print("Traffic flow to next destination:
\(delayCategory)") }
- (void)navigator:(GMSNavigator *)navigator
didUpdateDelayCategory:(GMSNavigationDelayCategory)delayCategory {
NSLog(@"Traffic flow to next destination: %ld", (long)delayCategory); }
GMSNavigationDelayCategory
प्रॉपर्टी में, देरी के ये लेवल दिखते हैं:
देरी की कैटगरी | ब्यौरा |
---|---|
GMSNavigationDelayCategoryNoData | 0 - उपलब्ध नहीं है, ट्रैफ़िक का कोई डेटा नहीं है या : |
रास्ते की जानकारी. | |
GMSNavigationDelayCategoryHeavy | 1 - ज़्यादा. |
GMSNavigationDelayCategoryMedium | 2 - मीडियम. |
GMSNavigationDelayCategoryLight | 3 - हल्का. |
तेज़ी से चलने की जानकारी पाने की सुविधा
जब ड्राइवर स्पीड की सीमा से ज़्यादा तेज़ी से गाड़ी चला रहा हो, तब अपडेट पाने के लिए, didUpdateSpeedingPercentage
इवेंट को मैनेज करने का तरीका बनाएं.
// Listener to handle speeding events. func navigator( _ navigator:
GMSNavigator, didUpdateSpeedingPercentage percentageAboveLimit: CGFloat ) {
print("Speed is \(percentageAboveLimit) above the limit.") }
// Listener to handle speeding events. - (void)navigator:(GMSNavigator
*)navigator didUpdateSpeedingPercentage:(CGFloat)percentageAboveLimit {
NSLog(@"Speed is %f percent above the limit.", percentageAboveLimit); }
लाइटिंग के सुझाए गए मोड में बदलाव करना
रोशनी में होने वाले अनुमानित बदलावों के अपडेट पाने के लिए, didChangeSuggestedLightingMode
इवेंट को मैनेज करने का तरीका बनाएं.
// Define a listener for suggested changes to lighting mode. func navigator(_
navigator: GMSNavigator, didChangeSuggestedLightingMode lightingMode:
GMSNavigationLightingMode) { print("Suggested lighting mode has changed:
\(String(describing: lightingMode))")
// Make the suggested change. mapView.lightingMode = lightingMode }
// Define a listener for suggested changes to lighting mode.
-(void)navigator:(GMSNavigator *)navigator didChangeSuggestedLightingMode:
(GMSNavigationLightingMode)lightingMode { NSLog(@"Suggested lighting mode has
changed: %ld", (long)lightingMode);
// Make the suggested change. _mapView.lightingMode = lightingMode; }