การปรับแต่ง UI ของ iOS

การใช้ Consumer SDK ช่วยให้คุณสามารถใช้ตัวทำเครื่องหมายที่กำหนดเองและ เส้นบอกเส้นทางในการออกแบบแอป องค์ประกอบการออกแบบเหล่านี้ช่วยให้แอปผู้บริโภค ของคุณแสดงตัวอย่างเส้นทางของรถได้แบบไดนามิก

คำแนะนำนี้อธิบายตัวเลือกที่ SDK มีให้ในพร็อพเพอร์ตี้ consumerMapStyleCoordinator พร็อพเพอร์ตี้นี้พร้อมให้ใช้งานผ่านคลาส GMTCMapView โดยจะครอบคลุมเฉพาะองค์ประกอบของ UI และถือว่าคุณมีแอป Consumer ที่ใช้งานได้ สำหรับข้อมูลเกี่ยวกับการตั้งค่าบริการแบ็กเอนด์ที่ Consumer SDK ต้องการ โปรดดูที่การเริ่มต้นใช้งาน Fleet Engine

กำลังเริ่มตัวเลือกการกำหนดค่า UI

โค้ดเรียกกลับที่แนะนำที่ใช้เพื่อตั้งค่าตัวเลือกการปรับแต่ง UI ในตอนแรกจะประกาศไว้ใน GMTCMapViewDelegate โค้ดเรียกกลับ mapViewDidInitialize จะทริกเกอร์เมื่อออบเจ็กต์ GMTCMapView พร้อมที่จะแสดงแผนที่ ตัวประสานงานรูปแบบเริ่มต้นแล้ว แต่ไม่มีองค์ประกอบของ UI

Swift

/** ViewController.swift */

class ViewController: UIViewController, GMTCMapViewDelegate {

  // MARK: - GMTCMapViewDelegate

  func mapViewDidInitialize(_ mapview: GMTCMapView) {
    // Set the UI customization options here.
  }
}

Objective-C

/** ViewController.m */

@interface ViewController () <GMTCMapViewDelegate>

#pragma mark GMTCMapViewDelegate

- (void)mapViewDidInitialize:(GMTCMapView *)mapview {
  // Set the UI customization options here.
}

เครื่องหมายที่กำหนดเอง

ตัวอย่างต่อไปนี้ใช้ GMTCMapView สำหรับการปรับแต่งรูปแบบเครื่องหมาย หากต้องการกำหนดประเภทเครื่องหมายและคุณสมบัติของเครื่องหมาย ให้ใช้ setMarkerStyleOptions(_:markerType:) ตัวเลือกตัวทำเครื่องหมายที่กำหนดเองจะลบล้างค่าเริ่มต้น ที่ Consumer SDK ระบุไว้

Swift

/** ViewController.swift */

private func changeMarkerStyle(
  markerStyleOptions: GMTCMarkerStyleOptions?,
  markerType: GMTCCustomizableMarkerType
) {
  let styleCoordinator = mapView.consumerMapStyleCoordinator
  styleCoordinator.setMarkerStyleOptions(markerStyleOptions, markerType: markerType)
}

/** To restore the default values, call setMarkerStyleOptions(_:markerType:) using nil for the GMTCMarkerStyleOptions parameter.
Here is an example of retrieving the active GMTCMarkerStyleOptions. */

private func retrieveMarkerStyle(markerType: GMTCCustomizableMarkerType) {
  let styleCoordinator = mapView.consumerMapStyleCoordinator

  // The 'markerStyleOptions' contains the stored style options for this marker type.
  let markerStyleOptions = styleCoordinator.markerStyleOptions(for: markerType)
}

Objective-C

/** ViewController.h */

- (void)changeMarkerStyle:(nullable GMTCMarkerStyleOptions *)markerStyleOptions
               markerType:(GMTCCustomizableMarkerType)markerType {
  GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;
  [styleCoordinator setMarkerStyleOptions:markerStyleOptions markerType:markerType];
}

/** To restore the default values, call setMarkerStyleOptions:markerStyleOptions:markerType: using nil for the GMTCMarkerStyleOptions parameter.
Here is an example of retrieving the active GMTCMarkerStyleOptions. */

- (void)retrieveMarkerStyle:(GMTCCustomizableMarkerType)markerType {
  GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;

  // The 'markerStyleOptions' contains the stored style options for this marker type.
  GMTCMarkerStyleOptions *markerStyleOptions = [styleCoordinator markerStyleOptionsForType:markerType];
}

ประเภทเครื่องหมาย

เครื่องหมายต่อไปนี้สามารถปรับแต่งได้

  • GMTCCustomizableMarkerType.unknown
  • GMTCCustomizableMarkerType.tripPickupPoint
  • GMTCCustomizableMarkerType.tripDropoffPoint
  • GMTCCustomizableMarkerType.tripVehicle
  • GMTCCustomizableMarkerType.intermediateDestination

ใช้ GMTCCustomizableMarkerType.tripPickupPoint, GMTCCustomizableMarkerType.intermediateDestination และ GMTCCustomizableMarkerType.tripDropoffPoint เพื่อปรับแต่งจุดอ้างอิงระหว่างการเดินทางและความคืบหน้าของคำสั่งซื้อ

ใช้ GMTCCustomizableMarkerType.tripVehicle เพื่อปรับแต่งไอคอนยานพาหนะ ระหว่างการเดินทางและคำสั่งซื้อ ไอคอนเครื่องหมายจะไม่เปลี่ยนแปลง ตามประเภทยานพาหนะจริงสำหรับการเดินทาง

ตัวเลือกเครื่องหมาย

พร็อพเพอร์ตี้ที่ปรับแต่งได้ซึ่งใช้ได้กับเครื่องหมายแต่ละรายการเป็นส่วนย่อยของที่พักที่ Google Maps MarkerOptions มีให้ Consumer SDK GMTCMarkerStyleOptions สร้างขึ้นโดยใช้เครื่องมือเริ่มต้น และจะทำการเปลี่ยนแปลงไม่ได้เมื่อสร้างแล้ว พร็อพเพอร์ตี้แต่ละรายการมีค่าเริ่มต้นไว้ จึงต้องระบุเฉพาะค่าที่กำหนดเองเท่านั้น

พร็อพเพอร์ตี้ต่อไปนี้สามารถปรับแต่งได้

  • groundAnchor
  • isVisible
  • iconView
  • icon
  • zIndex
  • isFlat

การตั้งค่า isVisible เป็น "เท็จ" เทียบเท่ากับ "การปิด" เครื่องหมาย ควรให้ข้อมูลที่เพียงพอเพื่อให้คุณใช้องค์ประกอบ UI ของตัวเองแทนได้

ตัวอย่าง

Swift

/** MapViewController.swift */

private func updateMarkerUIOptions() {
  // Get the GMTCConsumerMapStyleCoordinator
  let consumerMapStyleCoordinator = mapView.consumerMapStyleCoordinator

  // The marker type that you would like to set custom UI options for.
  let customizableMarkerType = GMTCCustomizableMarkerType.tripVehicle

  // Initializing marker options.
  let markerStyleOptions = GMTCMutableMarkerStyleOptions()
  markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor
  markerStyleOptions.icon = icon
  markerStyleOptions.zIndex = 100
  markerStyleOptions.isFlat = false
  markerStyleOptions.isVisible = true

  consumerMapStyleCoordinator.setMarkerStyleOptions(markerStyleOptions, markerType: customizableMarkerType)

  // Reset marker options to default values.
  consumerMapStyleCoordinator.setMarkerStyleOptions(nil, markerType: customizableMarkerType)
}

Objective-C

/** MapViewController.m */

- (void)updateMarkerUIOptions {
  // Get the GMTCConsumerMapStyleCoordinator
  GMTCConsumerMapStyleCoordinator *consumerMapStyleCoordinator = [_mapView consumerMapStyleCoordinator];

  // The marker type that you would like to set custom UI options for.
  GMTCCustomizableMarkerType customizableMarkerType = GMTCCustomizableMarkerTypeTripVehicle;

  // Initializing marker options.
  GMTCMutableMarkerStyleOptions *markerStyleOptions =
      [[GMTCMutableMarkerStyleOptions alloc] init];
  markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor;
  markerStyleOptions.icon = icon;
  markerStyleOptions.zIndex = 100;
  markerStyleOptions.isFlat = NO;
  markerStyleOptions.isVisible = YES;

  [consumerMapStyleCoordinator setMarkerStyleOptions:markerStyleOptions markerType:customizableMarkerType];

  // Reset marker options to default values.
  [consumerMapStyleCoordinator setMarkerStyleOptions:nil markerType:customizableMarkerType];
}

การอัปเดตเวลาถึงโดยประมาณแบบไดนามิกสำหรับเครื่องหมายการรับสินค้า

หากต้องการสร้างเครื่องหมายการรับสินค้าที่แสดงเวลาถึงโดยประมาณที่อัปเดตอยู่เรื่อยๆ ให้อัปเดตตัวเลือกรูปแบบเครื่องหมายสำหรับ GMTCCustomizableMarkerType.tripPickupPoint

ตัวอย่าง

Swift

/** MapViewController.swift */

/// Updates the ETA every minute by creating a Timer that repeats every minute.
private func schedulePickupMarkerStyleUpdates() {
  Timer.scheduledTimer(
    timeInterval: 60.0,  // Update marker ETA every minute.
    target: self,
    selector: #selector(updatePickupMarkerETA),
    userInfo: nil,
    repeats: true)
}

/// Updates the marker options for GMTCCustomizableMarkerType.tripPickupPoint for the current time.
@objc private func updatePickupMarkerETA() {
  let consumerMapStyleCoordinator = mapView.consumerMapStyleCoordinator
  let previousOptions = consumerMapStyleCoordinator.markerStyleOptions(for: .tripPickupPoint)

  // Get updated ETA icon.
  let updatedETAIcon = pickupIconForCurrentTime()

  let markerStyleOptions = GMTCMutableMarkerStyleOptions()
  markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor
  markerStyleOptions.icon = updatedETAIcon
  markerStyleOptions.zIndex = 100
  markerStyleOptions.isFlat = false
  markerStyleOptions.isVisible = true

  consumerMapStyleCoordinator.setMarkerStyleOptions(markerStyleOptions, markerType: .tripPickupPoint)
}

Objective-C

/** MapViewController.m */

/** Updates the ETA every minute by creating an NSTimer that repeats every minute. */
- (void)schedulePickupMarkerStyleUpdates {
  [NSTimer scheduledTimerWithTimeInterval:60.0 // Update marker ETA every minute.
                                   target:self
                                 selector:@selector(updatePickupMarkerETA)
                                 userInfo:nil
                                  repeats:YES];
}

/** Updates the marker options for GMTCCustomizableMarkerTypeTripPickupPoint for the current time. */
- (void)updatePickupMarkerETA {
  GMTCConsumerMapStyleCoordinator *consumerMapStyleCoordinator = [_mapView consumerMapStyleCoordinator];
  GMTCMarkerStyleOptions *previousOptions = [consumerMapStyleCoordinator markerStyleOptionsForType:GMTCCustomizableMarkerTypeTripPickupPoint];

  // Get updated ETA icon.
  UIImage *updatedETAIcon = [self pickupIconForCurrentTime];

  GMTCMutableMarkerStyleOptions *markerStyleOptions =
                               [[GMTCMutableMarkerStyleOptions alloc] init];
  markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor;
  markerStyleOptions.icon = updatedETAIcon;
  markerStyleOptions.zIndex = 100;
  markerStyleOptions.isFlat = NO;
  markerStyleOptions.isVisible = YES;

  [consumerMapStyleCoordinator setMarkerStyleOptions:markerStyleOptions markerType:GMTCCustomizableMarkerTypeTripPickupPoint];
}

เส้นประกอบที่กำหนดเอง

มีการตั้งค่าการปรับแต่งเส้นประกอบโดยใช้ GMTCConsumerMapStyleCoordinator#setPolylineStyleOptions(_:polylineType:)

ตัวอย่างต่อไปนี้แสดงวิธีตั้งค่าตัวเลือกรูปแบบของเส้นประกอบ

Swift

/** ViewController.swift */

private func changePolylineStyleOptions(
  polylineStyleOptions: GMTCPolylineStyleOptions?,
  polylineType: GMTCPolylineType
) {
  let styleCoordinator = mapView.consumerMapStyleCoordinator
  styleCoordinator.setPolylineStyleOptions(polylineStyleOptions, polylineType: polylineType)
}

/* Setting custom polyline options will override the default values provided by the Consumer SDK.
The default values can be restored by calling setPolylineStyleOptions(_:polylineType:) with nil for the GMTCPolylineStyleOptions.
The active GMTCPolylineStyleOptions can be retrieved via */

private func retrievePolylineStyleOptions(for polylineType: GMTCPolylineType) {
  let styleCoordinator = mapView.consumerMapStyleCoordinator

  // The 'polylineStyleOptions' contains the stored style options for this polyline type.
  let polylineStyleOptions = styleCoordinator.polylineStyleOptions(for: polylineType)
}

Objective-C

/** ViewController.h */

- (void)changePolylineStyleOptions:(nullable GMTCPolylineStyleOptions *)polylineStyleOptions
                      polylineType:(GMTCPolylineType)polylineType {
  GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;
  [styleCoordinator setPolylineStyleOptions:polylineStyleOptions polylineType:polylineType];
}

/* Setting custom polyline options will override the default values provided by the Consumer SDK.
The default values can be restored by calling setPolylineStyleOptions:polylineType: with nil for the GMTCPolylineStyleOptions.
The active GMTCPolylineStyleOptions can be retrieved via */

- (void)retrievePolylineStyleOptionsForType:(GMTCPolylineType)polylineType {
  GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;

  // The 'polylineStyleOptions' contains the stored style options for this polyline type.
  GMTCPolylineStyleOptions *polylineStyleOptions = [styleCoordinator polylineStyleOptionsForType:polylineType];
}

ประเภทเส้นประกอบ

ประเภทเส้นประกอบที่คุณปรับแต่งได้มีดังนี้

  • GMTCPolylineType.activeRoute
  • GMTCPolylineType.remainingRoute

GMTCPolylineType.activeRoute และ GMTCPolylineType.remainingRoute จะแสดงตลอด ความคืบหน้าในการเดินทางและการสั่งซื้อ GMTCPolylineType.activeRoute คือเส้นทางที่ยานพาหนะใช้ไปยังจุดถัดไปของผู้โดยสาร ไม่ว่าจะเป็นรถไปรับเองหรือจอดส่ง GMTCPolylineType.remainingRoute คือส่วนของการเดินทางที่จะยังคงอยู่หลังจากยานพาหนะแล่นผ่านGMTCPolylineType.activeRoute

คุณสมบัติของโพลีไลน์

พร็อพเพอร์ตี้ที่ปรับแต่งได้สำหรับเส้นแต่ละเส้นเป็นชุดย่อยของที่พักที่ระบุไว้ใน Google Maps PolylineOptions Consumer SDK GMTCPolylineStyleOptions สร้างขึ้นโดยใช้เครื่องมือเริ่มต้น ซึ่งอาจเปลี่ยนไม่ได้หรือเปลี่ยนแปลงได้หากคุณต้องการระบุค่าที่กำหนดเองสำหรับพร็อพเพอร์ตี้ใดๆ โดยจะมีค่าเริ่มต้นสำหรับแต่ละพร็อพเพอร์ตี้

พร็อพเพอร์ตี้ต่อไปนี้สามารถปรับแต่งได้

  • color
  • width
  • isVisible
  • isTrafficEnabled

การตั้งค่า isVisible เป็น false เทียบเท่ากับการปิดใช้โพลีไลน์ ตามค่าเริ่มต้น ระบบจะตั้งค่า isTrafficEnabled เป็น false

ตัวอย่าง

Swift

/** MapViewController.swift */

private func updatePolylineUIOptions() {
  // Get the GMTCConsumerMapStyleCoordinator
  let consumerMapStyleCoordinator = mapView.consumerMapStyleCoordinator

  // The polyline type that you would like to set custom UI options for.
  let customizablePolylineType = GMTCPolylineType.activeRoute

  // Initializing polyline options with default values (immutable version).
  let polylineStyleOptions = GMTCPolylineStyleOptions()
  consumerMapStyleCoordinator.setPolylineStyleOptions(
    polylineStyleOptions, polylineType: customizablePolylineType)

  // Initializing polyline options with custom values (mutable version).
  let mutablePolylineStyleOptions = GMTCMutablePolylineStyleOptions()
  mutablePolylineStyleOptions.isVisible = true
  mutablePolylineStyleOptions.isTrafficEnabled = true
  mutablePolylineStyleOptions.setTrafficColorFor(.slow, color: .yellow)
  mutablePolylineStyleOptions.setTrafficColorFor(.trafficJam, color: .purple)
  consumerMapStyleCoordinator.setPolylineStyleOptions(
    mutablePolylineStyleOptions, polylineType: customizablePolylineType)

  // Reset polyline options to default values.
  consumerMapStyleCoordinator.setPolylineStyleOptions(
    nil, polylineType: customizablePolylineType)
}

Objective-C

/** MapViewController.m */

- (void)updatePolylineUIOptions {
  // Get the GMTCConsumerMapStyleCoordinator
  GMTCConsumerMapStyleCoordinator *consumerMapStyleCoordinator = [_mapView consumerMapStyleCoordinator];

  // The polyline type that you would like to set custom UI options for.
  GMTCPolylineType customizablePolylineType = GMTCPolylineTypeActiveRoute;

  // Initializing polyline options with default values (immutable version).
  GMTCPolylineStyleOptions *polylineStyleOptions = [[GMTCPolylineStyleOptions alloc] init];
  [consumerMapStyleCoordinator setPolylineStyleOptions:polylineStyleOptions
                                          polylineType:customizablePolylineType];

  // Initializing polyline options with custom values (mutable version).
  GMTCMutablePolylineStyleOptions *mutablePolylineStyleOptions = [[GMTCMutablePolylineStyleOptions alloc] init];
  mutablePolylineStyleOptions.isVisible = YES;
  mutablePolylineStyleOptions.isTrafficEnabled = YES;
  [mutablePolylineStyleOptions setTrafficColorForSpeed:GMTSSpeedTypeSlow color:[UIColor yellowColor]];
  [mutablePolylineStyleOptions setTrafficColorForSpeed:GMTSSpeedTypeTrafficJam color:[UIColor purpleColor]];
  [consumerMapStyleCoordinator setPolylineStyleOptions:mutablePolylineStyleOptions
                                          polylineType:customizablePolylineType];

  // Reset polyline options to default values.
  [consumerMapStyleCoordinator setPolylineStyleOptions:nil
                                          polylineType:customizablePolylineType];
}

โพลีไลน์ที่รับรู้การจราจร

เลเยอร์การจราจรของเส้นประกอบจะถูกปิดใช้โดยค่าเริ่มต้น เมื่อเปิดใช้กับ polylineStyleOptions.isTrafficEnabled = true ระบบจะวาดส่วนที่แสดงถึงเส้นทางของการเข้าชมที่ไม่ใช่ปกติเป็นเส้นทาง

สภาพการจราจรแสดงเป็น 1 ใน 4 ความเร็ว ได้แก่ GMTSSpeedType.noData, GMTSSpeedType.normal, GMTSSpeedType.slow และ GMTSSpeedType.trafficJam คุณปรับแต่งสีที่แสดงถึงความเร็วการจัดประเภทแต่ละประเภทได้ด้วย setTrafficColorFor(_:color:)