เอกสารนี้ครอบคลุมวิธีปรับแต่งรูปลักษณ์ของเส้นทางสำหรับยานพาหนะที่ติดตามบนแผนที่ ระบบจะวาดเส้นทางบนแผนที่เป็นเส้นประกอบ สำหรับพิกัดแต่ละคู่ในเส้นทางที่ใช้งานอยู่หรือที่เหลือของยานพาหนะ ไลบรารีจะสร้างออบเจ็กต์ 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();