ส่วนนี้แสดงวิธีใช้ไลบรารีการติดตามยานพาหนะของ JavaScript เพื่อดูยานพาหนะ ขั้นตอนเหล่านี้จะถือว่าคุณได้ตั้งค่าไลบรารีการติดตามยานพาหนะและโหลดแผนที่แล้ว ดูรายละเอียดได้ที่ หัวข้อตั้งค่าไลบรารีการติดตามยานพาหนะของ JavaScript
เอกสารนี้จะพูดถึงสิ่งต่อไปนี้ที่คุณทำได้เมื่อดูยานพาหนะ
เริ่มติดตามยานพาหนะ
หากต้องการติดตามยานพาหนะ คุณต้องสร้างอินสแตนซ์ของผู้ให้บริการตำแหน่งยานพาหนะและตั้งค่าข้อจำกัดด้านตำแหน่งสำหรับวิวพอร์ตของแผนที่ตามที่อธิบายไว้ในส่วนต่อไปนี้
สร้างอินสแตนซ์ของผู้ให้บริการตำแหน่งยานพาหนะ
ไลบรารีการติดตามยานพาหนะของ JavaScript มีผู้ให้บริการตำแหน่งที่ดึงยานพาหนะหลายคันจาก Fleet Engine
หากต้องการสร้างอินสแตนซ์ ให้ทำตามขั้นตอนต่อไปนี้
ใช้รหัสโปรเจ็กต์ รวมถึงการอ้างอิงตัวดึงข้อมูลโทเค็น
ใช้คําค้นหาตัวกรองยานพาหนะ คําค้นหาตัวกรองยานพาหนะจะควบคุมยานพาหนะที่แผนที่แสดง ระบบจะส่งตัวกรองไปยัง Fleet Engine
- สําหรับบริการแบบออนดีมานด์ ให้ใช้
vehicleFilterและตรวจสอบListVehiclesRequest.filter - สําหรับงานที่กําหนดเวลาไว้ ให้ใช้
deliveryVehicleFilterและตรวจสอบListDeliveryVehiclesRequest.filter
- สําหรับบริการแบบออนดีมานด์ ให้ใช้
ตั้งค่าขอบเขตการแสดงยานพาหนะ ใช้
locationRestrictionเพื่อจํากัดพื้นที่ที่จะแสดงยานพาหนะบนแผนที่ ผู้ให้บริการตำแหน่งจะไม่ทำงานจนกว่าจะมีการตั้งค่านี้ คุณสามารถตั้งค่าขอบเขตตำแหน่งได้ทั้งในเครื่องมือสร้างหรือหลังจากเครื่องมือสร้างเริ่มต้นมุมมองแผนที่
ตัวอย่างต่อไปนี้แสดงวิธีสร้างอินสแตนซ์ของผู้ให้บริการตำแหน่งยานพาหนะสําหรับสถานการณ์ทั้งแบบออนดีมานด์และงานที่กําหนดเวลาไว้ นอกจากนี้ ตัวอย่างเหล่านี้ยังแสดงวิธีใช้ locationRestriction ในเครื่องมือสร้างเพื่อให้ผู้ให้บริการตำแหน่งทำงาน
การเดินทางแบบออนดีมานด์
JavaScript
locationProvider =
new google.maps.journeySharing
.FleetEngineFleetLocationProvider({
projectId,
authTokenFetcher,
// Optionally, specify location bounds to
// limit which vehicles are
// retrieved and immediately start tracking.
locationRestriction: {
north: 37.3,
east: -121.8,
south: 37.1,
west: -122,
},
// Optionally, specify a filter to limit
// which vehicles are retrieved.
vehicleFilter:
'attributes.foo = "bar" AND attributes.baz = "qux"',
});
TypeScript
locationProvider =
new google.maps.journeySharing
.FleetEngineFleetLocationProvider({
projectId,
authTokenFetcher,
// Optionally, specify location bounds to
// limit which vehicles are
// retrieved and immediately start tracking.
locationRestriction: {
north: 37.3,
east: -121.8,
south: 37.1,
west: -122,
},
// Optionally, specify a filter to limit
// which vehicles are retrieved.
vehicleFilter:
'attributes.foo = "bar" AND attributes.baz = "qux"',
});
งานที่กําหนดเวลาไว้
JavaScript
locationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryFleetLocationProvider({
projectId,
authTokenFetcher,
// Optionally, specify location bounds to
// limit which delivery vehicles are
// retrieved and immediately make the location provider active.
locationRestriction: {
north: 37.3,
east: -121.8,
south: 37.1,
west: -122,
},
// Optionally, specify a filter to limit
// which delivery vehicles are retrieved.
deliveryVehicleFilter:
'attributes.foo = "bar" AND attributes.baz = "qux"',
});
TypeScript
locationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryFleetLocationProvider({
projectId,
authTokenFetcher,
// Optionally, specify location bounds to
// limit which delivery vehicles are
// retrieved and immediately make the location provider active.
locationRestriction: {
north: 37.3,
east: -121.8,
south: 37.1,
west: -122,
},
// Optionally, specify a filter to limit
// which delivery vehicles are retrieved.
deliveryVehicleFilter:
'attributes.foo = "bar" AND attributes.baz = "qux"',
});
หากต้องการตั้งค่า locationRestriction หลังจากเครื่องมือสร้าง ให้เพิ่ม
locationProvider.locationRestriction ในโค้ดภายหลังตามที่
แสดงในตัวอย่าง JavaScript ต่อไปนี้
// You can choose to not set locationRestriction in the constructor.
// In this case, the location provider *DOES NOT START* after this line, because
// no locationRestriction is set.
locationProvider = new google.maps.journeySharing.DeliveryFleetLocationProvider({
... // not setting locationRestriction here
});
// You can then set the locationRestriction *after* the constructor.
// After this line, the location provider is active.
locationProvider.locationRestriction = {
north: 1,
east: 2,
south: 0,
west: 1,
};
ตั้งค่าข้อจํากัดด้านตำแหน่งโดยใช้วิวพอร์ตของแผนที่
นอกจากนี้ คุณยังตั้งค่าขอบเขต locationRestriction ให้ตรงกับพื้นที่ที่มองเห็นได้ในมุมมองแผนที่ในปัจจุบันได้ด้วย
การเดินทางแบบออนดีมานด์
JavaScript
google.maps.event.addListenerOnce(
mapView.map, 'bounds_changed', () => {
const bounds = mapView.map.getBounds();
if (bounds) {
// If you did not specify a location restriction in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.locationRestriction = bounds;
}
});
TypeScript
google.maps.event.addListenerOnce(
mapView.map, 'bounds_changed', () => {
const bounds = mapView.map.getBounds();
if (bounds) {
// If you did not specify a location restriction in the
// location provider constructor, you may do so here.
// Location tracking will start as soon as this is set.
locationProvider.locationRestriction = bounds;
}
});
งานที่กําหนดเวลาไว้
JavaScript
google.maps.event.addListenerOnce(
mapView.map, 'bounds_changed', () => {
const bounds = mapView.map.getBounds();
if (bounds) {
// If you did not specify a location restriction in the
// location provider constructor, you may do so here.
// Location provider will start as soon as this is set.
locationProvider.locationRestriction = bounds;
}
});
TypeScript
google.maps.event.addListenerOnce(
mapView.map, 'bounds_changed', () => {
const bounds = mapView.map.getBounds();
if (bounds) {
// If you did not specify a location restriction in the
// location provider constructor, you may do so here.
// Location provider will start as soon as this is set.
locationProvider.locationRestriction = bounds;
}
});
เริ่มต้นมุมมองแผนที่
เมื่อสร้างผู้ให้บริการตำแหน่งแล้ว ให้เริ่มต้นมุมมองแผนที่ด้วยวิธีเดียวกับที่คุณทำเมื่อติดตามยานพาหนะคันเดียว
หลังจากโหลดไลบรารีการแชร์การเดินทางของ 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);
ฟังเหตุการณ์และจัดการข้อผิดพลาด
เมื่อเริ่มติดตามยานพาหนะแล้ว คุณต้องฟังการเปลี่ยนแปลงของเหตุการณ์และจัดการข้อผิดพลาดที่เกิดขึ้นตามที่อธิบายไว้ในส่วนต่อไปนี้
ฟังเหตุการณ์การเปลี่ยนแปลง
คุณสามารถดึงข้อมูลเมตาเกี่ยวกับยานพาหนะจากออบเจ็กต์ยานพาหนะโดยใช้ผู้ให้บริการตำแหน่ง การเปลี่ยนแปลงข้อมูลเมตาจะทริกเกอร์เหตุการณ์ update ข้อมูลเมตาประกอบด้วยพร็อพเพอร์ตี้ยานพาหนะ เช่น สถานะการนำทาง ระยะทางที่เหลือ และแอตทริบิวต์ที่กำหนดเอง
ดูรายละเอียดได้ที่หัวข้อต่อไปนี้
- ข้อมูลอ้างอิงตัวเลือกยานพาหนะสำหรับการเดินทางแบบออนดีมานด์
- ข้อมูลอ้างอิงตัวเลือกยานพาหนะสำหรับงานที่กำหนดเวลาไว้
ตัวอย่างต่อไปนี้แสดงวิธีฟังเหตุการณ์การเปลี่ยนแปลงเหล่านี้
การเดินทางแบบออนดีมานด์
JavaScript
locationProvider.addListener('update', e => {
// e.vehicles contains data that may be
// useful to the rest of the UI.
for (vehicle of e.vehicles) {
console.log(vehicle.navigationStatus);
}
});
TypeScript
locationProvider.addListener('update',
(e: google.maps.journeySharing.FleetEngineFleetLocationProviderUpdateEvent) => {
// e.vehicles contains data that may be
// useful to the rest of the UI.
if (e.vehicles) {
for (vehicle of e.vehicles) {
console.log(vehicle.navigationStatus);
}
}
});
งานที่กําหนดเวลาไว้
JavaScript
locationProvider.addListener('update', e => {
// e.deliveryVehicles contains data that may be
// useful to the rest of the UI.
if (e.deliveryVehicles) {
for (vehicle of e.deliveryVehicles) {
console.log(vehicle.remainingDistanceMeters);
}
}
});
TypeScript
locationProvider.addListener('update',
(e: google.maps.journeySharing.FleetEngineDeliveryFleetLocationProviderUpdateEvent) => {
// e.deliveryVehicles contains data that may be
// useful to the rest of the UI.
if (e.deliveryVehicles) {
for (vehicle of e.deliveryVehicles) {
console.log(vehicle.remainingDistanceMeters);
}
}
});
ฟังข้อผิดพลาด
คุณยังต้องฟังและจัดการข้อผิดพลาดที่เกิดขึ้นขณะติดตามยานพาหนะด้วย ข้อผิดพลาดที่เกิดขึ้นแบบไม่พร้อมกันจากการขอข้อมูลยานพาหนะจะทริกเกอร์เหตุการณ์ข้อผิดพลาด
ตัวอย่างต่อไปนี้แสดงวิธีฟังเหตุการณ์เหล่านี้เพื่อให้คุณจัดการข้อผิดพลาดได้
JavaScript
locationProvider.addListener('error', e => {
// e.error is the error that triggered the event.
console.error(e.error);
});
TypeScript
locationProvider.addListener('error', (e: google.maps.ErrorEvent) => {
// e.error is the error that triggered the event.
console.error(e.error);
});
หยุดติดตามยานพาหนะ
หากต้องการหยุดติดตามยานพาหนะ ให้ตั้งค่าขอบเขตของผู้ให้บริการตำแหน่งเป็น null แล้วนำผู้ให้บริการตำแหน่งออกจากมุมมองแผนที่ตามที่อธิบายไว้ในส่วนต่อไปนี้
ตั้งค่าขอบเขตของผู้ให้บริการตำแหน่งเป็น null
หากต้องการหยุดผู้ให้บริการตำแหน่งไม่ให้ติดตามยานพาหนะ ให้ตั้งค่าขอบเขตของผู้ให้บริการตำแหน่งเป็น null
การเดินทางแบบออนดีมานด์
JavaScript
locationProvider.locationRestriction = null;
TypeScript
locationProvider.locationRestriction = null;
งานที่กําหนดเวลาไว้
JavaScript
locationProvider.locationRestriction = null;
TypeScript
locationProvider.locationRestriction = null;
นำผู้ให้บริการตำแหน่งออกจากมุมมองแผนที่
ตัวอย่างต่อไปนี้แสดงวิธีนำผู้ให้บริการตำแหน่งออกจากมุมมองแผนที่
JavaScript
mapView.removeLocationProvider(locationProvider);
TypeScript
mapView.removeLocationProvider(locationProvider);
ติดตามยานพาหนะนำส่งขณะดูยานพาหนะนำส่ง
หากคุณใช้บริการ Mobility สำหรับงานที่กำหนดเวลาไว้ คุณจะดูยานพาหนะและแสดงเส้นทางและงานที่จะเกิดขึ้นสำหรับยานพาหนะนำส่งที่เฉพาะเจาะจงในมุมมองแผนที่เดียวกันได้ หากต้องการทำเช่นนี้ ให้สร้างอินสแตนซ์ทั้งผู้ให้บริการตำแหน่งยานพาหนะนำส่งและผู้ให้บริการตำแหน่งยานพาหนะนำส่ง แล้วเพิ่มทั้ง 2 รายการลงในมุมมองแผนที่ เมื่อสร้างอินสแตนซ์แล้ว ผู้ให้บริการตำแหน่งยานพาหนะนำส่งจะเริ่มแสดงยานพาหนะนำส่งบนแผนที่ ตัวอย่างต่อไปนี้แสดงวิธีสร้างอินสแตนซ์ของผู้ให้บริการตำแหน่งทั้ง 2 รายการ
JavaScript
deliveryFleetLocationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryFleetLocationProvider({
projectId,
authTokenFetcher,
// Optionally, specify location bounds to
// limit which delivery vehicles are
// retrieved and immediately start tracking.
locationRestriction: {
north: 37.3,
east: -121.8,
south: 37.1,
west: -122,
},
// Optionally, specify a filter to limit
// which delivery vehicles are retrieved.
deliveryVehicleFilter:
'attributes.foo = "bar" AND attributes.baz = "qux"',
});
deliveryVehicleLocationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryVehicleLocationProvider({
projectId,
authTokenFetcher
});
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [
deliveryFleetLocationProvider,
deliveryVehicleLocationProvider,
],
// Any other options
});
TypeScript
deliveryFleetLocationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryFleetLocationProvider({
projectId,
authTokenFetcher,
// Optionally, specify location bounds to
// limit which delivery vehicles are
// retrieved and immediately start tracking.
locationRestriction: {
north: 37.3,
east: -121.8,
south: 37.1,
west: -122,
},
// Optionally, specify a filter to limit
// which delivery vehicles are retrieved.
deliveryVehicleFilter:
'attributes.foo = "bar" AND attributes.baz = "qux"',
});
deliveryVehicleLocationProvider =
new google.maps.journeySharing
.FleetEngineDeliveryVehicleLocationProvider({
projectId,
authTokenFetcher
});
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [
deliveryFleetLocationProvider,
deliveryVehicleLocationProvider,
],
// Any other options
});
ใช้การปรับแต่งเครื่องหมายเพื่อติดตามยานพาหนะนำส่ง
หากต้องการเปิดใช้ผู้ให้บริการตำแหน่งยานพาหนะนำส่งเพื่อติดตามยานพาหนะนำส่งเมื่อคุณคลิกเครื่องหมายยานพาหนะ ให้ทำตามขั้นตอนต่อไปนี้
ปรับแต่งเครื่องหมายและเพิ่มการดำเนินการคลิก
ซ่อนตัวทำเครื่องหมายเพื่อป้องกันไม่ให้มีตัวทำเครื่องหมายซ้ำ
ตัวอย่างสำหรับขั้นตอนเหล่านี้อยู่ในส่วนต่อไปนี้
ปรับแต่งเครื่องหมายและเพิ่มการดำเนินการคลิก
JavaScript
// Specify the customization function either separately, or as a field in
// the options for the delivery fleet location provider constructor.
deliveryFleetLocationProvider.deliveryVehicleMarkerCustomization =
(params) => {
if (params.isNew) {
params.marker.addListener('click', () => {
// params.vehicle.name follows the format
// "providers/{provider}/deliveryVehicles/{vehicleId}".
// Only the vehicleId portion is used for the delivery vehicle
// location provider.
deliveryVehicleLocationProvider.deliveryVehicleId =
params.vehicle.name.split('/').pop();
});
}
};
TypeScript
// Specify the customization function either separately, or as a field in
// the options for the delivery fleet location provider constructor.
deliveryFleetLocationProvider.deliveryVehicleMarkerCustomization =
(params: google.maps.journeySharing.DeliveryVehicleMarkerCustomizationFunctionParams) => {
if (params.isNew) {
params.marker.addListener('click', () => {
// params.vehicle.name follows the format
// "providers/{provider}/deliveryVehicles/{vehicleId}".
// Only the vehicleId portion is used for the delivery vehicle
// location provider.
deliveryVehicleLocationProvider.deliveryVehicleId =
params.vehicle.name.split('/').pop();
});
}
};
ซ่อนตัวทำเครื่องหมายเพื่อป้องกันไม่ให้มีตัวทำเครื่องหมายซ้ำ
ซ่อนเครื่องหมายจากผู้ให้บริการตำแหน่งยานพาหนะนำส่งเพื่อป้องกันไม่ให้แสดงเครื่องหมาย 2 รายการสำหรับยานพาหนะคันเดียวกัน
JavaScript
// Specify the customization function either separately, or as a field in
// the options for the delivery vehicle location provider constructor.
deliveryVehicleLocationProvider.deliveryVehicleMarkerCustomization =
(params) => {
if (params.isNew) {
params.marker.setVisible(false);
}
};
TypeScript
// Specify the customization function either separately, or as a field in
// the options for the delivery vehicle location provider constructor.
deliveryVehicleLocationProvider.deliveryVehicleMarkerCustomization =
(params: deliveryVehicleMarkerCustomizationFunctionParams) => {
if (params.isNew) {
params.marker.setVisible(false);
}
};