ส่วนนี้แสดงวิธีใช้ไลบรารีการติดตามกลุ่มยานพาหนะ JavaScript เพื่อติดตาม ยานพาหนะสำหรับการเดินทางตามคำขอหรืองานที่กำหนดเวลาไว้
หากต้องการติดตามยานพาหนะ ให้ทำตามขั้นตอนต่อไปนี้
- โหลดไลบรารีและเริ่มต้นมุมมองแผนที่
- ระบุตำแหน่งยานพาหนะและมุมมองแผนที่
- ฟังเหตุการณ์และจัดการข้อผิดพลาด
- หยุดติดตาม
โหลดไลบรารีและเริ่มต้นมุมมองแผนที่
หากต้องการแสดงการดำเนินงานของกลุ่มยานพาหนะบนแผนที่ในหน้าเว็บ ให้ใช้สคริปต์ที่เรียกแผนที่โดยใช้คีย์ API ตัวอย่างต่อไปนี้แสดงวิธี ดำเนินการนี้จาก HTML
แหล่งที่มาของ URL: เรียกใช้ Google Maps API เพื่อขอแผนที่โดยใช้คีย์ API
callback
พารามิเตอร์: เรียกใช้ฟังก์ชันinitMap
หลังจากที่ API แสดงผลการเรียกlibraries
พารามิเตอร์: โหลดไลบรารีการติดตามฟลีทแอตทริบิวต์
defer
: ช่วยให้เบราว์เซอร์แสดงผลส่วนที่เหลือของหน้าต่อไปได้ ขณะที่ API โหลด<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=journeySharing" defer></script>
ระบุตำแหน่งรถและมุมมองแผนที่
หากต้องการเริ่มติดตามยานพาหนะ คุณทั้งคู่ต้องสร้างอินสแตนซ์ของผู้ให้บริการตำแหน่งยานพาหนะ และเริ่มต้นมุมมองแผนที่ด้วยรหัสยานพาหนะตามที่อธิบายไว้ในส่วนต่อไปนี้
สร้างอินสแตนซ์ของผู้ให้บริการตำแหน่งรถ
ไลบรารีการติดตามกลุ่มยานพาหนะ JavaScript มีผู้ให้บริการตำแหน่งสำหรับ Fleet Engine API ใช้รหัสโปรเจ็กต์และการอ้างอิงถึงตัวดึงข้อมูลโทเค็น เพื่อสร้างอินสแตนซ์ตามที่แสดงในตัวอย่างต่อไปนี้
การเดินทางแบบออนดีมานด์
JavaScript
locationProvider =
new google.maps.journeySharing
.FleetEngineVehicleLocationProvider({
projectId,
authTokenFetcher,
// Optionally, you may specify
// vehicleId to immediately start
// tracking.
vehicleId: 'your-vehicle-id',
});
TypeScript
locationProvider =
new google.maps.journeySharing
.FleetEngineVehicleLocationProvider({
projectId,
authTokenFetcher,
// Optionally, you may specify
// vehicleId to immediately start
// tracking.
vehicleId: 'your-vehicle-id',
});
งานที่กำหนดเวลาไว้
JavaScript
locationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryVehicleLocationProvider({
projectId,
authTokenFetcher,
// Optionally, you may specify
// deliveryVehicleId to immediately start
// tracking.
deliveryVehicleId: 'your-delivery-id',
});
TypeScript
locationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryVehicleLocationProvider({
projectId,
authTokenFetcher,
// Optionally, you may specify
// deliveryVehicleId to immediately start
// tracking.
deliveryVehicleId: 'your-delivery-id',
});
เริ่มต้นมุมมองแผนที่
หลังจากโหลดไลบรารีการแชร์เส้นทาง JavaScript แล้ว ให้เริ่มต้น มุมมองแผนที่และเพิ่มลงในหน้า HTML หน้าเว็บควรมีองค์ประกอบ <div> ที่มีมุมมองแผนที่ องค์ประกอบ <div> มีชื่อว่า map_canvas ในตัวอย่างต่อไปนี้=
การเดินทางแบบออนดีมานด์
JavaScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.vehicleId
= 'your-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
TypeScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.VehicleId
= 'your-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
งานที่กำหนดเวลาไว้
JavaScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a delivery vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.deliveryVehicleId
= 'your-delivery-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
TypeScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a delivery vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.deliveryVehicleId
= 'your-delivery-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
รอรับเหตุการณ์และจัดการข้อผิดพลาด
เมื่อเริ่มติดตามยานพาหนะแล้ว คุณจะต้องอัปเดตความคืบหน้าบนแผนที่ และจัดการข้อผิดพลาดขณะที่ยานพาหนะเดินทางไปตามเส้นทาง
รอรับเหตุการณ์ยานพาหนะ
หากต้องการติดตามความคืบหน้าของยานพาหนะสำหรับการเดินทางแบบออนดีมานด์หรืองานที่กำหนดเวลาไว้ คุณต้องรอฟังเหตุการณ์การเปลี่ยนแปลง
คุณดึงข้อมูลเมตาจากออบเจ็กต์ vehicle
หรือ deliveryVehicle
โดยใช้
ผู้ให้บริการตำแหน่ง ข้อมูลเมตาประกอบด้วยเวลาที่คาดว่าจะถึงและระยะทางที่เหลือ
ก่อนที่ยานพาหนะจะรับหรือส่งผู้โดยสารรายถัดไป การเปลี่ยนแปลงข้อมูลเมตา
จะทริกเกอร์เหตุการณ์อัปเดตในผู้ให้บริการตำแหน่ง
ตัวอย่างต่อไปนี้แสดงวิธีรับฟังเหตุการณ์การเปลี่ยนแปลงเหล่านี้
การเดินทางแบบออนดีมานด์
JavaScript
locationProvider.addListener('update', e => {
// e.vehicle contains data that may be
// useful to the rest of the UI.
if (e.vehicle) {
console.log(e.vehicle.vehicleState);
}
});
TypeScript
locationProvider.addListener('update',
(e: google.maps.journeySharing.FleetEngineVehicleLocationProviderUpdateEvent) => {
// e.vehicle contains data that may be
// useful to the rest of the UI.
if (e.vehicle) {
console.log(e.vehicle.vehicleState);
}
});
งานที่กำหนดเวลาไว้
JavaScript
locationProvider.addListener('update', e => {
// e.deliveryVehicle contains data that may be
// useful to the rest of the UI.
if (e.deliveryVehicle) {
console.log(e.deliveryVehicle.remainingDuration);
}
});
TypeScript
locationProvider.addListener('update',
(e: google.maps.journeySharing.FleetEngineDeliveryVehicleLocationProviderUpdateEvent) => {
// e.deliveryVehicle contains data that may be
// useful to the rest of the UI.
if (e.deliveryVehicle) {
console.log(e.deliveryVehicle.remainingDuration);
}
});
จัดการข้อผิดพลาด
หลังจากโหลดไลบรารีการแชร์เส้นทาง JavaScript แล้ว ให้เริ่มต้น มุมมองแผนที่และเพิ่มลงในหน้า HTML หน้าเว็บควรมีองค์ประกอบ <div> ที่มีมุมมองแผนที่ องค์ประกอบ <div> มีชื่อว่า map_canvas ในตัวอย่างต่อไปนี้=
การเดินทางแบบออนดีมานด์
JavaScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.vehicleId
= 'your-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
TypeScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.VehicleId
= 'your-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
งานที่กำหนดเวลาไว้
JavaScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a delivery vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.deliveryVehicleId
= 'your-delivery-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
TypeScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
});
// If you did not specify a delivery vehicle ID in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.deliveryVehicleId
= 'your-delivery-vehicle-id';
// Give the map an initial viewport to allow it to
// initialize; otherwise the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they want.
mapView.map.setCenter('Times Square, New York, NY');
mapView.map.setZoom(14);
หยุดติดตามยานพาหนะ
หากต้องการหยุดติดตามยานพาหนะ คุณต้องนำยานพาหนะออกจากผู้ให้บริการตำแหน่งและ นำผู้ให้บริการตำแหน่งออกจากมุมมองแผนที่ตามที่อธิบายไว้ในส่วนต่อไปนี้ ตัวอย่างที่นี่ใช้ได้กับการใช้งานทั้งการเดินทางตามคำขอและงานที่กำหนดเวลาไว้
นำยานพาหนะออกจากผู้ให้บริการตำแหน่ง
หากต้องการหยุดไม่ให้ผู้ให้บริการตำแหน่งติดตามยานพาหนะ ให้นำ รหัสยานพาหนะนำส่งออกจากผู้ให้บริการตำแหน่ง
การเดินทางแบบออนดีมานด์
JavaScript
locationProvider.vehicleId = '';
TypeScript
locationProvider.vehicleId = '';
งานที่กำหนดเวลาไว้
JavaScript
locationProvider.deliveryVehicleId = '';
TypeScript
locationProvider.deliveryVehicleId = '';
นำผู้ให้บริการตำแหน่งออกจากมุมมองแผนที่
ตัวอย่างต่อไปนี้แสดงวิธีนำผู้ให้บริการตำแหน่งออกจากมุมมองแผนที่
JavaScript
mapView.removeLocationProvider(locationProvider);
TypeScript
mapView.removeLocationProvider(locationProvider);