CarPlay के लिए नेविगेशन चालू करें

इस सेक्शन में बताया गया है कि Apple CarPlay लाइब्रेरी के साथ नेविगेशन SDK टूल का इस्तेमाल करके, इन-डैश हेड यूनिट पर अपने ऐप्लिकेशन का नेविगेशन अनुभव कैसे दिखाया जा सकता है. अगर किसी ड्राइवर के इन-डैश सिस्टम में CarPlay काम करता है, तो ड्राइवर सीधे अपनी कार के डिसप्ले पर अपने फ़ोन को यूनिट से कनेक्ट करके आपके ऐप्लिकेशन को इस्तेमाल कर सकते हैं. आवाज़ से रास्ता बताने की सुविधा कार के स्पीकर पर भी काम करती है.

CarPlay ऐप्लिकेशन बनाने के लिए, Apple के यूज़र इंटरफ़ेस (यूआई) टेंप्लेट के सेट का इस्तेमाल किया जाता है. टेंप्लेट दिखाने और उसके अंदर डेटा देने की ज़िम्मेदारी आपके ऐप्लिकेशन की है.

इन-डैशबोर्ड सिस्टम में सुरक्षा की मंज़ूरी वाले इंटरैक्टिव एलिमेंट दिखते हैं, ताकि ड्राइवर बिना किसी मुश्किल के अपनी मंज़िल पर सुरक्षित तरीके से नेविगेट कर सके. अपने ऐप्लिकेशन को भी प्रोग्राम किया जा सकता है, ताकि ड्राइवर ऐप्लिकेशन से जुड़ी खास सुविधाओं का इस्तेमाल कर सके. जैसे, ऑर्डर स्वीकार या अस्वीकार करना या मैप पर ग्राहक की जगह देखना. ऑर्डर की स्थिति के अपडेट को इन-डैशबोर्ड यूनिट में दिखाने के लिए भी प्रोग्राम किया जा सकता है.

CarPlay और फ़ोन नेविगेशन में,
बाईं ओर दी गई इमेज में CarPlay नेविगेशन डिसप्ले का उदाहरण दिया गया है. दाईं ओर दी गई इमेज में वही नेविगेशन दिख रहा है जो फ़ोन पर दिखता है.

सेटअप

CarPlay के साथ शुरुआत करें

सबसे पहले, Apple के दस्तावेज़ के बारे में जानें:

  • https://developer.apple.com/carplay/
  • https://developer.apple.com/carplay/documentation/CarPlay-App-Programming-Guide.pdf

नेविगेशन SDK टूल सेट अप करना

  1. Apple का दस्तावेज़ पढ़ने के बाद, नेविगेशन SDK टूल के साथ काम किया जा सकता है.
  2. अगर आपने अपने ऐप्लिकेशन में नेविगेशन SDK टूल को पहले से इंटिग्रेट नहीं किया है, तो अपना प्रोजेक्ट सेट अप करें.
  3. अपने ऐप्लिकेशन के लिए, TurnByturn दिशा-निर्देश फ़ीड चालू करें.
  4. ज़रूरी नहीं. नेविगेशन SDK टूल से जनरेट किए गए आइकॉन का इस्तेमाल करें.
  5. यूज़र इंटरफ़ेस (यूआई) व्यू क्लास में दी गई GMSMapView क्लास का इस्तेमाल करके, मैप ड्रॉ करें. ज़्यादा जानकारी के लिए किसी रास्ते पर नेविगेट करना लेख देखें. TurnByturn लाइब्रेरी के डेटा की मदद से CPNavigationSession में जानकारी अपने-आप भर जाती है.

मैप और नेविगेशन यूज़र इंटरफ़ेस (यूआई) बनाएं

GMSMapView क्लास, मैप को रेंडर करती है और CPMapTemplate CarPlay स्क्रीन पर यूज़र इंटरफ़ेस (यूआई) को रेंडर करती है. यह काफ़ी हद तक फ़ोन के लिए GMSMapView जैसी ही सुविधा देता है, लेकिन सीमित इंटरैक्टिविटी के साथ.

Swift

init(window: CPWindow) {
    super.init(nibName: nil, bundle: nil)
    self.window = window

    // More CPMapTemplate initialization

}

override func viewDidLoad() {
    super.viewDidLoad()

    let mapViewOptions = GMSMapViewOptions()
    mapViewOptions.screen = window.screen
    mapViewOptions.frame = self.view.bounds

    mapView = GMSMapView(options: mapViewOptions)
    mapView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    mapView.settings.isNavigationHeaderEnabled = false
    mapView.settings.isNavigationFooterEnabled = false

    // Disable buttons: in CarPlay, no part of the map is clickable.
    // The app should instead place these buttons in the appropriate slots of the CarPlay template.
    mapView.settings.compassButton = false
    mapView.settings.isRecenterButtonEnabled = false
    mapView.shouldDisplaySpeedometer = false
    mapView.isMyLocationEnabled = true

    self.view.addSubview(mapView)
}

Objective-C

- (instancetype)initWithWindow:(CPWindow *)window {
  self = [super initWithNibName:nil bundle:nil];
  if (self) {
    _window = window;

  // More CPMapTemplate initialization
  }
}

- (void)viewDidLoad {
  [super viewDidLoad];
  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.screen = _window.screen;
  options.frame = self.view.bounds;
  _mapView = [[GMSMapView alloc] initWithOptions:options];
  _mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  _mapView.settings.navigationHeaderEnabled = NO;
  _mapView.settings.navigationFooterEnabled = NO;

  // Disable buttons: in CarPlay, no part of the map is clickable.
  // The app should instead place these buttons in the appropriate slots of the CarPlay template.
  _mapView.settings.compassButton = NO;
  _mapView.settings.recenterButtonEnabled = NO;

  _mapView.shouldDisplaySpeedometer = NO;
  _mapView.myLocationEnabled = YES;

  [self.view addSubview:_mapView];
}

मैप इंटरैक्शन चालू करें

ड्राइवर की सुरक्षा के लिए, CarPlay, स्क्रीन की सतह के इंटरैक्शन को CPMapTemplateDelegate तरीकों की एक सीरीज़ तक सीमित करता है. इन-डैश स्क्रीन पर मैप के साथ ड्राइवर के सीमित इंटरैक्शन के लिए, इन कॉलबैक का इस्तेमाल करें.

उपयोगकर्ता की अन्य कार्रवाइयों के लिए, CPMapButton का एक कलेक्शन बनाएं और उसे CPMapTemplate.mapButtons को असाइन करें.

नीचे दिया गया कोड, पैन करने, ज़ूम इन और ज़ूम आउट करने के साथ-साथ उपयोगकर्ता की जगह की जानकारी देने के लिए, पैनिंग इंटरैक्शन और बटन बनाता है.

इंटरैक्शन पैन करना

Swift

// MARK: CPMapTemplateDelegate
func mapTemplate(_ mapTemplate: CPMapTemplate, panBeganWith direction: CPMapTemplate.PanDirection) {

}

func mapTemplate(_ mapTemplate: CPMapTemplate, panWith direction: CPMapTemplate.PanDirection) {
    let scrollAmount = scrollAmount(for: direction)
    let scroll = GMSCameraUpdate.scrollBy(x: scrollAmount.x, y: scrollAmount.y)
    mapView.animate(with: scroll)
}

func mapTemplate(_ mapTemplate: CPMapTemplate, panEndedWith direction: CPMapTemplate.PanDirection) {
}

func scrollAmount(for panDirection: CPMapTemplate.PanDirection) -> CGPoint {
    let scrollDistance = 80.0
    var scrollAmount = CGPoint(x: 0, y: 0)
    switch panDirection {
        case .left:
            scrollAmount.x -= scrollDistance
            break;
        case .right:
            scrollAmount.x += scrollDistance
            break;
        case .up:
            scrollAmount.y += scrollDistance
            break;
        case .down:
            scrollAmount.y -= scrollDistance
            break;
        default:
            break;
    }
    if scrollAmount.x != 0 && scrollAmount.y != 0 {
        // Adjust length if scrolling diagonally.
        scrollAmount = CGPointMake(scrollAmount.x * sqrt(1.0/2.0), scrollAmount.y * sqrt(1.0/2.0))
    }
    return scrollAmount
}

Objective-C

#pragma mark - CPMapTemplateDelegate

- (void)mapTemplate:(CPMapTemplate *)mapTemplate panBeganWithDirection:(CPPanDirection)direction {
}

- (void)mapTemplate:(CPMapTemplate *)mapTemplate panWithDirection:(CPPanDirection)direction {
CGPoint scrollAmount = [self scrollAmountForPanDirection:direction];
GMSCameraUpdate *scroll = [GMSCameraUpdate scrollByX:scrollAmount.x Y:scrollAmount.y];
[_mapView animateWithCameraUpdate:scroll];
}

- (void)mapTemplate:(CPMapTemplate *)mapTemplate panEndedWithDirection:(CPPanDirection)direction {
}
- (CGPoint)scrollAmountForPanDirection:(CPPanDirection)direction {
  static const CGFloat scrollDistance = 80.;
  CGPoint scrollAmount = {0., 0.};
  if (direction & CPPanDirectionLeft) {
    scrollAmount.x = -scrollDistance;
  }
  if (direction & CPPanDirectionRight) {
    scrollAmount.x = scrollDistance;
  }
  if (direction & CPPanDirectionUp) {
    scrollAmount.y = -scrollDistance;
  }
  if (direction & CPPanDirectionDown) {
    scrollAmount.y = scrollDistance;
  }
  if (scrollAmount.x != 0 && scrollAmount.y != 0) {
  // Adjust length if scrolling diagonally.
  scrollAmount =
    CGPointMake(scrollAmount.x * (CGFloat)M_SQRT1_2, scrollAmount.y * (CGFloat)M_SQRT1_2);
  }
  return scrollAmount;
}

बटन के सामान्य इस्तेमाल

Swift

// MARK: Create Buttons

func createMapButtons() -> [CPMapButton] {
    let panButton = mapButton(systemImageName: "dpad.fill") { [weak self] in
        self?.didTapPanButton()
    }

    let zoomOutButton = mapButton(systemImageName: "minus.magnifyingglass") { [weak self] in
        self?.didTapZoomOutButton()
    }

    let zoomInButton = mapButton(systemImageName: "plus.magnifyingglass") { [weak self] in
        self?.didTapZoomInButton()
    }

    let myLocationButton = mapButton(systemImageName: "location") { [weak self] in
        self?.didTapMyLocationButton()
    }

    let mapButtons = [panButton, zoomOutButton, zoomInButton, myLocationButton]
    return mapButtons
}

func mapButton(systemImageName: String, handler: @escaping () -> Void) -> CPMapButton {

}


// MARK: Button callbacks

@objc func didTapPanButton() {
    mapTemplate?.showPanningInterface(animated: true)
}

@objc func didTapZoomOutButton() {
    mapView.animate(with: GMSCameraUpdate.zoomOut())
}

@objc func didTapZoomInButton() {
    mapView.animate(with: GMSCameraUpdate.zoomIn())
}

@objc func didTapMyLocationButton() {
    if let lastLocation = lastLocation {
        let cameraPosition = GMSCameraPosition(target: lastLocation.coordinate, zoom: 15)
        mapView.animate(to: cameraPosition)
    }
}

Objective-C

#pragma mark - Create Buttons

- (NSArray<CPMapButton *>*)createMapButtons {
    NSMutableArray<CPMapButton *> *mapButtons = [NSMutableArray<CPMapButton *> array];

    __weak __typeof__(self) weakSelf = self;
    CPMapButton *panButton = [self mapButtonWithSystemImageNamed:@"dpad.fill"
                                                        handler:^(CPMapButton *_) {
                                                        [weakSelf didTapPanButton];
                                                        }];
    [mapButtons addObject:panButton];

    CPMapButton *zoomOutButton =
        [self mapButtonWithSystemImageNamed:@"minus.magnifyingglass"
                                    handler:^(CPMapButton *_Nonnull mapButon) {
                                    [weakSelf didTapZoomOutButton];
                                    }];
    [mapButtons addObject:zoomOutButton];

    CPMapButton *zoomInButton =
        [self mapButtonWithSystemImageNamed:@"plus.magnifyingglass"
                                    handler:^(CPMapButton *_Nonnull mapButon) {
                                    [weakSelf didTapZoomInButton];
                                    }];
    [mapButtons addObject:zoomInButton];

    CPMapButton *myLocationButton =
        [self mapButtonWithSystemImageNamed:@"location"
                                    handler:^(CPMapButton *_Nonnull mapButton) {
                                    [weakSelf didTapMyLocationButton];
                                    }];
    [mapButtons addObject:myLocationButton];
    return mapButtons;
}

#pragma mark - Button Callbacks

- (void)didTapZoomOutButton {
[_mapView animateWithCameraUpdate:[GMSCameraUpdate zoomOut]];
}

- (void)didTapZoomInButton {
[_mapView animateWithCameraUpdate:[GMSCameraUpdate zoomIn]];
}

- (void)didTapMyLocationButton {
CLLocation *location = self.lastLocation;
if (location) {
    GMSCameraPosition *position =
        [[GMSCameraPosition alloc] initWithTarget:self.lastLocation.coordinate zoom:15.];
    [_mapView animateToCameraPosition:position];
}
}

- (void)didTapPanButton {
[_mapTemplate showPanningInterfaceAnimated:YES];
_isPanningInterfaceEnabled = YES;
}

- (void)didTapStopPanningButton {
[_mapTemplate dismissPanningInterfaceAnimated:YES];
_isPanningInterfaceEnabled = NO;
}

ध्यान दें: CarPlay स्क्रीन पर दूसरे रास्ते नहीं चुने जा सकते. उन्हें CarPlay के शुरू होने से पहले फ़ोन से चुना जाना चाहिए.

नेविगेशन के निर्देश दिखाएं

इस सेक्शन में डेटा फ़ीड के लिए लिसनर सेट अप करने का तरीका बताया गया है. साथ ही, दिशा-निर्देशों और यात्रा का अनुमान लगाने वाले पैनल में नेविगेशन के लिए दिशा-निर्देशों को भरने का तरीका बताया गया है. ज़्यादा जानकारी के लिए, CarPlay ऐप्लिकेशन प्रोग्रामिंग गाइड का"Create a CarPlay नेविगेशन ऐप्लिकेशन" सेक्शन देखें.

दिशा-निर्देश और यात्रा के अनुमान वाले पैनल में एक नेविगेशन कार्ड होता है. इस कार्ड में, मौजूदा यात्रा से जुड़ी नेविगेशन की जानकारी दिखती है. नेविगेशन SDK टूल में TurnByTurn लाइब्रेरी की मदद से, कुछ जानकारी उपलब्ध कराई जा सकती है. जैसे, सिंबल, टेक्स्ट, और बचा हुआ समय.

लिसनर सेट अप करें

मोड़-दर-मोड़ डेटा फ़ीड चालू करें में, इवेंट लिसनर सेट अप करने के लिए दिए गए निर्देशों का पालन करें.

नेविगेशन की जानकारी अपने-आप भरना

नीचे दिए गए कोड सैंपल के पहले हिस्से में, GMSNavigationNavInfo.timeToCurrentStepSeconds से CPTravelEstimate में अनुवाद करके, CarPlay पर यात्रा के लिए अनुमान लगाने का तरीका बताया गया है. इनके और दूसरे डिसप्ले एलिमेंट के बारे में ज़्यादा जानने के लिए, मोड़-दर-मोड़ डेटा फ़ीड चालू करें पर जाएं.

सैंपल के दूसरे हिस्से में, ऑब्जेक्ट बनाने और उसे CPManuevers के userInfo फ़ील्ड में सेव करने का तरीका बताया गया है. यह CPManeuverDisplayStyle तय करता है, जिसका इस्तेमाल लेन के दिशा-निर्देशों की जानकारी के लिए भी किया जाता है. ज़्यादा जानकारी के लिए Apple की CarPlay ऐप्लिकेशन प्रोग्रामिंग गाइड देखें.

Swift

// Get a CPTravelEstimate from GMSNavigationNavInfo
func getTravelEstimates(from navInfo:GMSNavigationNavInfo) -> CPTravelEstimates {
    let distanceRemaining = navInfo.roundedDistance(navInfo.distanceToCurrentStepMeters)
    let timeRemaining = navInfo.roundedTime(navInfo.timeToCurrentStepSeconds)
    let travelEstimates = CPTravelEstimates(distanceRemaining: distanceRemaining, timeRemaining: timeRemaining)
    return travelEstimates
}

//  Create an object to be stored in the userInfo field of CPManeuver to determine the CPManeuverDisplayStyle. 

/** An object to be stored in the userInfo field of a CPManeuver. */

struct ManeuverUserInfo {
    var stepInfo: GMSNavigationStepInfo
    var isLaneGuidance: Bool
}

func mapTemplate(_ mapTemplate: CPMapTemplate, displayStyleFor maneuver: CPManeuver) -> CPManeuverDisplayStyle {
    let userInfo = maneuver.userInfo
    if let maneuverUserInfo = userInfo as? ManeuverUserInfo {
        return maneuverUserInfo.isLaneGuidance ? .symbolOnly : .leadingSymbol
    }
    return .leadingSymbol
}

// Get a CPManeuver with instructionVariants and symbolImage from GMSNavigationStepInfo
func getManeuver(for stepInfo: GMSNavigationStepInfo) -> CPManeuver {
    let maneuver = CPManeuver()
    maneuver.userInfo = ManeuverUserInfo(stepInfo: stepInfo, isLaneGuidance: false)
    switch stepInfo.maneuver {
        case .destination:
            maneuver.instructionVariants = ["Your destination is ahead."]
            break
        case .destinationLeft:
            maneuver.instructionVariants = ["Your destination is ahead on your left."]
            break
        case .destinationRight:
            maneuver.instructionVariants = ["Your destination is ahead on your right."]
            break
        default:
            maneuver.attributedInstructionVariants = currentNavInfo?.instructions(forStep: stepInfo, options: instructionOptions)
            break
    }
    maneuver.symbolImage = stepInfo.maneuverImage(with: instructionOptions.imageOptions)
    return maneuver
}

// Get the lane image for a CPManeuver from GMSNavigationStepInfo
func laneGuidanceManeuver(for stepInfo: GMSNavigationStepInfo) -> CPManeuver? {
    let maneuver = CPManeuver()
    maneuver.userInfo = ManeuverUserInfo(stepInfo: stepInfo, isLaneGuidance: true)
    let lanesImage = stepInfo.lanesImage(with: imageOptions)
    guard let lanesImage = lanesImage else { return nil }
    maneuver.symbolImage = lanesImage
    return maneuver
}

Objective-C

// Get a CPTravelEstimate from GMSNavigationNavInfo
- (nonull CPTravelEstimates *)travelEstimates:(GMSNavigationNavInfo *_Nonnull navInfo) {
NSMeasurement<NSUnitLength *> *distanceRemaining = [navInfo roundedDistance:navInfo.distanceToCurrentStepMeters];
NSTimeInterval timeRemaining = [navInfo roundedTime:navInfo.timeToCurrentStepSeconds];
CPTravelEstimate* travelEstimate = [[CPTravelEstimates alloc] initWithDistanceRemaining:distanceRemaining
                                                timeRemaining:timeRemaining];
}
//  Create an object to be stored in the userInfo field of CPManeuver to determine the CPManeuverDisplayStyle. 

/** An object to be stored in the userInfo field of a CPManeuver. */
@interface ManeuverUserInfo : NSObject

@property(nonatomic, readonly, nonnull) GMSNavigationStepInfo *stepInfo;
@property(nonatomic, readonly, getter=isLaneGuidance) BOOL laneGuidance;

- (nonnull instancetype)initWithStepInfo:(GMSNavigationStepInfo *)stepInfo
                        isLaneGuidance:(BOOL)isLaneGuidance NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;

@end

- (CPManeuverDisplayStyle)mapTemplate:(CPMapTemplate *)mapTemplate
            displayStyleForManeuver:(nonnull CPManeuver *)maneuver {
ManeuverUserInfo *userInfo = maneuver.userInfo;
return userInfo.laneGuidance ? CPManeuverDisplayStyleSymbolOnly : CPManeuverDisplayStyleDefault;
}
// Get a CPManeuver with instructionVariants and symbolImage from GMSNavigationStepInfo
- (nonnull CPManeuver *)maneuverForStep:(nonnull GMSNavigationStepInfo *)stepInfo {
CPManeuver *maneuver = [[CPManeuver alloc] init];
maneuver.userInfo = [[ManeuverUserInfo alloc] initWithStepInfo:stepInfo isLaneGuidance:NO];
switch (stepInfo.maneuver) {
    case GMSNavigationManeuverDestination:
    maneuver.instructionVariants = @[ @"Your destination is ahead." ];
    break;
    case GMSNavigationManeuverDestinationLeft:
    maneuver.instructionVariants = @[ @"Your destination is ahead on your left." ];
    break;
    case GMSNavigationManeuverDestinationRight:
    maneuver.instructionVariants = @[ @"Your destination is ahead on your right." ];
    break;
    default: {
    maneuver.attributedInstructionVariants =
        [_currentNavInfo instructionsForStep:stepInfo options:_instructionOptions];
    break;
    }
}
maneuver.symbolImage = [stepInfo maneuverImageWithOptions:_instructionOptions.imageOptions];
return maneuver;
}
// Get the lane image for a CPManeuver from GMSNavigationStepInfo
- (nullable CPManeuver *)laneGuidanceManeuverForStep:(nonnull GMSNavigationStepInfo *)stepInfo {
CPManeuver *maneuver = [[CPManeuver alloc] init];
maneuver.userInfo = [[ManeuverUserInfo alloc] initWithStepInfo:stepInfo isLaneGuidance:YES];
UIImage *lanesImage = [stepInfo lanesImageWithOptions:_imageOptions];
if (!lanesImage) {
    return nil;
}
maneuver.symbolImage = lanesImage;
return maneuver;
}

दांव-पेच

CarPlay, CPManeuver क्लास का इस्तेमाल करता है, ताकि वह मोड़-दर-मोड़ निर्देश दे सके. पैमानों और लेन के दिशा-निर्देशों के बारे में ज़्यादा जानकारी के लिए, मोड़-दर-मोड़ डेटा फ़ीड चालू करें देखें.