Tùy chỉnh điểm đánh dấu

Chọn nền tảng: Android iOS JavaScript

Trước khi tuỳ chỉnh điểm đánh dấu (hoặc hình nhiều đường), trước tiên bạn cần khởi chạy Các tuỳ chọn tuỳ chỉnh giao diện người dùng.

Khởi chạy các tuỳ chọn tuỳ chỉnh giao diện người dùng

Lệnh gọi lại đề xuất dùng để đặt các tuỳ chọn tuỳ chỉnh giao diện người dùng ban đầu sẽ được khai báo trong GMTCMapViewDelegate. mapViewDidInitialize lệnh gọi lại được kích hoạt khi đối tượng GMTCMapView đã sẵn sàng kết xuất bản đồ. Trình điều phối kiểu được khởi chạy nhưng không có phần tử giao diện người dùng nào.

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

Tuỳ chỉnh điểm đánh dấu

Ví dụ sau đây sử dụng GMTCMapView để tuỳ chỉnh kiểu điểm đánh dấu. Để đặt loại điểm đánh dấu và các thuộc tính của loại điểm đánh dấu đó, hãy sử dụng setMarkerStyleOptions(_:markerType:). Các tuỳ chọn cho điểm đánh dấu tuỳ chỉnh của bạn sẽ ghi đè các giá trị mặc định do SDK người tiêu dùng cung cấp.

Swift

/** MapViewController.swift */

func updateMarkerUIOptions() {
  let customizableMarkerType = GMTCCustomizableMarkerType.tripVehicle
  let markerStyleOptions = GMTCMutableMarkerStyleOptions()
  markerStyleOptions.groundAnchor = groundAnchor
  markerStyleOptions.isVisible = true
  markerStyleOptions.icon = icon
  markerStyleOptions.zIndex = 100
  markerStyleOptions.isFlat = false
  let coordinator = self.mapView.consumerMapStyleCoordinator
  coordinator.setMarkerStyleOptions(markerStyleOptions, markerType: customizableMarkerType)
}

/** 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


/** MapViewController.m */

- (void)updateMarkerUIOptions {
  // The marker type that you would like to set custom UI options for.
  GMTCCustomizableMarkerType customizableMarkerType = GMTCCustomizableMarkerTypeTripVehicle;

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

  [[_mapView consumerMapStyleCoordinator] setMarkerStyleOptions:markerStyleOptions markerType:customizableMarkerType];
}

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

Các loại điểm đánh dấu

Bạn có thể tuỳ chỉnh các điểm đánh dấu sau:

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

Sử dụng GMTCCustomizableMarkerType.tripPickupPoint, GMTCCustomizableMarkerType.intermediateDestinationGMTCCustomizableMarkerType.tripDropoffPoint để tuỳ chỉnh điểm tham chiếu trong khoảng thời gian chia sẻ hành trình.

Dùng GMTCCustomizableMarkerType.tripVehicle để tuỳ chỉnh biểu tượng xe trong khi chia sẻ hành trình. Biểu tượng điểm đánh dấu không thay đổi theo loại xe thực tế cho chuyến đi.

Tùy chọn điểm đánh dấu

Các thuộc tính có thể tuỳ chỉnh có sẵn cho mỗi điểm đánh dấu là tập hợp con của cơ sở lưu trú do Google Maps cung cấp MarkerOptions. SDK dành cho người tiêu dùng GMTCMarkerStyleOptions có những đặc điểm sau:

  • Được tạo bằng trình khởi tạo
  • Bất biến khi được tạo dựng.
  • Có các giá trị mặc định, nên bạn chỉ cần chỉ định các giá trị tuỳ chỉnh.

Bạn có thể tuỳ chỉnh các thuộc tính sau:

  • groundAnchor
  • isVisible: Để tắt một điểm đánh dấu, hãy đặt isVisible thành false. Đủ dữ liệu sẽ được cung cấp để bạn có thể sử dụng thành phần giao diện người dùng của riêng mình tại vị trí của nó.
  • iconView
  • icon
  • zIndex
  • isFlat

Ví dụ:

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

Thông tin cập nhật linh động về thời gian đến dự kiến cho điểm đánh dấu đến lấy hàng

Để tạo một điểm đánh dấu đến lấy hàng nhằm tự động hiển thị ETA đã cập nhật theo định kỳ, cập nhật tùy chọn kiểu điểm đánh dấu cho GMTCCustomizableMarkerType.tripPickupPoint.

Ví dụ:

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

Các bước tiếp theo