iOS'te gezi takip etme

Platform seçin: Android iOS JavaScript

Bir geziyi takip ettiğinizde, tüketici uygulamanız seyahatin konumunu yardımcı olacak bir araçtır. Bunun için uygulamanızın başlatılması gerekiyor geziyi takip etme, gezi seyrini güncelleme ve gezi sırasında takip etmeyi bırakma gerekir.

Bu dokümanda, bu sürecin nasıl işlediği açıklanmaktadır.

Bir geziyi takip etmeye başlayın

Yolculuk paylaşımını kullanarak bir geziyi takip etmeye şu şekilde başlayabilirsiniz:

  • Teslim alma ve teslim alma konumları gibi tüm kullanıcı girişlerini toplamak ViewController ile başlayan fiyatlarla!

  • Yolculuk paylaşımını doğrudan başlatmak için yeni bir ViewController oluşturun.

Aşağıdaki örnekte, görünüm yüklemeleri.

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

Bir geziyi takip etmeyi bırakma

Tamamlandığında veya iptal edildiğinde takip etmeyi bırakıyorsunuz. Aşağıdakiler örnek, etkin seyahatin paylaşımının nasıl durdurulacağını gösterir.

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 seyrini güncelle

Bir gezi sırasında gezi seyrini şu şekilde yönetirsiniz:

Bir gezi tamamlandığında veya iptal edildiğinde, güncellemeleri dinlemeyi durdurun. Örnek için bkz. Güncellemeleri dinlemeyi durdurun.

Güncellemeleri dinlemeye başlayın örneği

Aşağıdaki örnekte, tripModel geri çağırmasının 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 dinleme örneği

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

Swift

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

Objective-C

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

Gezi güncellemelerini işleme örneği

Aşağıdaki örnekte, GMTCTripModelSubscriber öğesinin nasıl uygulanacağı gösterilmektedir protokolü kullanabilir.

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.
}

Gezi hatalarını işleme

tripModel için abone olduysanız ve hata oluşursa geri çağırmayı alabilirsiniz tripModel için delege etme yöntemini tripModel(_:didFailUpdateTripWithError:). Hata mesajları Google Cloud Hata Standardı'na uyar. Ayrıntılı hata için mesaj tanımlarını ve tüm hata kodlarını öğrenmek için Google Cloud Hataları belgeleri.

Gezi izleme sırasında sık karşılaşılan bazı hatalar aşağıda verilmiştir:

HTTP TBG Açıklama
400 GEÇERSİZ_BAĞ_DEĞİŞKEN Müşteri, geçersiz bir gezi adı belirtti. Gezi adı providers/{provider_id}/trips/{trip_id} biçimindedir. İlgili içeriği oluşturmak için kullanılan provider_id, şuna ait Cloud projesinin kimliği olmalıdır: son derece önemlidir.
401 KİMLİK DOĞRULAMADI Geçerli bir kimlik doğrulama kimlik bilgisi yoksa bu hatayı alırsınız. Örneğin, JWT jetonu gezi kimliği veya JWT jetonu olmadan imzalanmışsa süresi doldu.
403 PERMISSION_DENIED İstemcinin yeterli izni yoksa bu hatayı alırsınız. (örneğin, tüketici rolüne sahip bir kullanıcı, güncellemeyi çağırmaya çalışırsa) JWT jetonu geçersiz veya istemci projesi için API etkin değil. JWT jetonu eksik olabilir veya jeton şu durumdaki bir gezi kimliğiyle imzalanmış olabilir: istenen gezi kimliğiyle eşleşmiyor.
429 RESOURCE_EXHAUSTED Kaynak kotası sıfırdı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 son tarihi aşıldı. Bu hata yalnızca çağrıda bulunan bir yöntemin varsayılan teslim tarihinden (yani istenen son tarihin sunucunun isteği işlemesi için yeterli olmaması) ve Talep, verilen süre zarfında tamamlanmamıştır.

Tüketici SDK'sı Hatalarını Ele Alma

Tüketici SDK'sı, gezi güncelleme hatalarını bir geri çağırma aracılığıyla tüketici uygulamasına gönderir. mekanizmasıdır. Geri çağırma parametresi platforma özgü bir dönüş türüdür ( TripUpdateError ve Android'de NSError .

Durum kodlarını çıkart

Geri çağırmaya iletilen hatalar genellikle gRPC hatalarıdır ve ek bilgileri durum kodu biçiminde ayıklamak için kullanılır. tam listesini görmek için Durum kodları ve gRPC'de kullanılmaları.

Swift

NSError, tripModel(_:didFailUpdateTripWithError:) sonra geri aranı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: sonra geri aranı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 hata düzeltmesi.

Sunucu ve ağ hataları

Aşağıdaki durum kodları ağ veya sunucu hataları içindir ve çözmek için bir işlem yapması gerekmez. Tüketici SDK'sı otomatik olarak bu değişimlerden kurtulursunuz.

Durum KoduAçıklama
İPTAL EDİLDİ Sunucu, yanıt göndermeyi durdurdu. Bu durum, normalde sunucu sorunu.
İPTAL EDİLDİ Sunucu giden yanıtı sonlandırdı. Bu, normalde şu durumda olur:
arka plana gönderildiğinde veya arka planda bir değişiklik olduğunda
Tüketici uygulaması.
INTERRUPTED
DEADLINE_EXCEEDED Sunucunun yanıt vermesi çok uzun sürdü.
UNAVAILABLE Sunucu kullanılamıyor. Bu durum genellikle ağdan kaynaklanır sorun.

İstemci hataları

Aşağıdaki durum kodları istemci hataları içindir ve aşağıdaki durumlarla ilgili olarak işlem yapmanız gerekir: çözer. Tüketici SDK'sı siz geçiş yapana kadar geziyi yenilemeyi yeniden denemeye devam eder yolculuk paylaşımı da iptal edilir. Bununla birlikte, siz işlem yapana kadar geri dönüş işlemi gerçekleşmez.

Durum KoduAçıklama
GEÇERSİZ_BAĞ_DEĞİŞKEN Tüketici uygulaması geçersiz bir gezi adı belirtti; Gezi adı providers/{provider_id}/trips/{trip_id} biçimine uyar.
NOT_FOUND Gezi hiç oluşturulmadı.
PERMISSION_DENIED Tüketici uygulamasının izinleri yeterli değil. Bu hata aşağıdaki durumlarda oluşur:
  • Tüketici uygulamasının izinleri yok
  • Google Cloud'daki proje için Tüketici SDK'sı etkinleştirilmemiş Konsol.
  • JWT jetonu yok veya geçersiz.
  • JWT jetonu, seyahat isteği.
RESOURCE_EXHAUSTED Kaynak kotası sıfırdır veya trafik akışı hızı hız sınırlaması var.
KİMLİK DOĞRULAMADI Geçersiz bir JWT jetonu nedeniyle isteğin kimlik doğrulaması başarısız oldu. Bu hatası, JWT jetonu bir seyahat kimliği olmadan imzalandığında veya JWT jetonunun süresi dolduğunda.