Phần này cho biết cách sử dụng thư viện theo dõi đội xe JavaScript để xem đội xe. Các quy trình này giả định rằng bạn đã thiết lập thư viện theo dõi đội xe và tải một bản đồ. Để biết thông tin chi tiết, hãy xem bài viết Thiết lập thư viện theo dõi đội xe bằng JavaScript.
Tài liệu này thảo luận về những việc bạn có thể làm khi xem một đội xe:
- Bắt đầu theo dõi đội xe.
- Lắng nghe các sự kiện và xử lý lỗi.
- Dừng theo dõi.
- Theo dõi một chiếc xe khi xem đội xe.
Bắt đầu theo dõi đội xe
Để theo dõi một đội xe, bạn cần khởi tạo một trình cung cấp vị trí đội xe và đặt các hạn chế về vị trí cho khung hiển thị bản đồ như mô tả trong các phần sau.
Tạo thực thể cho trình cung cấp vị trí của đội xe
Thư viện theo dõi đội xe JavaScript bao gồm một trình cung cấp vị trí tìm nạp nhiều xe từ Fleet Engine.
Để khởi tạo, hãy làm theo các bước sau:
Sử dụng mã dự án cũng như một tham chiếu đến trình tìm nạp mã thông báo.
Sử dụng truy vấn bộ lọc xe Truy vấn bộ lọc xe kiểm soát những chiếc xe mà bản đồ hiển thị. Bộ lọc được truyền đến Fleet Engine.
- Đối với các dịch vụ theo yêu cầu, hãy sử dụng
vehicleFilter
và xem lạiListVehiclesRequest.filter
- Đối với các tác vụ định kỳ, hãy sử dụng
deliveryVehicleFilter
và xem lạiListDeliveryVehiclesRequest.filter
- Đối với các dịch vụ theo yêu cầu, hãy sử dụng
Đặt vùng ranh giới cho màn hình hiển thị xe. Sử dụng
locationRestriction
để giới hạn khu vực hiển thị xe trên bản đồ. Trình cung cấp vị trí sẽ không hoạt động cho đến khi bạn đặt giá trị này. Bạn có thể đặt ranh giới vị trí trong hàm khởi tạo hoặc sau hàm khởi tạo.Khởi chạy chế độ xem bản đồ.
Các ví dụ sau đây cho thấy cách khởi tạo trình cung cấp vị trí của đội xe cho cả trường hợp theo yêu cầu và trường hợp có lịch trình. Các ví dụ này cũng cho thấy cách sử dụng locationRestriction
trong hàm khởi tạo để kích hoạt trình cung cấp vị trí.
Chuyến đi theo yêu cầu
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"',
});
Nhiệm vụ theo lịch
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"',
});
Để đặt locationRestriction
sau hàm khởi tạo, hãy thêm locationProvider.locationRestriction
sau đó trong mã của bạn như minh hoạ trong ví dụ sau đây về 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,
};
Đặt hạn chế về vị trí bằng khung hiển thị bản đồ
Bạn cũng có thể đặt ranh giới locationRestriction
cho phù hợp với khu vực hiện đang hiển thị trong chế độ xem bản đồ.
Chuyến đi theo yêu cầu
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;
}
});
Nhiệm vụ theo lịch
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;
}
});
Khởi chạy chế độ xem bản đồ
Sau khi bạn tạo trình cung cấp vị trí, hãy khởi tạo khung hiển thị bản đồ theo cách tương tự như khi bạn theo dõi một chiếc xe.
Sau khi tải thư viện JavaScript Chia sẻ hành trình, hãy khởi động chế độ xem bản đồ và thêm chế độ xem đó vào trang HTML. Trang của bạn phải chứa một phần tử <div> chứa chế độ xem bản đồ. Phần tử <div> có tên là map_canvas trong các ví dụ sau.=
Chuyến đi theo yêu cầu
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);
Nhiệm vụ theo lịch
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);
Theo dõi các sự kiện và xử lý lỗi
Sau khi bắt đầu theo dõi đội xe, bạn cần theo dõi các thay đổi về sự kiện và xử lý mọi lỗi phát sinh như mô tả trong các phần sau.
Theo dõi các sự kiện thay đổi
Bạn có thể truy xuất thông tin meta về đội xe từ đối tượng xe bằng cách sử dụng trình cung cấp vị trí. Các thay đổi đối với thông tin meta sẽ kích hoạt sự kiện cập nhật. Thông tin meta bao gồm các thuộc tính của xe, chẳng hạn như trạng thái điều hướng, khoảng cách còn lại và các thuộc tính tuỳ chỉnh.
Để biết thông tin chi tiết, hãy tham khảo phần sau:
- Thông tin tham khảo về các lựa chọn xe cho chuyến đi theo yêu cầu
- Tài liệu tham khảo về các lựa chọn cho xe trong tác vụ định kỳ
Các ví dụ sau đây cho thấy cách theo dõi những sự kiện thay đổi này.
Chuyến đi theo yêu cầu
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);
}
}
});
Nhiệm vụ theo lịch
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);
}
}
});
Lắng nghe lỗi
Bạn cũng muốn lắng nghe và xử lý các lỗi xảy ra trong khi theo dõi một chiếc xe. Các lỗi phát sinh không đồng bộ từ việc yêu cầu thông tin về xe sẽ kích hoạt các sự kiện lỗi.
Ví dụ sau đây cho thấy cách theo dõi các sự kiện này để bạn có thể xử lý lỗi.
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);
});
Dừng theo dõi đội xe
Để dừng theo dõi đội xe, bạn đặt ranh giới của trình cung cấp vị trí thành giá trị rỗng, sau đó xoá trình cung cấp vị trí khỏi khung hiển thị bản đồ như mô tả trong các phần sau.
Đặt ranh giới của trình cung cấp vị trí thành giá trị rỗng
Để ngăn trình cung cấp vị trí theo dõi đội xe, hãy đặt ranh giới của trình cung cấp vị trí thành giá trị rỗng.
Chuyến đi theo yêu cầu
JavaScript
locationProvider.locationRestriction = null;
TypeScript
locationProvider.locationRestriction = null;
Nhiệm vụ theo lịch
JavaScript
locationProvider.locationRestriction = null;
TypeScript
locationProvider.locationRestriction = null;
Xoá trình cung cấp vị trí khỏi khung hiển thị bản đồ
Ví dụ sau đây cho biết cách xoá một trình cung cấp vị trí khỏi khung hiển thị bản đồ.
JavaScript
mapView.removeLocationProvider(locationProvider);
TypeScript
mapView.removeLocationProvider(locationProvider);
Theo dõi xe giao hàng trong khi xem đội xe giao hàng
Nếu đang sử dụng dịch vụ Mobility cho các tác vụ định kỳ, bạn có thể xem đội xe và hiển thị tuyến đường cũng như các tác vụ sắp tới cho một xe giao hàng cụ thể trong cùng một chế độ xem bản đồ. Để thực hiện việc này, hãy khởi tạo cả Delivery Fleet Location Provider và Delivery Vehicle Location Provider, rồi thêm cả hai vào khung hiển thị bản đồ. Sau khi được khởi tạo, trình cung cấp vị trí của đội xe giao hàng sẽ bắt đầu hiển thị các xe giao hàng trên bản đồ. Các ví dụ sau đây cho thấy cách khởi tạo cả hai trình cung cấp vị trí:
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
});
Sử dụng chế độ tuỳ chỉnh điểm đánh dấu để theo dõi xe giao hàng
Để cho phép trình cung cấp vị trí xe giao hàng theo dõi xe giao hàng khi bạn nhấp vào điểm đánh dấu đội xe, hãy làm theo các bước sau:
Tuỳ chỉnh một điểm đánh dấu và thêm thao tác nhấp.
Ẩn điểm đánh dấu để tránh điểm đánh dấu trùng lặp.
Ví dụ về các bước này có trong các phần sau.
Tuỳ chỉnh điểm đánh dấu và thêm hành động nhấp
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();
});
}
};
Ẩn điểm đánh dấu để tránh điểm đánh dấu trùng lặp
Ẩn điểm đánh dấu khỏi trình cung cấp vị trí của xe giao hàng để ngăn việc kết xuất 2 điểm đánh dấu cho cùng một xe:
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);
}
};