自定义标记

本文档介绍了如何在面向消费者的基于 Web 配送跟踪应用所使用的地图中自定义车辆和位置标记。

借助 JavaScript 消费者 SDK,您可以自定义添加到地图中的两种标记的外观和风格:

您可以通过以下两种方式之一执行此操作:

  • 最简单:指定要应用于同一类型的所有标记的 MarkerOptions 对象。然后,Consumer SDK 会在以下两种情况下应用样式:在将标记添加到地图之前,以及在标记所用的数据发生更改时。
  • 更高级:指定自定义函数。自定义函数可用于根据数据设置标记样式,以及为标记添加互动功能(例如点击处理)。具体而言,Consumer SDK 会向自定义函数传递有关标记所代表的对象类型(车辆或目的地)的数据。这样,标记样式就可以根据标记元素本身的当前状态(例如,距离目的地还有多少个计划停靠点)而变化。您甚至可以联接 Fleet Engine 外部来源的数据,并根据这些信息设置标记样式。

简单示例:使用 MarkerOptions

以下示例展示了如何使用 MarkerOptions 对象配置车辆标记的样式。此示例将标记不透明度设置为 50%。

JavaScript

deliveryVehicleMarkerCustomization = {
  opacity: 0.5
};

TypeScript

deliveryVehicleMarkerCustomization = {
  opacity: 0.5
};

高级示例:使用自定义函数

以下示例展示了如何配置车辆标记的样式,以显示车辆在到达预定任务的停靠点之前剩余的停靠点数量。

JavaScript

deliveryVehicleMarkerCustomization =
  (params) => {
    var stopsLeft = params.taskTrackingInfo.remainingStopCount;
    params.marker.setLabel(`${stopsLeft}`);
  };

TypeScript

deliveryVehicleMarkerCustomization =
  (params: ShipmentMarkerCustomizationFunctionParams) => {
    const stopsLeft = params.taskTrackingInfo.remainingStopCount;
    params.marker.setLabel(`${stopsLeft}`);
  };

为标记添加点击处理

您可以向任何标记添加点击处理,例如以下车辆标记示例。

JavaScript

deliveryVehicleMarkerCustomization =
  (params) => {
    if (params.isNew) {
      params.marker.addListener('click', () => {
        // Perform desired action.
      });
    }
  };

TypeScript

deliveryVehicleMarkerCustomization =
  (params: ShipmentMarkerCustomizationFunctionParams) => {
    if (params.isNew) {
      params.marker.addListener('click', () => {
        // Perform desired action.
      });
    }
  };

显示标记的其他信息

您可以使用 InfoWindow 显示有关车辆或位置标记的其他信息。以下示例创建了一个 InfoWindow 并将其附加到车辆标记:

JavaScript

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

locationProvider.addListener('update', e => {
  const stopsCount =
      e.taskTrackingInfo.remainingStopCount;
  infoWindow.setContent(
      `Your vehicle is ${stopsCount} stops away.`);

  // 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});

locationProvider.addListener('update', (e: google.maps.journeySharing.FleetEngineShipmentLocationProviderUpdateEvent) => {
  const stopsCount =
      e.taskTrackingInfo.remainingStopCount;
  infoWindow.setContent(
      `Your vehicle is ${stopsCount} stops away.`);

  // 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();

后续步骤