自定义标记
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档介绍了如何在您用于面向消费者的基于网络的货件跟踪应用的地图中自定义车辆和位置标记。
借助 JavaScript Consumer 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();
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eCustomize the appearance of delivery vehicle and destination markers on your shipment tracking map using the JavaScript Consumer SDK.\u003c/p\u003e\n"],["\u003cp\u003eAchieve basic styling by applying \u003ccode\u003eMarkerOptions\u003c/code\u003e to define marker attributes like opacity.\u003c/p\u003e\n"],["\u003cp\u003eImplement dynamic styling and interactivity by using customization functions that adapt marker appearance based on real-time data like remaining stops.\u003c/p\u003e\n"],["\u003cp\u003eEnhance marker interaction with click handling for triggering actions when users click on them.\u003c/p\u003e\n"],["\u003cp\u003eDisplay extra information with \u003ccode\u003eInfoWindow\u003c/code\u003e objects to provide users with context about a specific marker, such as remaining stops for a delivery vehicle.\u003c/p\u003e\n"]]],[],null,["This document covers how to customize vehicle and location markers in the map\nyou use for your web-based shipment tracking app for consumers.\n\nWith the JavaScript Consumer SDK, you can customize the look and feel of two\ntypes of markers added to the map:\n\n- **Delivery vehicle markers** : use [`deliveryVehicleMarkerCustomization`](/maps/documentation/javascript/reference/journey-sharing-shipment-tracking#FleetEngineShipmentLocationProviderOptions.deliveryVehicleMarkerCustomization)\n- **Destination markers** : use [`destinationMarkerCustomization`](/maps/documentation/javascript/reference/journey-sharing-shipment-tracking#FleetEngineShipmentLocationProviderOptions.destinationMarkerCustomization)\n\nYou do this in one of two ways:\n\n- **Simplest** : Specify a [`MarkerOptions`](/maps/documentation/javascript/reference/marker#MarkerOptions) object to apply to all markers of the same type. The Consumer SDK then applies the styling in two situations: before adding the markers to the map, and when the data used for the markers have changed.\n- **More advanced**: Specify a customization function. Customization functions allow for styling of the markers based on data, as well as adding interactivity to markers, such as click handling. Specifically, the Consumer SDK passes data to the customization function about the type of object the marker represents: vehicle or destination. This then allows marker styling to change based on the current state of the marker element itself; for example, the number of planned stops remaining until the destination. You can even join against data from sources outside Fleet Engine and style the marker based on that information.\n\nSimple example: use `MarkerOptions`\n\nThe following example shows how to configure a vehicle marker's styling with a\n`MarkerOptions` object. This example sets the marker opacity to 50%.\n\n\u003cbr /\u003e\n\nJavaScript\n\n\u003cbr /\u003e\n\n deliveryVehicleMarkerCustomization = {\n opacity: 0.5\n };\n\n\u003cbr /\u003e\n\nTypeScript\n\n\u003cbr /\u003e\n\n deliveryVehicleMarkerCustomization = {\n opacity: 0.5\n };\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nAdvanced example: use a customization function\n\nThe following example shows how to configure a vehicle marker's styling to\ndisplay the remaining stop count for the vehicle before reaching the stop for\nthe scheduled task.\n\n\u003cbr /\u003e\n\nJavaScript\n\n\u003cbr /\u003e\n\n deliveryVehicleMarkerCustomization =\n (params) =\u003e {\n var stopsLeft = params.taskTrackingInfo.remainingStopCount;\n params.marker.setLabel(`${stopsLeft}`);\n };\n\n\u003cbr /\u003e\n\nTypeScript\n\n\u003cbr /\u003e\n\n deliveryVehicleMarkerCustomization =\n (params: ShipmentMarkerCustomizationFunctionParams) =\u003e {\n const stopsLeft = params.taskTrackingInfo.remainingStopCount;\n params.marker.setLabel(`${stopsLeft}`);\n };\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nAdd click handling to markers\n\nYou can add click handling to any marker, such as in the following example\nfor a vehicle marker.\n\n\u003cbr /\u003e\n\nJavaScript\n\n\u003cbr /\u003e\n\n deliveryVehicleMarkerCustomization =\n (params) =\u003e {\n if (params.isNew) {\n params.marker.addListener('click', () =\u003e {\n // Perform desired action.\n });\n }\n };\n\n\u003cbr /\u003e\n\nTypeScript\n\n\u003cbr /\u003e\n\n deliveryVehicleMarkerCustomization =\n (params: ShipmentMarkerCustomizationFunctionParams) =\u003e {\n if (params.isNew) {\n params.marker.addListener('click', () =\u003e {\n // Perform desired action.\n });\n }\n };\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nDisplay additional information for markers\n\nYou can use an [`InfoWindow`](/maps/documentation/javascript/infowindows) to display additional information about a\nvehicle or location marker. The following example creates an\n`InfoWindow` and attaches it to a vehicle marker:\n\n\u003cbr /\u003e\n\nJavaScript\n\n\u003cbr /\u003e\n\n // 1. Create an info window.\n const infoWindow = new google.maps.InfoWindow(\n {disableAutoPan: true});\n\n locationProvider.addListener('update', e =\u003e {\n const stopsCount =\n e.taskTrackingInfo.remainingStopCount;\n infoWindow.setContent(\n `Your vehicle is ${stopsCount} stops away.`);\n\n // 2. Attach the info window to a vehicle marker.\n // This property can return multiple markers.\n const marker = mapView.vehicleMarkers[0];\n infoWindow.open(mapView.map, marker);\n });\n\n // 3. Close the info window.\n infoWindow.close();\n\n\u003cbr /\u003e\n\nTypeScript\n\n\u003cbr /\u003e\n\n // 1. Create an info window.\n const infoWindow = new google.maps.InfoWindow(\n {disableAutoPan: true});\n\n locationProvider.addListener('update', (e: google.maps.journeySharing.FleetEngineShipmentLocationProviderUpdateEvent) =\u003e {\n const stopsCount =\n e.taskTrackingInfo.remainingStopCount;\n infoWindow.setContent(\n `Your vehicle is ${stopsCount} stops away.`);\n\n // 2. Attach the info window to a vehicle marker.\n // This property can return multiple markers.\n const marker = mapView.vehicleMarkers[0];\n infoWindow.open(mapView.map, marker);\n });\n\n // 3. Close the info window.\n infoWindow.close();\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nWhat's next\n\n- [Customize polylines](/maps/documentation/mobility/journey-sharing/scheduled/shipment-tracking/customize-polylines)"]]