Tuỳ chỉnh hình nhiều đường

Tài liệu này trình bày cách tuỳ chỉnh giao diện của các tuyến đường cho xe được theo dõi trên bản đồ. Các tuyến đường được vẽ trên bản đồ dưới dạng đường nhiều đoạn. Đối với mỗi cặp toạ độ trong đường đi hiện tại hoặc đường đi còn lại của một chiếc xe, thư viện sẽ tạo một đối tượng google.maps.Polyline. Bạn có thể tạo kiểu cho các đối tượng Polyline bằng cách chỉ định các chế độ tuỳ chỉnh đường nhiều đoạn mà thư viện sẽ áp dụng trong 2 trường hợp:

  • Trước khi thêm các đối tượng vào bản đồ
  • Khi dữ liệu dùng cho các đối tượng đã thay đổi

Tạo kiểu cho đường nhiều đoạn

Tương tự như cách bạn có thể tuỳ chỉnh điểm đánh dấu, bạn có thể tạo kiểu cho đường nhiều đoạn của tuyến đường theo nhiều cách:

  1. Tạo kiểu cho đường nhiều đoạn của tuyến đường theo loại: Sử dụng PolylineOptions để áp dụng cho tất cả các đối tượng Polyline được so khớp khi các đối tượng này được tạo hoặc cập nhật. Để xem ví dụ, hãy xem phần Tạo kiểu cho đường nhiều đoạn theo loại.

  2. Tạo kiểu cho đường nhiều đoạn của tuyến đường dựa trên dữ liệu: Chỉ định một hàm tuỳ chỉnh dựa trên dữ liệu từ tính năng theo dõi đội xe hoặc từ các nguồn bên ngoài:

    • Dữ liệu từ tính năng theo dõi đội xe: Tính năng theo dõi đội xe sẽ truyền dữ liệu hình nhiều đường đến hàm tuỳ chỉnh, bao gồm cả dữ liệu về trạng thái hiện tại của xe. Bạn có thể tạo kiểu cho hình nhiều đường dựa trên dữ liệu này, bao gồm cả việc tô màu đối tượng Polyline thành màu đậm hơn hoặc làm cho đối tượng này dày hơn khi xe di chuyển chậm hơn.

    • Nguồn bên ngoài: Bạn có thể kết hợp dữ liệu theo dõi đội xe với dữ liệu từ các nguồn bên ngoài Fleet Engine và tạo kiểu cho đối tượng Polyline dựa trên thông tin đó.

    Để xem ví dụ, hãy xem phần Tạo kiểu cho đường nhiều đoạn dựa trên dữ liệu.

  3. Kiểm soát chế độ hiển thị của hình nhiều đường của tuyến đường: Bạn có thể ẩn hoặc hiện hình nhiều đường bằng thuộc tính visible. Để biết thông tin chi tiết, hãy xem phần Kiểm soát chế độ hiển thị của đường nhiều đoạn trong hướng dẫn này.

  4. Hiển thị thông tin bổ sung cho điểm đánh dấu xe hoặc điểm đánh dấu vị trí: Bạn có thể hiển thị thông tin bổ sung bằng thuộc tính infowindow. Để biết thông tin chi tiết, hãy xem Hiển thị thông tin bổ sung cho điểm đánh dấu xe hoặc vị trí trong hướng dẫn này.

Các lựa chọn tuỳ chỉnh đường nhiều đoạn

Bạn có thể sử dụng các lựa chọn tuỳ chỉnh sau đây trong cả FleetEngineVehicleLocationProviderOptionsFleetEngineDeliveryVehicleLocationProviderOptions. Bạn có thể đặt các chế độ tuỳ chỉnh cho các trạng thái đường đi khác nhau trong hành trình của xe:

Tạo kiểu cho đường nhiều đoạn của tuyến đường theo loại

Để tạo kiểu cho đường nhiều đoạn của tuyến đường theo loại, hãy thay đổi kiểu của các đối tượng Polyline bằng PolylineOptions.

Ví dụ sau đây cho thấy cách định cấu hình kiểu cho đối tượng Polyline bằng PolylineOptions. Hãy làm theo mẫu này để tuỳ chỉnh định kiểu của bất kỳ Polyline đối tượng nào bằng cách sử dụng bất kỳ chế độ tuỳ chỉnh đường nhiều đoạn của tuyến đường nào được liệt kê trong Các lựa chọn tuỳ chỉnh đường nhiều đoạn trong hướng dẫn này.

Ví dụ về chuyến đi theo yêu cầu hoặc nhiệm vụ đã lên lịch

JavaScript

activePolylineCustomization = {
  strokeWidth: 5,
  strokeColor: 'black',
};

TypeScript

activePolylineCustomization = {
  strokeWidth: 5,
  strokeColor: 'black',
};

Tạo kiểu cho đường nhiều đoạn của tuyến đường bằng dữ liệu

Để tạo kiểu cho đường nhiều đoạn của tuyến đường bằng dữ liệu, hãy thay đổi kiểu của các đối tượng Polyline bằng các hàm tuỳ chỉnh.

Ví dụ sau đây cho thấy cách định cấu hình kiểu cho một tuyến đường đang hoạt động bằng hàm tuỳ chỉnh. Hãy làm theo mẫu này để tuỳ chỉnh định kiểu của bất kỳ đối tượng Polyline nào bằng cách sử dụng bất kỳ tham số tuỳ chỉnh đường nhiều đoạn nào được liệt kê trong Các lựa chọn tuỳ chỉnh đường nhiều đoạn trong hướng dẫn này.

Ví dụ về chuyến đi theo yêu cầu

JavaScript

// Color the Polyline objects in green if the vehicle is nearby.
activePolylineCustomization =
  (params) => {
    const distance = params.vehicle.waypoints[0].distanceMeters;
    if (distance < 1000) {

      // params.polylines contains an ordered list of Polyline objects for
      // the path.
      for (const polylineObject of params.polylines) {
        polylineObject.setOptions({strokeColor: 'green'});
      }
    }
  };

TypeScript

// Color the Polyline objects in green if the vehicle is nearby.
activePolylineCustomization =
  (params: VehiclePolylineCustomizationFunctionParams) => {
    const distance = params.vehicle.waypoints[0].distanceMeters;
    if (distance < 1000) {

      // params.polylines contains an ordered list of Polyline objects for
      // the path.
      for (const polylineObject of params.polylines) {
        polylineObject.setOptions({strokeColor: 'green'});
      }
    }
  };

Ví dụ về nhiệm vụ đã lên lịch

JavaScript

// Color the Polyline objects in green if the vehicle is nearby.
activePolylineCustomization =
  (params) => {
    const distance = params.deliveryVehicle.remainingDistanceMeters;
    if (distance < 1000) {

      // params.polylines contains an ordered list of Polyline objects for
      // the path.
      for (const polylineObject of params.polylines) {
        polylineObject.setOptions({strokeColor: 'green'});
      }
    }
  };

TypeScript

// Color the Polyline objects in green if the vehicle is nearby.
activePolylineCustomization =
  (params: DeliveryVehiclePolylineCustomizationFunctionParams) => {
    const distance = params.deliveryVehicle.remainingDistanceMeters;
    if (distance < 1000) {

      // params.polylines contains an ordered list of Polyline objects for
      // the path.
      for (const polylineObject of params.polylines) {
        polylineObject.setOptions({strokeColor: 'green'});
      }
    }
  };

Ví dụ về kiểu nhận biết lưu lượng truy cập (chỉ dành cho chuyến đi theo yêu cầu)

Fleet Engine trả về dữ liệu tốc độ lưu lượng truy cập cho đường đi hiện tại và đường đi còn lại của xe được theo dõi. Bạn có thể sử dụng thông tin này để tạo kiểu cho các đối tượng Polyline theo tốc độ lưu lượng truy cập của các đối tượng đó:

JavaScript

// Color the Polyline objects according to their real-time traffic levels
// using '#05f' for normal, '#fa0' for slow, and '#f33' for traffic jam.
activePolylineCustomization =
  FleetEngineVehicleLocationProvider.
      TRAFFIC_AWARE_ACTIVE_POLYLINE_CUSTOMIZATION_FUNCTION;

// Or alter the objects further after the customization function has been
// run -- in this example, change the blue for normal to green:
activePolylineCustomization =
  (params) => {
    FleetEngineVehicleLocationProvider.
        TRAFFIC_AWARE_ACTIVE_POLYLINE_CUSTOMIZATION_FUNCTION(params);
    for (const polylineObject of params.polylines) {
      if (polylineObject.get('strokeColor') === '#05f') {
        polylineObject.setOptions({strokeColor: 'green'});
      }
    }
  };

TypeScript

// Color the Polyline objects according to their real-time traffic levels
// using '#05f' for normal, '#fa0' for slow, and '#f33' for traffic jam.
activePolylineCustomization =
  FleetEngineVehicleLocationProvider.
      TRAFFIC_AWARE_ACTIVE_POLYLINE_CUSTOMIZATION_FUNCTION;

// Or alter the objects further after the customization function has been
// run -- in this example, change the blue for normal to green:
activePolylineCustomization =
  (params: VehiclePolylineCustomizationFunctionParams) => {
    FleetEngineVehicleLocationProvider.
        TRAFFIC_AWARE_ACTIVE_POLYLINE_CUSTOMIZATION_FUNCTION(params);
    for (const polylineObject of params.polylines) {
      if (polylineObject.get('strokeColor') === '#05f') {
        polylineObject.setOptions({strokeColor: 'green'});
      }
    }
  };

Kiểm soát chế độ hiển thị của đường nhiều đoạn

Theo mặc định, tất cả các đối tượng Polyline đều hiển thị. Để ẩn đối tượng Polyline, hãy đặt thuộc tính visible của đối tượng đó thành false.

Ví dụ về chuyến đi theo yêu cầu hoặc nhiệm vụ đã lên lịch

JavaScript

remainingPolylineCustomization = {visible: false};

TypeScript

remainingPolylineCustomization = {visible: false};

Hiển thị cửa sổ thông tin cho điểm đánh dấu xe hoặc điểm đánh dấu vị trí

Bạn có thể sử dụng InfoWindow để hiển thị thông tin bổ sung về điểm đánh dấu xe hoặc điểm đánh dấu vị trí.

Ví dụ sau đây cho thấy cách tạo InfoWindow và đính kèm vào điểm đánh dấu xe.

Ví dụ về chuyến đi theo yêu cầu

JavaScript

// 1. Create an info window.
const infoWindow = new google.maps.InfoWindow(
    {disableAutoPan: true});

// (Assumes a vehicle location provider.)
locationProvider.addListener('update', e => {
  if (e.vehicle) {
    const distance =
          e.vehicle.remainingDistanceMeters;
    infoWindow.setContent(
        `Your vehicle is ${distance}m away from the next drop-off point.`);

    // 2. Attach the info window to a vehicle marker.
    // This property can return multiple markers.
    const marker = mapView.vehicleMarkers[0];
    infoWindow.open(mapView.map, marker);
  }
});

// 3. Close the info window.
infoWindow.close();

TypeScript

// 1. Create an info window.
const infoWindow = new google.maps.InfoWindow(
    {disableAutoPan: true});

// (Assumes a vehicle location provider.)
locationProvider.addListener('update', (e: google.maps.journeySharing.FleetEngineVehicleLocationProviderUpdateEvent) => {
  if (e.vehicle) {
    const distance =
          e.vehicle.remainingDistanceMeters;
    infoWindow.setContent(
        `Your vehicle is ${distance}m away from the next drop-off.`);
    // 2. Attach the info window to a vehicle marker.
    // This property can return multiple markers.
    const marker = mapView.vehicleMarkers[0];
    infoWindow.open(mapView.map, marker);
  }
});

// 3. Close the info window.
infoWindow.close();

Ví dụ về nhiệm vụ đã lên lịch

JavaScript

// 1. Create an info window.
const infoWindow = new google.maps.InfoWindow(
    {disableAutoPan: true});

// (Assumes a delivery vehicle location provider.)
locationProvider.addListener('update', e => {
  if (e.deliveryVehicle) {
    const distance =
           e.deliveryVehicle.remainingDistanceMeters;
    infoWindow.setContent(
        `Your vehicle is ${distance}m away from the next task.`);

    // 2. Attach the info window to a vehicle marker.
    // This property can return multiple markers.
    const marker = mapView.vehicleMarkers[0];
    infoWindow.open(mapView.map, marker);
  }
});

// 3. Close the info window.
infoWindow.close();

TypeScript

// 1. Create an info window.
const infoWindow = new google.maps.InfoWindow(
    {disableAutoPan: true});

// (Assumes a delivery vehicle location provider.)
locationProvider.addListener('update', (e: google.maps.journeySharing.FleetEngineDeliveryVehicleLocationProviderUpdateEvent) => {
  if (e.deliveryVehicle) {
    const distance =
           e.deliveryVehicle.remainingDistanceMeters;
    infoWindow.setContent(
        `Your vehicle is ${distance}m away from the next task.`);

    // 2. Attach the info window to a vehicle marker.
    // This property can return multiple markers.
    const marker = mapView.vehicleMarkers[0];
    infoWindow.open(mapView.map, marker);
  }
});

// 3. Close the info window.
infoWindow.close();

Bước tiếp theo