این بخش نحوه استفاده از کتابخانه ردیابی ناوگان جاوا اسکریپت را برای مشاهده ناوگان نشان میدهد. این رویهها فرض میکنند که شما کتابخانه ردیابی ناوگان را راهاندازی کرده و نقشه را بارگذاری کردهاید. برای جزئیات بیشتر، به «راهاندازی کتابخانه ردیابی ناوگان جاوا اسکریپت» مراجعه کنید.
این سند موارد زیر را که میتوانید هنگام مشاهده ناوگان انجام دهید، مورد بحث قرار میدهد:
- ردیابی ناوگان را شروع کنید .
- به رویدادها گوش دهید و خطاها را مدیریت کنید .
- ردیابی را متوقف کنید .
- هنگام مشاهده ناوگان، یک وسیله نقلیه واحد را ردیابی کنید .
شروع ردیابی ناوگان
برای ردیابی ناوگان، باید یک ارائهدهنده موقعیت مکانی ناوگان را نمونهسازی کنید و محدودیتهای مکانی را برای نمای نقشه، همانطور که در بخشهای بعدی توضیح داده شده است، تنظیم کنید.
یک ارائه دهنده موقعیت مکانی ناوگان را نمونه سازی کنید
کتابخانه ردیابی ناوگان جاوا اسکریپت شامل یک ارائه دهنده موقعیت مکانی است که چندین وسیله نقلیه را از Fleet Engine دریافت میکند.
برای نمونهسازی آن، مراحل زیر را دنبال کنید:
از شناسه پروژه خود و همچنین مرجعی به ابزار دریافت توکن خود استفاده کنید .
استفاده از پرسوجوی فیلتر خودرو پرسوجوی فیلتر خودرو کنترل میکند که کدام خودروها در نقشه نمایش داده شوند. این فیلتر به موتور ناوگان منتقل میشود.
- برای سرویسهای درخواستی، از
vehicleFilterاستفاده کنید وListVehiclesRequest.filterبررسی کنید. - برای وظایف زمانبندیشده، از
deliveryVehicleFilterاستفاده کنید وListDeliveryVehiclesRequest.filterبررسی کنید.
- برای سرویسهای درخواستی، از
محدوده نمایش وسایل نقلیه را تنظیم کنید . از
locationRestrictionبرای محدود کردن ناحیه نمایش وسایل نقلیه روی نقشه استفاده کنید. ارائه دهنده مکان تا زمانی که این تنظیم نشده باشد فعال نیست. میتوانید محدودههای مکان را یا در سازنده یا بعد از سازنده تنظیم کنید.نمای نقشه را مقداردهی اولیه کنید .
مثالهای زیر نحوهی نمونهسازی یک ارائهدهندهی موقعیت مکانی ناوگان را برای سناریوهای وظایف بر اساس تقاضا و زمانبندیشده نشان میدهند. این مثالها همچنین نحوهی استفاده از locationRestriction در سازنده برای فعال کردن ارائهدهندهی موقعیت مکانی را نشان میدهند.
سفرهای درخواستی
جاوا اسکریپت
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"',
});
تایپ اسکریپت
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"',
});
وظایف برنامهریزیشده
جاوا اسکریپت
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"',
});
تایپ اسکریپت
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 بعداً در کد خود اضافه کنید، همانطور که در مثال جاوا اسکریپت زیر نشان داده شده است.
// 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 را طوری تنظیم کنید که با منطقهای که در حال حاضر در نمای نقشه قابل مشاهده است، مطابقت داشته باشد.
سفرهای درخواستی
جاوا اسکریپت
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;
}
});
تایپ اسکریپت
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;
}
});
وظایف برنامهریزیشده
جاوا اسکریپت
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;
}
});
تایپ اسکریپت
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;
}
});
مقداردهی اولیه نمای نقشه
پس از ساخت ارائهدهنده موقعیت مکانی، نمای نقشه را به همان روشی که هنگام دنبال کردن یک وسیله نقلیه انجام میدهید، تنظیم کنید.
پس از بارگذاری کتابخانه جاوا اسکریپت Journey Sharing، نمای نقشه را مقداردهی اولیه کرده و آن را به صفحه HTML اضافه کنید. صفحه شما باید حاوی یک عنصر <div> باشد که نمای نقشه را در خود جای میدهد. عنصر <div> در مثالهای زیر map_canvas نام دارد.
سفرهای درخواستی
جاوا اسکریپت
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);
تایپ اسکریپت
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);
وظایف برنامهریزیشده
جاوا اسکریپت
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);
تایپ اسکریپت
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);
به رویدادها گوش دهید و خطاها را مدیریت کنید
وقتی شروع به دنبال کردن ناوگان کردید، باید به تغییرات رویدادها گوش دهید و هرگونه خطایی را که پیش میآید، همانطور که در بخشهای بعدی توضیح داده شده است، مدیریت کنید.
به رویدادهای تغییر گوش دهید
شما میتوانید با استفاده از ارائهدهنده مکان، اطلاعات متا در مورد ناوگان را از شیء وسیله نقلیه بازیابی کنید. تغییرات در اطلاعات متا، یک رویداد بهروزرسانی را فعال میکند. اطلاعات متا شامل ویژگیهای وسیله نقلیه مانند وضعیت ناوبری، مسافت باقیمانده و ویژگیهای سفارشی است.
برای جزئیات، به موارد زیر مراجعه کنید:
مثالهای زیر نحوهی گوش دادن به این رویدادهای تغییر را نشان میدهند.
سفرهای درخواستی
جاوا اسکریپت
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);
}
});
تایپ اسکریپت
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);
}
}
});
وظایف برنامهریزیشده
جاوا اسکریپت
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);
}
}
});
تایپ اسکریپت
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);
}
}
});
به خطاها گوش دهید
شما همچنین میخواهید خطاهایی را که هنگام تعقیب یک وسیله نقلیه رخ میدهند، شناسایی و مدیریت کنید. خطاهایی که به صورت ناهمزمان از درخواست اطلاعات وسیله نقلیه ناشی میشوند، رویدادهای خطا را تحریک میکنند.
مثال زیر نحوهی گوش دادن به این رویدادها را نشان میدهد تا بتوانید خطاها را مدیریت کنید.
جاوا اسکریپت
locationProvider.addListener('error', e => {
// e.error is the error that triggered the event.
console.error(e.error);
});
تایپ اسکریپت
locationProvider.addListener('error', (e: google.maps.ErrorEvent) => {
// e.error is the error that triggered the event.
console.error(e.error);
});
ردیابی ناوگان را متوقف کنید
برای متوقف کردن ردیابی ناوگان، محدودههای ارائهدهنده موقعیت مکانی را روی null تنظیم میکنید و سپس ارائهدهنده موقعیت مکانی را از نمای نقشه، همانطور که در بخشهای بعدی توضیح داده شده است، حذف میکنید.
محدودههای ارائهدهنده مکان را روی null تنظیم کنید.
برای جلوگیری از ردیابی ناوگان توسط ارائهدهنده موقعیت مکانی، محدودیتهای ارائهدهنده موقعیت مکانی را روی null تنظیم کنید.
سفرهای درخواستی
جاوا اسکریپت
locationProvider.locationRestriction = null;
تایپ اسکریپت
locationProvider.locationRestriction = null;
وظایف برنامهریزیشده
جاوا اسکریپت
locationProvider.locationRestriction = null;
تایپ اسکریپت
locationProvider.locationRestriction = null;
ارائه دهنده موقعیت مکانی را از نمای نقشه حذف کنید
مثال زیر نحوه حذف یک ارائه دهنده موقعیت مکانی از نمای نقشه را نشان میدهد.
جاوا اسکریپت
mapView.removeLocationProvider(locationProvider);
تایپ اسکریپت
mapView.removeLocationProvider(locationProvider);
هنگام مشاهده ناوگان تحویل، وسیله نقلیه تحویل را ردیابی کنید
اگر از سرویسهای Mobility برای وظایف زمانبندیشده استفاده میکنید، میتوانید هم ناوگان را مشاهده کنید و هم مسیر و وظایف آینده را برای یک وسیله نقلیه تحویل خاص در همان نمای نقشه نمایش دهید. برای انجام این کار، هم یک ارائهدهنده موقعیت مکانی ناوگان تحویل و هم یک ارائهدهنده موقعیت مکانی وسیله نقلیه تحویل را نمونهسازی کنید و هر دو را به نمای نقشه اضافه کنید. پس از نمونهسازی، ارائهدهنده موقعیت مکانی ناوگان تحویل شروع به نمایش وسایل نقلیه تحویل روی نقشه میکند. مثالهای زیر نحوه نمونهسازی هر دو ارائهدهنده موقعیت مکانی را نشان میدهند:
جاوا اسکریپت
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
});
تایپ اسکریپت
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
});
از سفارشیسازی نشانگر برای ردیابی وسیله نقلیه تحویل استفاده کنید
برای اینکه ارائهدهنده موقعیت مکانی وسیله نقلیه تحویل، بتواند وقتی روی نشانگر ناوگان آن کلیک میکنید، وسیله نقلیه تحویل را ردیابی کند، این مراحل را دنبال کنید:
یک نشانگر را سفارشی کنید و یک عمل کلیک اضافه کنید.
برای جلوگیری از تکرار نشانگرها، نشانگر را پنهان کنید.
نمونههایی از این مراحل در بخشهای بعدی آمده است.
یک نشانگر را سفارشی کنید و یک عمل کلیک اضافه کنید
جاوا اسکریپت
// 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();
});
}
};
تایپ اسکریپت
// 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();
});
}
};
پنهان کردن نشانگر برای جلوگیری از تکرار نشانگرها
برای جلوگیری از نمایش دو نشانگر برای یک وسیله نقلیه، نشانگر را از ارائه دهنده موقعیت مکانی وسیله نقلیه تحویل پنهان کنید:
جاوا اسکریپت
// 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);
}
};
تایپ اسکریپت
// 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);
}
};