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

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

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

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

Callback ที่แนะนำซึ่งใช้เพื่อตั้งค่าตัวเลือกการปรับแต่ง UI ในตอนแรก มีการประกาศใน GMTCMapViewDelegate mapViewDidInitialize Callback จะถูกเรียกใช้เมื่อออบเจ็กต์ 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 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 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:)