नेविगेशन इवेंट के लिए सुनें
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
इस गाइड का इस्तेमाल करके, अपने ऐप्लिकेशन को अलग-अलग इवेंट सुनने और उनका जवाब देने की सुविधा चालू करें. ये इवेंट, उपयोगकर्ता के रास्ते के हिसाब से बदलते रहते हैं. इस गाइड में, किसी रास्ते को तय करने के बारे में नहीं बताया गया है. इसमें सिर्फ़ किसी रास्ते पर होने वाले इवेंट के बारे में बताया गया है.
खास जानकारी
iOS के लिए Navigation SDK टूल, आपको उपयोगकर्ता की जगह की जानकारी और रास्ते की स्थितियों से जुड़े लिसनर उपलब्ध कराता है. साथ ही, यह समय और दूरी से जुड़ा अहम डेटा भी उपलब्ध कराता है. मैप के व्यू कंट्रोलर पर, आपके ऐप्लिकेशन को इन लिसनर के लिए प्रोटोकॉल अपनाने होंगे:
GMSRoadSnappedLocationProviderListener
और
GMSNavigatorListener.
इस सूची में, नेविगेशन इवेंट के लिए उपलब्ध लिसनर के तरीके दिखाए गए हैं:
GMSNavigatorListener.didUpdateDelayCategory,
जब मार्गदर्शन चालू होता है, तब अगले डेस्टिनेशन पर पहुंचने में लगने वाले समय की कैटगरी में बदलाव होने पर इस फ़ंक्शन को कॉल किया जाता है.
GMSNavigatorListener.didChangeSuggestedLightingMode,
यह इवेंट तब ट्रिगर होता है, जब रोशनी की अनुमानित स्थितियों को अपडेट किया जाता है. उदाहरण के लिए, जब उपयोगकर्ता की मौजूदा जगह पर रात होती है, तब रोशनी बदल जाती है.
/**Copyright2020GoogleInc.Allrightsreserved.**LicensedundertheApacheLicense,Version2.0(the"License");*youmaynotusethisfileexceptincompliancewiththeLicense.*YoumayobtainacopyoftheLicenseat**http://www.apache.org/licenses/LICENSE-2.0**Unlessrequiredbyapplicablelaworagreedtoinwriting,software*distributedundertheLicenseisdistributedonan"AS IS"BASIS,*WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.*SeetheLicenseforthespecificlanguagegoverningpermissionsand*limitationsundertheLicense.*/importGoogleNavigationimportUIKitclassViewController:UIViewController,GMSNavigatorListener,GMSRoadSnappedLocationProviderListener{varmapView:GMSMapView!varlocationManager:CLLocationManager!overridefuncloadView(){locationManager=CLLocationManager()letcamera=GMSCameraPosition.camera(withLatitude:47.67,longitude:-122.20,zoom:14)mapView=GMSMapView.map(withFrame:CGRect.zero,camera:camera)//AddlistenersforGMSNavigatorandGMSRoadSnappedLocationProvider.mapView.navigator?.add(self)mapView.roadSnappedLocationProvider?.add(self)//Setthetimeupdatethreshold(seconds)anddistanceupdatethreshold(meters).mapView.navigator?.timeUpdateThreshold=10mapView.navigator?.distanceUpdateThreshold=100//Showthetermsandconditions.letcompanyName="Ride Sharing Co."GMSNavigationServices.showTermsAndConditionsDialogIfNeeded(withCompanyName:companyName){termsAcceptediniftermsAccepted{//Enablenavigationiftheuseracceptstheterms.self.mapView.isNavigationEnabled=true//Requestauthorizationtouselocationservices.self.locationManager.requestAlwaysAuthorization()//Requestauthorizationforalertnotificationswhichdeliverguidanceinstructions//inthebackground.UNUserNotificationCenter.current().requestAuthorization(options:[.alert]){granted,errorin//Handledeniedauthorizationtodisplaynotifications.if!granted||error!=nil{print("Authorization to deliver notifications was rejected.")}}}else{//Handlethecasewhentheuserrejectsthetermsandconditions.}}view=mapViewmakeButton()}//Createarouteandstartguidance.@objcfuncstartNav(){vardestinations=[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){routeStatusinguardrouteStatus==.OKelse{print("Handle route statuses that are not OK.")return}self.mapView.navigator?.isGuidanceActive=trueself.mapView.cameraMode=.followingself.mapView.locationSimulator?.simulateLocationsAlongExistingRoute()}mapView.roadSnappedLocationProvider?.startUpdatingLocation()}//Listenertohandlecontinuouslocationupdates.funclocationProvider(_locationProvider:GMSRoadSnappedLocationProvider,didUpdatelocation:CLLocation){print("Location: \(location.description)")}//Listenertohandlespeedingevents.funcnavigator(_navigator:GMSNavigator,didUpdateSpeedingPercentagepercentageAboveLimit:CGFloat){print("Speed is \(percentageAboveLimit) above the limit.")}//Listenertohandlearrivalevents.funcnavigator(_navigator:GMSNavigator,didArriveAtwaypoint:GMSNavigationWaypoint){print("You have arrived at: \(waypoint.title)")mapView.navigator?.continueToNextDestination()mapView.navigator?.isGuidanceActive=true}//Listenerforroutechangeevents.funcnavigatorDidChangeRoute(_navigator:GMSNavigator){print("The route has changed.")}//Listenerfortimetonextdestination.funcnavigator(_navigator:GMSNavigator,didUpdateRemainingTimetime:TimeInterval){print("Time to next destination: \(time)")}//Delegatefordistancetonextdestination.funcnavigator(_navigator:GMSNavigator,didUpdateRemainingDistancedistance:CLLocationDistance){letmiles=distance*0.00062137print("Distance to next destination: \(miles) miles.")}//Delegatefortrafficupdatestonextdestinationfuncnavigator(_navigator:GMSNavigator,didUpdatedelayCategory:GMSNavigationDelayCategory){print("Delay category to next destination: \(String(describing: delayCategory)).")}//Delegateforsuggestedlightingmodechanges.funcnavigator(_navigator:GMSNavigator,didChangeSuggestedLightingModelightingMode:GMSNavigationLightingMode){print("Suggested lighting mode has changed: \(String(describing: lightingMode))")//Changetothesuggestedlightingmode.mapView.lightingMode=lightingMode}//Addabuttontotheview.funcmakeButton(){//Startnavigation.letnavButton=UIButton(frame:CGRect(x:5,y:150,width:200,height:35))navButton.backgroundColor=.bluenavButton.alpha=0.5navButton.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"@importGoogleNavigation;@interfaceViewController()<GMSNavigatorListener,GMSRoadSnappedLocationProviderListener>
@end@implementationViewController{GMSMapView*_mapView;CLLocationManager*_locationManager;}-(void)loadView{_locationManager=[[CLLocationManageralloc]init];GMSCameraPosition*camera=[GMSCameraPositioncameraWithLatitude:47.67longitude:-122.20zoom:14];_mapView=[GMSMapViewmapWithFrame:CGRectZerocamera:camera];// Add listeners for GMSNavigator and GMSRoadSnappedLocationProvider.[_mapView.navigatoraddListener:self];[_mapView.roadSnappedLocationProvideraddListener: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.";[GMSNavigationServicesshowTermsAndConditionsDialogIfNeededWithCompanyName:companyNamecallback:^(BOOLtermsAccepted){if(termsAccepted){// Enable navigation if the user accepts the terms._mapView.navigationEnabled=YES;// Request authorization to use location services.[_locationManagerrequestAlwaysAuthorization];}else{// Handle the case when the user rejects the terms and conditions.}}];self.view=_mapView;[selfmakeButton];}// Create a route and initiate navigation.-(void)startNav{NSArray<GMSNavigationWaypoint*>*destinations=@[[[GMSNavigationWaypointalloc]initWithPlaceID:@"ChIJnUYTpNASkFQR_gSty5kyoUk"title:@"PCC Natural Market"],[[GMSNavigationWaypointalloc]initWithPlaceID:@"ChIJJ326ROcSkFQRBfUzOL2DSbo"title:@"Marina Park"]];[_mapView.navigatorsetDestinations:destinationscallback:^(GMSRouteStatusrouteStatus){_mapView.navigator.guidanceActive=YES;_mapView.navigator.sendsBackgroundNotifications=YES;_mapView.cameraMode=GMSNavigationCameraModeFollowing;[_mapView.locationSimulatorsimulateLocationsAlongExistingRoute];}];[_mapView.roadSnappedLocationProviderstartUpdatingLocation];}#pragma mark - GMSNavigatorListener// Listener for continuous location updates.-(void)locationProvider:(GMSRoadSnappedLocationProvider*)locationProviderdidUpdateLocation:(CLLocation*)location{NSLog(@"Location: %@",location.description);}// Listener to handle speeding events.-(void)navigator:(GMSNavigator*)navigatordidUpdateSpeedingPercentage:(CGFloat)percentageAboveLimit{NSLog(@"Speed is %f percent above the limit.",percentageAboveLimit);}// Listener to handle arrival events.-(void)navigator:(GMSNavigator*)navigatordidArriveAtWaypoint:(GMSNavigationWaypoint*)waypoint{NSLog(@"You have arrived at: %@",waypoint.title);[_mapView.navigatorcontinueToNextDestination];_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*)navigatordidUpdateRemainingTime:(NSTimeInterval)time{NSLog(@"Time to next destination: %f",time);}// Listener for distance to next destination.-(void)navigator:(GMSNavigator*)navigatordidUpdateRemainingDistance:(CLLocationDistance)distance{doublemiles=distance*0.00062137;NSLog(@"%@",[NSStringstringWithFormat:@"Distance to next destination: %.2f.",miles]);}// Listener for traffic updates for next destination-(void)navigator:(GMSNavigator*)navigatordidUpdateDelayCategory:(GMSNavigationDelayCategory)delayCategory{NSLog(@"Delay category to next destination: %ld.",delayCategory);}// Listener for suggested lighting mode changes.-(void)navigator:(GMSNavigator*)navigatordidChangeSuggestedLightingMode:(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=[UIButtonbuttonWithType:UIButtonTypeCustom];[navButtonaddTarget:selfaction:@selector(startNav)forControlEvents:UIControlEventTouchUpInside];[navButtonsetTitle:@"Navigate"forState:UIControlStateNormal];[navButtonsetBackgroundColor:[UIColorblueColor]];[navButtonsetAlpha:0.5];navButton.frame=CGRectMake(5.0,150.0,100.0,35.0);[_mapViewaddSubview:navButton];}@end
ज़रूरी प्रोटोकॉल के मुताबिक काम करने का एलान करना
नेविगेशन के तरीकों को लागू करने से पहले, व्यू कंट्रोलर को इन प्रोटोकॉल को अपनाना होगा:
मैप पर उपयोगकर्ता की प्रोग्रेस दिखाने के लिए, जगह की जानकारी के अपडेट ज़रूरी हैं.
location इंस्टेंस, इन प्रॉपर्टी को दिखाता है:
जगह की प्रॉपर्टी
ब्यौरा
altitude
मौजूदा ऊंचाई.
coordinate.latitude
सड़क के हिसाब से अक्षांश का मौजूदा निर्देशांक.
coordinate.longitude
सड़क के हिसाब से तय किया गया मौजूदा देशांतर निर्देशांक.
कोर्स
डिग्री में मौजूदा बियरिंग.
स्पीड
मौजूदा स्पीड.
timestamp
मौजूदा रीडिंग की तारीख/समय.
जगह की जानकारी के अपडेट लगातार पाने के लिए, mapView.roadSnappedLocationProvider.startUpdatingLocation को कॉल करें. साथ ही, didUpdateLocation इवेंट को हैंडल करने के लिए, GMSRoadSnappedLocationProviderListener का इस्तेमाल करें.
यहां दिए गए उदाहरण में, startUpdatingLocation को कॉल करने का तरीका बताया गया है:
आपका ऐप्लिकेशन, didArriveAtWaypoint इवेंट का इस्तेमाल करके यह पता लगाता है कि कोई डेस्टिनेशन कब पहुंचा गया. continueToNextDestination() को कॉल करके, निर्देशों को फिर से शुरू किया जा सकता है और अगले वेपॉइंट पर जाया जा सकता है. इसके बाद, निर्देशों को फिर से चालू करें. आपके ऐप्लिकेशन को continueToNextDestination() पर कॉल करने के बाद, दिशा-निर्देशों को फिर से चालू करना होगा.
ऐप्लिकेशन के continueToNextDestination को कॉल करने के बाद, नेविगेटर के पास पिछली मंज़िल का डेटा नहीं रहता. अगर आपको किसी रूट लेग के बारे में जानकारी का विश्लेषण करना है, तो आपको continueToNextDestination() को कॉल करने से पहले, नेविगेटर से यह जानकारी वापस लानी होगी.
यहां दिए गए कोड के उदाहरण में, didArriveAtWaypoint इवेंट को मैनेज करने का तरीका बताया गया है:
Swift
funcnavigator(_navigator:GMSNavigator,didArriveAtwaypoint:GMSNavigationWaypoint){print("You have arrived at: \(waypoint.title)")mapView.navigator?.continueToNextDestination()mapView.navigator?.isGuidanceActive=true}
रास्ता बदलने पर सूचना पाने के लिए, navigatorDidChangeRoute इवेंट को मैनेज करने का तरीका बनाएं. GMSNavigator की routeLegs और currentRouteLeg प्रॉपर्टी का इस्तेमाल करके, नए रूट को ऐक्सेस किया जा सकता है.
मंज़िल तक पहुंचने में लगने वाले समय के अपडेट लगातार पाने के लिए, didUpdateRemainingTime इवेंट को हैंडल करने का तरीका बनाएं. time पैरामीटर से, अगले डेस्टिनेशन तक पहुंचने का अनुमानित समय मिलता है. यह समय सेकंड में होता है.
Swift
funcnavigator(_navigator:GMSNavigator,didUpdateRemainingTimetime:TimeInterval){print("Time to next destination: \(time)")}
Objective-C
-(void)navigator:(GMSNavigator*)navigatordidUpdateRemainingTime:(NSTimeInterval)time{NSLog(@"Time to nextdestination:%f", time); }
अगले डेस्टिनेशन पर पहुंचने के अनुमानित समय में कम से कम बदलाव सेट करने के लिए, GMSNavigator पर timeUpdateThreshold प्रॉपर्टी सेट करें. इसकी वैल्यू सेकंड में दी जाती है. अगर यह प्रॉपर्टी सेट नहीं की जाती है, तो सेवाएं एक सेकंड की डिफ़ॉल्ट वैल्यू का इस्तेमाल करती हैं.
Swift
navigator?.timeUpdateThreshold=10
Objective-C
navigator.timeUpdateThreshold=10;
मंज़िल की दूरी के अपडेट पाना
मंज़िल की दूरी के अपडेट लगातार पाने के लिए, didUpdateRemainingDistance इवेंट को हैंडल करने का एक तरीका बनाएं. distance पैरामीटर, अगली मंज़िल की अनुमानित दूरी को मीटर में दिखाता है.
Swift
funcnavigator(_navigator:GMSNavigator,didUpdateRemainingDistancedistance:CLLocationDistance){letmiles=distance*0.00062137print("Distance to nextdestination: \(miles) miles.")}
अगली मंज़िल की अनुमानित दूरी में कम से कम बदलाव सेट करने के लिए, GMSNavigator पर distanceUpdateThreshold प्रॉपर्टी सेट करें. वैल्यू मीटर में दी जाती है. अगर यह प्रॉपर्टी सेट नहीं की जाती है, तो सेवाएं एक मीटर की डिफ़ॉल्ट वैल्यू का इस्तेमाल करती हैं.
Swift
navigator?.distanceUpdateThreshold=100
Objective-C
navigator.distanceUpdateThreshold=100;
ट्रैफ़िक के अपडेट पाना
बाकी रास्ते के लिए ट्रैफ़िक की जानकारी के अपडेट पाने के लिए, didUpdateDelayCategory इवेंट को हैंडल करने का तरीका बनाएं. delayCategoryToNextDestination को कॉल करने पर GMSNavigationDelayCategory मिलता है. यह 0 से 3 तक की वैल्यू देता है. कैटगरी में अपडेट, ऐप्लिकेशन इस्तेमाल करने वाले व्यक्ति की मौजूदा जगह के हिसाब से किए जाते हैं. अगर ट्रैफ़िक डेटा उपलब्ध नहीं है, तो GMSNavigationDelayCategory 0 दिखाता है. संख्याएं 1 से 3 तक, हल्के से भारी फ़्लो को दिखाती हैं.
Swift
funcnavigator(_navigator:GMSNavigator,didUpdatedelayCategory:GMSNavigationDelayCategory){print("Traffic flow to next destination:\(delayCategory)")}
Objective-C
-(void)navigator:(GMSNavigator*)navigatordidUpdateDelayCategory:(GMSNavigationDelayCategory)delayCategory{NSLog(@"Traffic flow to next destination: %ld",(long)delayCategory);}
GMSNavigationDelayCategory प्रॉपर्टी, देरी के इन लेवल को दिखाती है:
देरी की कैटगरी
ब्यौरा
GMSNavigationDelayCategoryNoData
0 - Unavailable, no data for traffic or :
रास्ते की जानकारी.
GMSNavigationDelayCategoryHeavy
1 - हैवी.
GMSNavigationDelayCategoryMedium
2 - मीडियम.
GMSNavigationDelayCategoryLight
3 - लाइट.
तेज़ गति से गाड़ी चलाने के बारे में अपडेट पाना
जब कोई ड्राइवर तय की गई स्पीड लिमिट से ज़्यादा स्पीड पर गाड़ी चला रहा हो, तब अपडेट पाने के लिए, didUpdateSpeedingPercentage इवेंट को मैनेज करने का एक तरीका बनाएं.
Swift
// Listener to handle speeding events. func navigator( _ navigator:GMSNavigator,didUpdateSpeedingPercentagepercentageAboveLimit:CGFloat){print("Speed is \(percentageAboveLimit) above the limit.")}
Objective-C
// Listener to handle speeding events. - (void)navigator:(GMSNavigator*)navigatordidUpdateSpeedingPercentage:(CGFloat)percentageAboveLimit{NSLog(@"Speed is %f percent above the limit.",percentageAboveLimit);}
सुझाए गए लाइटिंग मोड को बदलना
रोशनी में होने वाले अनुमानित बदलावों के बारे में अपडेट पाने के लिए, didChangeSuggestedLightingMode इवेंट को हैंडल करने का तरीका बनाएं.
Swift
// Define a listener for suggested changes to lighting mode. func navigator(_navigator:GMSNavigator,didChangeSuggestedLightingModelightingMode:GMSNavigationLightingMode){print("Suggested lighting mode has changed:\(String(describing:lightingMode))")// Make the suggested change. mapView.lightingMode = lightingMode }
Objective-C
// Define a listener for suggested changes to lighting mode.-(void)navigator:(GMSNavigator*)navigatordidChangeSuggestedLightingMode:(GMSNavigationLightingMode)lightingMode{NSLog(@"Suggested lighting mode haschanged:%ld", (long)lightingMode);// Make the suggested change. _mapView.lightingMode = lightingMode; }