किसी रास्ते पर नेविगेट करें

iOS के लिए नेविगेशन SDK टूल का इस्तेमाल करके, अपने ऐप्लिकेशन में किसी एक डेस्टिनेशन के लिए रास्ता प्लॉट करने के लिए, इस गाइड का पालन करें.

खास जानकारी

  1. अपना प्रोजेक्ट सेट अप करें सेक्शन में बताए गए तरीके से, अपने ऐप्लिकेशन में Navigation SDK टूल को इंटिग्रेट करें.
  2. GMSMapView को कॉन्फ़िगर करें.
  3. उपयोगकर्ता को नियम और शर्तें स्वीकार करने के लिए कहें. साथ ही, जगह की जानकारी से जुड़ी सेवाओं और बैकग्राउंड में सूचनाएं पाने की अनुमति दें.
  4. एक या उससे ज़्यादा डेस्टिनेशन वाला ऐरे बनाएं.
  5. मोड़-दर-मोड़ निर्देशों को कंट्रोल करने के लिए, GMSNavigator तय करें.

    • setDestinations का इस्तेमाल करके डेस्टिनेशन जोड़ें.
    • नेविगेशन शुरू करने के लिए, isGuidanceActive को true पर सेट करें.
    • अपने ऐप्लिकेशन की जांच करने, डिबग करने, और उसे दिखाने के लिए, रास्ते पर वाहन की प्रोग्रेस को सिम्युलेट करने के लिए, simulateLocationsAlongExistingRoute का इस्तेमाल करें.

कोड देखना

/*
 * 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)
  }

}
/*
 * 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

उपयोगकर्ता से ज़रूरी अनुमतियां मांगना

Navigation SDK टूल का इस्तेमाल करने से पहले, उपयोगकर्ता को नियमों और शर्तों से सहमत होना होगा. साथ ही, जगह की जानकारी की सेवाओं के इस्तेमाल की अनुमति देनी होगी, जो नेविगेशन के लिए ज़रूरी है. अगर आपका ऐप्लिकेशन बैकग्राउंड में चलेगा, तो उसे उपयोगकर्ता को निर्देशों से जुड़ी चेतावनी वाली सूचनाओं को अनुमति देने के लिए भी कहा जाना चाहिए. इस सेक्शन में, अनुमति के लिए ज़रूरी प्रॉम्प्ट दिखाने का तरीका बताया गया है.

जगह की जानकारी की सेवाओं को अनुमति देना

Navigation SDK टूल, जगह की जानकारी देने वाली सेवाओं का इस्तेमाल करता है. इसके लिए, उपयोगकर्ता की अनुमति लेना ज़रूरी है. जगह की जानकारी की सेवाएं चालू करने और अनुमति वाला डायलॉग बॉक्स दिखाने के लिए, यह तरीका अपनाएं:

  1. Info.plist में NSLocationAlwaysUsageDescription बटन जोड़ें.
  2. वैल्यू के लिए, कम शब्दों में बताएं कि आपके ऐप्लिकेशन को जगह की जानकारी से जुड़ी सेवाओं की ज़रूरत क्यों है. उदाहरण के लिए: "इस ऐप्लिकेशन को, बारी-बारी से निर्देश देने वाली नेविगेशन सुविधा के लिए, जगह की जानकारी की सेवाओं का इस्तेमाल करने की अनुमति चाहिए."

  3. अनुमति वाला डायलॉग बॉक्स दिखाने के लिए, जगह की जानकारी मैनेजर इंस्टेंस के requestAlwaysAuthorization() को कॉल करें.

SwiftObjective-C

self.locationManager.requestAlwaysAuthorization()

[_locationManager requestAlwaysAuthorization];

बैकग्राउंड में निर्देश पाने के लिए, सूचनाएं पाने की अनुमति देना

ऐप्लिकेशन के बैकग्राउंड में चलने के दौरान सूचनाएं देने के लिए, नेविगेशन SDK टूल को उपयोगकर्ता की अनुमति चाहिए. उपयोगकर्ता को ये सूचनाएं दिखाने की अनुमति देने के लिए, यह कोड जोड़ें:

SwiftObjective-C

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");
     }
   }];

नियम और शर्तें स्वीकार करना

नियम और शर्तों वाला डायलॉग बॉक्स दिखाने के लिए, नीचे दिए गए कोड का इस्तेमाल करें. साथ ही, उपयोगकर्ता के नियमों को स्वीकार करने पर नेविगेशन चालू करें. ध्यान दें कि इस उदाहरण में, जगह की जानकारी देने वाली सेवाओं और दिशा-निर्देश से जुड़ी चेतावनी की सूचनाओं (पहले दिखाई गई) का कोड शामिल है.

SwiftObjective-C

  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() तरीके को कॉल करें. इसके लिए, एक या एक से ज़्यादा डेस्टिनेशन (GMSNavigationWaypoint) वाला कलेक्शन इस्तेमाल करें. रास्ता मिलने पर, उसे मैप पर दिखाया जाता है. रास्ते के साथ-साथ दिशा-निर्देश पाने के लिए, पहले डेस्टिनेशन से शुरू करें. इसके लिए, कॉलबैक में isGuidanceActive को true पर सेट करें.

इस उदाहरण में दिखाया गया है कि:

  • दो डेस्टिनेशन वाला नया रूट बनाना.
  • निर्देश शुरू हो रहे हैं.
  • बैकग्राउंड में निर्देशों की सूचनाएं पाने की सुविधा चालू करना.
  • रास्ते पर यात्रा की जानकारी दिखाना (ज़रूरी नहीं).
  • कैमरे के मोड को "फ़ॉलो करें" पर सेट करना (ज़रूरी नहीं).

SwiftObjective-C

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 प्रॉपर्टी सेट करें, जैसा कि इस उदाहरण में दिखाया गया है:

SwiftObjective-C

self.mapView.travelMode = .cycling

_mapView.travelMode = GMSNavigationTravelModeCycling;

सड़कों को सेट करना जिनसे बचना है

किसी रास्ते पर हाइवे और/या टोल रोड से बचने के लिए, avoidsHighways और avoidsTolls BOOL प्रॉपर्टी का इस्तेमाल करें.

SwiftObjective-C

self.mapView.navigator?.avoidsTolls = true

_mapView.navigator.avoidsTolls = YES;

PlaceID फ़ाइंडर

रूट के डेस्टिनेशन के लिए इस्तेमाल किए जाने वाले प्लेस आईडी ढूंढने के लिए, PlaceID Finder का इस्तेमाल किया जा सकता है. GMSNavigationWaypoint की मदद से, placeID से कोई डेस्टिनेशन जोड़ें.

फ़्लोटिंग टेक्स्ट

अपने ऐप्लिकेशन में, फ़्लोटिंग टेक्स्ट को कहीं भी जोड़ा जा सकता है. हालांकि, ऐसा तब ही किया जा सकता है, जब Google एट्रिब्यूशन को कवर न किया गया हो. Navigation SDK, मैप पर किसी अक्षांश/देशांतर या लेबल पर टेक्स्ट को ऐंकर करने की सुविधा नहीं देता. ज़्यादा जानकारी के लिए, जानकारी वाली विंडो देखें.