เอกสารนี้ครอบคลุมวิธีปรับแต่งรูปลักษณ์ของเส้นทางสำหรับยานพาหนะที่ติดตามบนแผนที่ ระบบจะวาดเส้นทางบนแผนที่ในรูปแบบเส้นประกอบ สำหรับพิกัดแต่ละคู่ในเส้นทางที่ใช้งานอยู่หรือเส้นทางที่เหลือของยานพาหนะ ไลบรารีจะสร้างออบเจ็กต์google.maps.Polyline
คุณสามารถจัดรูปแบบออบเจ็กต์ Polyline ได้โดยระบุการปรับแต่งเส้นประกอบที่ไลบรารีจะนำไปใช้ใน 2 กรณีต่อไปนี้
- ก่อนเพิ่มออบเจ็กต์ลงในแผนที่
- เมื่อข้อมูลที่ใช้สำหรับออบเจ็กต์มีการเปลี่ยนแปลง
จัดรูปแบบเส้นประกอบ
คุณสามารถจัดรูปแบบเส้นประกอบของเส้นทางได้หลายวิธี ซึ่งคล้ายกับวิธีปรับแต่งเครื่องหมาย
จัดรูปแบบเส้นประกอบของเส้นทางตามประเภท: ใช้
PolylineOptionsเพื่อนำไปใช้กับออบเจ็กต์Polylineที่ตรงกันทั้งหมดเมื่อมีการ สร้างหรืออัปเดต ดูตัวอย่างได้ที่ จัดรูปแบบเส้นประกอบตามประเภทจัดรูปแบบเส้นประกอบของเส้นทางตามข้อมูล: ระบุฟังก์ชันการปรับแต่ง ตามข้อมูลจากการติดตามยานพาหนะในกองยาน หรือจากแหล่งที่มาภายนอก:
ข้อมูลจากการติดตามยานพาหนะในกองยาน: การติดตามยานพาหนะในกองยานจะส่งข้อมูลเส้นประกอบไปยัง ฟังก์ชันการปรับแต่ง ซึ่งรวมถึงข้อมูลเกี่ยวกับสถานะปัจจุบันของยานพาหนะ คุณสามารถจัดรูปแบบเส้นประกอบตามข้อมูลนี้ได้ เช่น การระบายสีออบเจ็กต์
Polylineให้เข้มขึ้น หรือทำให้หนาขึ้นเมื่อยานพาหนะเคลื่อนที่ช้าลงแหล่งที่มาภายนอก: คุณสามารถรวมข้อมูลการติดตามยานพาหนะในกองยานกับข้อมูลจาก แหล่งที่มาภายนอก Fleet Engine และจัดรูปแบบออบเจ็กต์
Polylineตามข้อมูลดังกล่าวได้
ดูตัวอย่างได้ที่ จัดรูปแบบเส้นประกอบตามข้อมูล
ควบคุมระดับการมองเห็นเส้นประกอบของเส้นทาง: คุณสามารถซ่อนหรือแสดง เส้นประกอบได้โดยใช้พร็อพเพอร์ตี้
visibleดูรายละเอียดได้ที่ ควบคุมระดับการมองเห็นเส้นประกอบในคู่มือนี้แสดงข้อมูลเพิ่มเติมสำหรับเครื่องหมายยานพาหนะหรือเครื่องหมายระบุตำแหน่ง: คุณสามารถแสดงข้อมูลเพิ่มเติมได้โดยใช้พร็อพเพอร์ตี้
infowindowดูรายละเอียดได้ที่ แสดงข้อมูลเพิ่มเติมสำหรับเครื่องหมายยานพาหนะหรือเครื่องหมายระบุตำแหน่งใน คู่มือนี้
ตัวเลือกการปรับแต่งเส้นประกอบ
ตัวเลือกการปรับแต่งต่อไปนี้มีให้ใช้งานทั้งใน
FleetEngineVehicleLocationProviderOptions
และ
FleetEngineDeliveryVehicleLocationProviderOptions
คุณสามารถตั้งค่าการปรับแต่งสำหรับสถานะเส้นทางต่างๆ ในการเดินทางของยานพาหนะได้ดังนี้
เส้นทางที่เดินทางไปแล้ว: ใช้
takenPolylineCustomization- การเดินทางแบบออนดีมานด์, ข้อมูลอ้างอิงงานที่กำหนดเวลาไว้เส้นทางที่กำลังเดินทางอยู่: ใช้
activePolylineCustomization- การเดินทางแบบออนดีมานด์, ข้อมูลอ้างอิงงานที่กำหนดเวลาไว้เส้นทางที่ยังไม่ได้เดินทาง: ใช้
remainingPolylineCustomization- การเดินทางแบบออนดีมานด์, ข้อมูลอ้างอิงงานที่กำหนดเวลาไว้
จัดรูปแบบเส้นประกอบของเส้นทางตามประเภท
หากต้องการจัดรูปแบบเส้นประกอบของเส้นทางตามประเภท ให้เปลี่ยนการจัดรูปแบบออบเจ็กต์ Polyline โดยใช้ PolylineOptions
ตัวอย่างต่อไปนี้แสดงวิธีกำหนดค่าการจัดรูปแบบสำหรับออบเจ็กต์ Polyline
ด้วย PolylineOptions ทำตามรูปแบบนี้เพื่อปรับแต่งการจัดรูปแบบออบเจ็กต์
Polyline โดยใช้การปรับแต่งเส้นประกอบของเส้นทางใดก็ได้ที่ระบุไว้ใน
ตัวเลือกการปรับแต่งเส้นประกอบในคู่มือนี้
ตัวอย่างสำหรับการเดินทางแบบออนดีมานด์หรืองานที่กำหนดเวลาไว้
JavaScript
activePolylineCustomization = {
strokeWidth: 5,
strokeColor: 'black',
};
TypeScript
activePolylineCustomization = {
strokeWidth: 5,
strokeColor: 'black',
};
จัดรูปแบบเส้นประกอบของเส้นทางโดยใช้ข้อมูล
หากต้องการจัดรูปแบบเส้นประกอบของเส้นทางโดยใช้ข้อมูล ให้เปลี่ยนการจัดรูปแบบออบเจ็กต์ Polyline โดยใช้ฟังก์ชันการปรับแต่ง
ตัวอย่างต่อไปนี้แสดงวิธีกำหนดค่าการจัดรูปแบบสำหรับเส้นทางที่ใช้งานอยู่โดยใช้ฟังก์ชันการปรับแต่ง ทำตามรูปแบบนี้เพื่อปรับแต่งการจัดรูปแบบของ
ออบเจ็กต์ Polyline โดยใช้พารามิเตอร์การปรับแต่งเส้นประกอบใดก็ได้ที่ระบุไว้
ใน ตัวเลือกการปรับแต่งเส้นประกอบ ในคู่มือนี้
ตัวอย่างการเดินทางแบบออนดีมานด์
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'});
}
}
};
ตัวอย่างงานที่กำหนดเวลาไว้
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'});
}
}
};
ตัวอย่างการจัดรูปแบบที่คำนึงถึงการจราจร (การเดินทางแบบออนดีมานด์เท่านั้น)
Fleet Engine จะแสดงผลข้อมูลความเร็วการจราจรสำหรับเส้นทางที่ใช้งานอยู่และเส้นทางที่เหลือของยานพาหนะที่ติดตาม คุณสามารถใช้ข้อมูลนี้เพื่อจัดรูปแบบออบเจ็กต์ Polyline ตามความเร็วการจราจรได้ดังนี้
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'});
}
}
};
ควบคุมระดับการมองเห็นเส้นประกอบ
ออบเจ็กต์ Polyline ทั้งหมดจะมองเห็นได้โดยค่าเริ่มต้น หากต้องการทำให้ออบเจ็กต์ Polyline มองไม่เห็น
ให้ตั้งค่าพร็อพเพอร์ตี้ visible เป็น false
ตัวอย่างสำหรับการเดินทางแบบออนดีมานด์หรืองานที่กำหนดเวลาไว้
JavaScript
remainingPolylineCustomization = {visible: false};
TypeScript
remainingPolylineCustomization = {visible: false};
แสดงหน้าต่างข้อมูลสำหรับเครื่องหมายยานพาหนะหรือเครื่องหมายระบุตำแหน่ง
คุณสามารถใช้ InfoWindow เพื่อแสดงข้อมูลเพิ่มเติมเกี่ยวกับเครื่องหมายยานพาหนะหรือเครื่องหมายระบุตำแหน่ง
ตัวอย่างต่อไปนี้แสดงวิธีสร้าง InfoWindow และแนบกับตัวทำเครื่องหมายยานพาหนะ
ตัวอย่างการเดินทางแบบออนดีมานด์
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();
ตัวอย่างงานที่กำหนดเวลาไว้
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();