iOS'te gezi takip etme

Platform seçin: Android iOS JavaScript

Bir geziyi takip ettiğinizde tüketici uygulamanız, tüketiciye uygun aracın konumunu gösterir. Bunu yapmak için uygulamanızın bir geziyi takip etmeye başlaması, gezi ilerlemesini güncellemesi ve gezi tamamlandığında takibi durdurması gerekir.

Bu belgede, söz konusu sürecin işleyiş şekli açıklanmaktadır.

Bir geziyi takip etmeye başlama

Bir geziyi takip etmeye başlamak için:

  • ViewController konumundan bırakma ve teslim alma yerleri gibi tüm kullanıcı girişlerini toplayın.

  • Bir geziyi doğrudan takip etmeye başlamak için yeni bir ViewController oluşturun.

Aşağıdaki örnekte, görünüm yüklendikten hemen sonra bir geziyi takip etmeye nasıl başlayacağınız gösterilmektedir.

Swift

/*
 * MapViewController.swift
 */
override func viewDidLoad() {
  super.viewDidLoad()
  ...
  self.mapView = GMTCMapView(frame: UIScreen.main.bounds)
  self.mapView.delegate = self
  self.view.addSubview(self.mapView)
}

func mapViewDidInitializeCustomerState(_: GMTCMapView) {
  self.mapView.pickupLocation = self.selectedPickupLocation
  self.mapView.dropoffLocation = self.selectedDropoffLocation

  self.startConsumerMatchWithLocations(
    pickupLocation: self.mapView.pickupLocation!,
    dropoffLocation: self.mapView.dropoffLocation!
  ) { [weak self] (tripName, error) in
    guard let strongSelf = self else { return }
    if error != nil {
      // print error message.
      return
    }
    let tripService = GMTCServices.shared().tripService
    // Create a tripModel instance for listening the update of the trip
    // specified by this trip name.
    let tripModel = tripService.tripModel(forTripName: tripName)
    // Create a journeySharingSession instance based on the tripModel
    let journeySharingSession = GMTCJourneySharingSession(tripModel: tripModel)
    // Add the journeySharingSession instance on the mapView for UI updating.
    strongSelf.mapView.show(journeySharingSession)
    // Register for the trip update events.
    tripModel.register(strongSelf)

    strongSelf.currentTripModel = tripModel
    strongSelf.currentJourneySharingSession = journeySharingSession
    strongSelf.hideLoadingView()
  }

  self.showLoadingView()
}

Objective-C

/*
 * MapViewController.m
 */
- (void)viewDidLoad {
  [super viewDidLoad];
  ...
  self.mapView = [[GMTCMapView alloc] initWithFrame:CGRectZero];
  self.mapView.delegate = self;
  [self.view addSubview:self.mapView];
}

// Handle the callback when the GMTCMapView did initialized.
- (void)mapViewDidInitializeCustomerState:(GMTCMapView *)mapview {
  self.mapView.pickupLocation = self.selectedPickupLocation;
  self.mapView.dropoffLocation = self.selectedDropoffLocation;

  __weak __typeof(self) weakSelf = self;
  [self startTripBookingWithPickupLocation:self.selectedPickupLocation
                           dropoffLocation:self.selectedDropoffLocation
                                completion:^(NSString *tripName, NSError *error) {
                                  __typeof(self) strongSelf = weakSelf;
                                  GMTCTripService *tripService = [GMTCServices sharedServices].tripService;
                                  // Create a tripModel instance for listening to updates to the trip specified by this trip name.
                                  GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];
                                  // Create a journeySharingSession instance based on the tripModel.
                                  GMTCJourneySharingSession *journeySharingSession =
                                    [[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];
                                  // Add the journeySharingSession instance on the mapView for updating the UI.
                                  [strongSelf.mapView showMapViewSession:journeySharingSession];
                                  // Register for trip update events.
                                  [tripModel registerSubscriber:self];

                                  strongSelf.currentTripModel = tripModel;
                                  strongSelf.currentJourneySharingSession = journeySharingSession;
                                  [strongSelf hideLoadingView];
                                }];
    [self showLoadingView];
}

Gezileri takip etmeyi bırakma

Tamamlanan veya iptal edilen gezileri takip etmeyi bırakabilirsiniz. Aşağıdaki örnekte, etkin gezi paylaşımının nasıl durdurulacağı gösterilmektedir.

Swift

/*
 * MapViewController.swift
 */
func cancelCurrentActiveTrip() {
  // Stop the tripModel
  self.currentTripModel.unregisterSubscriber(self)

  // Remove the journey sharing session from the mapView's UI stack.
  self.mapView.hide(journeySharingSession)
}

Objective-C

/*
 * MapViewController.m
 */
- (void)cancelCurrentActiveTrip {
  // Stop the tripModel
  [self.currentTripModel unregisterSubscriber:self];

  // Remove the journey sharing session from the mapView's UI stack.
  [self.mapView hideMapViewSession:journeySharingSession];
}

Gezi ilerleme durumunu güncelleme

Gezilerde, gezi ilerlemesini aşağıdaki şekilde yönetirsiniz:

Bir yolculuk tamamlandığında veya iptal edildiğinde güncellemeleri dinlemeyi durdurun. Örnek için Güncellemeleri dinlemeyi durdurma örneği başlıklı makaleyi inceleyin.

Güncellemeleri dinlemeye başlama örneği

Aşağıdaki örnekte, tripModel geri çağırma işlevinin nasıl kaydedileceği gösterilmektedir.

Swift

/*
 * MapViewController.swift
 */
override func viewDidLoad() {
  super.viewDidLoad()
  // Register for trip update events.
  self.currentTripModel.register(self)
}

Objective-C

/*
 * MapViewController.m
 */
- (void)viewDidLoad {
  [super viewDidLoad];
  // Register for trip update events.
  [self.currentTripModel registerSubscriber:self];
  ...
}

Güncellemeleri dinlemeyi durdurma örneği

Aşağıdaki örnekte tripModel geri çağırma kaydının nasıl iptal edileceği gösterilmektedir.

Swift

/*
 * MapViewController.swift
 */
deinit {
  self.currentTripModel.unregisterSubscriber(self)
}

Objective-C

/*
 * MapViewController.m
 */
- (void)dealloc {
  [self.currentTripModel unregisterSubscriber:self];
  ...
}

Seyahat güncellemelerini işleme örneği

Aşağıdaki örnekte, gezi durumu güncellendiğinde geri çağırma işlemlerini yönetmek için GMTCTripModelSubscriber protokolünün nasıl uygulanacağı gösterilmektedir.

Swift

/*
 * MapViewController.swift
 */
func tripModel(_: GMTCTripModel, didUpdate trip: GMTSTrip?, updatedPropertyFields: GMTSTripPropertyFields) {
  // Update the UI with the new `trip` data.
  self.updateUI(with: trip)
}

func tripModel(_: GMTCTripModel, didUpdate tripStatus: GMTSTripStatus) {
  // Handle trip status did change.
}

func tripModel(_: GMTCTripModel, didUpdateActiveRouteRemainingDistance activeRouteRemainingDistance: Int32) {
  // Handle remaining distance of active route did update.
}

func tripModel(_: GMTCTripModel, didUpdateActiveRoute activeRoute: [GMTSLatLng]?) {
  // Handle trip active route did update.
}

func tripModel(_: GMTCTripModel, didUpdate vehicleLocation: GMTSVehicleLocation?) {
  // Handle vehicle location did update.
}

func tripModel(_: GMTCTripModel, didUpdatePickupLocation pickupLocation: GMTSTerminalLocation?) {
  // Handle pickup location did update.
}

func tripModel(_: GMTCTripModel, didUpdateDropoffLocation dropoffLocation: GMTSTerminalLocation?) {
  // Handle drop off location did update.
}

func tripModel(_: GMTCTripModel, didUpdatePickupETA pickupETA: TimeInterval) {
  // Handle the pickup ETA did update.
}

func tripModel(_: GMTCTripModel, didUpdateDropoffETA dropoffETA: TimeInterval) {
  // Handle the drop off ETA did update.
}

func tripModel(_: GMTCTripModel, didUpdateRemaining remainingWaypoints: [GMTSTripWaypoint]?) {
  // Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}

func tripModel(_: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
  // Handle the error.
}

func tripModel(_: GMTCTripModel, didUpdateIntermediateDestinations intermediateDestinations: [GMTSTerminalLocation]?) {
  // Handle the intermediate destinations being updated.
}

func tripModel(_: GMTCTripModel, didUpdateActiveRouteTraffic activeRouteTraffic: GMTSTrafficData?) {
  // Handle trip active route traffic being updated.
}

Objective-C

/*
 * MapViewController.m
 */
#pragma mark - GMTCTripModelSubscriber implementation

- (void)tripModel:(GMTCTripModel *)tripModel
            didUpdateTrip:(nullable GMTSTrip *)trip
    updatedPropertyFields:(enum GMTSTripPropertyFields)updatedPropertyFields {
  // Update the UI with the new `trip` data.
  [self updateUIWithTrip:trip];
  ...
}

- (void)tripModel:(GMTCTripModel *)tripModel didUpdateTripStatus:(enum GMTSTripStatus)tripStatus {
  // Handle trip status did change.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateActiveRouteRemainingDistance:(int32_t)activeRouteRemainingDistance {
   // Handle remaining distance of active route did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateActiveRoute:(nullable NSArray<GMTSLatLng *> *)activeRoute {
  // Handle trip active route did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateVehicleLocation:(nullable GMTSVehicleLocation *)vehicleLocation {
  // Handle vehicle location did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdatePickupLocation:(nullable GMTSTerminalLocation *)pickupLocation {
  // Handle pickup location did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateDropoffLocation:(nullable GMTSTerminalLocation *)dropoffLocation {
  // Handle drop off location did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel didUpdatePickupETA:(NSTimeInterval)pickupETA {
  // Handle the pickup ETA did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateRemainingWaypoints:(nullable NSArray<GMTSTripWaypoint *> *)remainingWaypoints {
  // Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}

- (void)tripModel:(GMTCTripModel *)tripModel didUpdateDropoffETA:(NSTimeInterval)dropoffETA {
  // Handle the drop off ETA did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(nullable NSError *)error {
  // Handle the error.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateIntermediateDestinations:
        (nullable NSArray<GMTSTerminalLocation *> *)intermediateDestinations {
  // Handle the intermediate destinations being updated.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateActiveRouteTraffic:(nullable GMTSTrafficData *)activeRouteTraffic {
  // Handle trip active route traffic being updated.
}

Seyahat hatalarını giderme

tripModel aboneliğiniz varsa ve bir hata oluşursa tripModel(_:didFailUpdateTripWithError:) temsilci yöntemini uygulayarak tripModel geri arama işlevini kullanabilirsiniz. Hata mesajları, Google Cloud Error standardına uygundur. Ayrıntılı hata mesajı tanımları ve tüm hata kodları için Google Cloud Hataları dokümanlarına bakın.

Seyahat izleme sırasında oluşabilecek bazı yaygın hatalar şunlardır:

HTTP TBG Açıklama
400 INVALID_ARGUMENT İstemci, geçersiz bir gezi adı belirtti. Gezinin adı, providers/{provider_id}/trips/{trip_id} biçiminde olmalıdır. provider_id, servis sağlayıcıya ait Cloud projesinin kimliği olmalıdır.
401 UNAUTHENTICATED Geçerli kimlik doğrulama kimlik bilgileri yoksa bu hatayı alırsınız. Örneğin, JWT jetonu bir gezi kimliği olmadan imzalanmışsa veya JWT jetonunun süresi dolmuşsa.
403 PERMISSION_DENIED İstemcinin yeterli izni yoksa (örneğin, tüketici rolüne sahip bir kullanıcı updateTrip'i çağırmaya çalışıyorsa), JWT jetonu geçersizse veya API, istemci projesi için etkinleştirilmemişse bu hatayı alırsınız. JWT jetonu eksik olabilir veya jeton, istenen gezi kimliğiyle eşleşmeyen bir gezi kimliğiyle imzalanmış olabilir.
429 RESOURCE_EXHAUSTED Kaynak kotası sıfır veya trafik hızı sınırı aşıyor.
503 UNAVAILABLE Hizmet kullanılamıyor. Genellikle sunucu kapalıdır.
504 DEADLINE_EXCEEDED İstek bitiş tarihi aşıldı. Bu hata yalnızca arayan, yöntemin varsayılan son tarihinden daha kısa bir son tarih belirlerse (yani, istenen son tarih, sunucunun isteği işlemesi için yeterli değilse) ve istek son tarih içinde tamamlanmazsa oluşur.

Tüketici SDK'sı Hatalarını İşleme

Consumer SDK, geri çağırma mekanizmasını kullanarak seyahat güncelleme hatalarını tüketici uygulamasına gönderir. Geri çağırma parametresi, platforma özgü bir dönüş türüdür ( TripUpdateError Android'de ve NSError iOS'te).

Ayıklama durum kodları

Geri çağırmaya iletilen hatalar genellikle gRPC hatalarıdır ve bunlardan durum kodu şeklinde ek bilgiler de çıkarabilirsiniz. Durum kodlarının tam listesi için Durum kodları ve gRPC'de kullanımları başlıklı makaleyi inceleyin.

Swift

NSError, tripModel(_:didFailUpdateTripWithError:) içinde geri çağrılır.

// Called when there is a trip update error.
func tripModel(_ tripModel: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
  // Check to see if the error comes from gRPC.
  if let error = error as NSError?, error.domain == "io.grpc" {
    let gRPCErrorCode = error.code
    ...
  }
}

Objective-C

NSError, tripModel:didFailUpdateTripWithError: içinde geri çağrılır.

// Called when there is a trip update error.
- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(NSError *)error {
  // Check to see if the error comes from gRPC.
  if ([error.domain isEqualToString:@"io.grpc"]) {
    NSInteger gRPCErrorCode = error.code;
    ...
  }
}

Durum kodlarını yorumlama

Durum kodları iki tür hatayı kapsar: sunucu ve ağ ile ilgili hatalar ve istemci tarafı hataları.

Sunucu ve ağ hataları

Aşağıdaki durum kodları ağ veya sunucu hatalarıyla ilgilidir ve bunları düzeltmek için herhangi bir işlem yapmanız gerekmez. Consumer SDK, bu hataları otomatik olarak düzeltir.

Durum KoduAçıklama
İPTAL EDİLDİ Sunucu, yanıt göndermeyi durdurdu. Bu durum genellikle bir sunucu sorunundan kaynaklanır.
İPTAL EDİLDİ Sunucu, giden yanıtı sonlandırdı. Bu durum genellikle
uygulama arka plana gönderildiğinde veya tüketici uygulamasında durum değişikliği olduğunda meydana gelir.
KESİLDİ
DEADLINE_EXCEEDED Sunucunun yanıt vermesi çok uzun sürdü.
UNAVAILABLE Sunucu kullanılamıyordu. Bu durum genellikle ağ sorunundan kaynaklanır.

İstemci hataları

Aşağıdaki durum kodları istemci hataları içindir ve bunları çözmek için işlem yapmanız gerekir. Tüketici SDK'sı, yolculuk paylaşımını sonlandırana kadar yolculuğu yenilemeyi tekrar denemeye devam eder ancak siz işlem yapana kadar kurtarılamaz.

Durum KoduAçıklama
INVALID_ARGUMENT Tüketici uygulaması geçersiz bir gezi adı belirtmiş. Gezi adı, providers/{provider_id}/trips/{trip_id} biçiminde olmalıdır.
NOT_FOUND Gezi hiç oluşturulmamış olabilir.
PERMISSION_DENIED Tüketici uygulamasının izinleri yetersiz. Bu hata aşağıdaki durumlarda ortaya çıkar:
  • Tüketici uygulamasının izinleri yok
  • Google Cloud Console'da proje için Consumer SDK etkinleştirilmemiştir.
  • JWT jetonu eksik veya geçersiz.
  • JWT jetonu, istenen seyahatle eşleşmeyen bir seyahat kimliğiyle imzalanmış.
RESOURCE_EXHAUSTED Kaynak kotası sıfır veya trafik akışı hızı, hız sınırını aşıyor.
UNAUTHENTICATED Geçersiz JWT jetonu nedeniyle istek kimlik doğrulama işleminde başarısız oldu. Bu hata, JWT jetonu seyahat kimliği olmadan imzalandığında veya JWT jetonunun süresi dolduğunda oluşur.