عرض مجموعة مركبات

يعرض هذا القسم كيفية استخدام مكتبة تتبُّع مجموعة JavaScript. لعرض أسطول. تفترض هذه الإجراءات أنك أعددت الأسطول مكتبة التتبع وتحميل خريطة. للحصول على التفاصيل، يمكنك مراجعة إعداد مكتبة تتبُّع مجموعة JavaScript

يناقش هذا المستند الأشياء التالية التي يمكنك القيام بها عند عرض مجموعة الأجهزة:

  1. ابدأ بتتبُّع مجموعة الأجهزة.
  2. الاستماع إلى الأحداث ومعالجة الأخطاء
  3. توقّف عن التتبُّع.
  4. تتبُّع مركبة واحدة أثناء عرض أسطول

ابدأ بتتبُّع الأسطول

لتتبع أسطول من الأسطول، تحتاج إلى إنشاء مثيل لمزود موقع الأسطول تعيين قيود الموقع الجغرافي لإطار عرض الخريطة كما هو موضح في ما يلي الأقسام.

إنشاء مثيل لموقع أسطول المركبات

تتضمن مكتبة تتبع مجموعة JavaScript موفّر موقع لجلب عدة مركبات من Fleet Engine.

لإنشاء مثيل له، اتبع الخطوات التالية:

  1. استخدِم رقم تعريف المشروع بالإضافة إلى مرجع إلى أداة استرجاع الرموز المميّزة.

  2. استخدام طلب بحث عن فلتر مركبات: يتحكّم طلب البحث في فلتر المركبات في المركبات التي تعرضها الخريطة. يتم تمرير الفلتر إلى Fleet Engine.

    • للحصول على الخدمات المتوفرة عند الطلب، يمكنك استخدام vehicleFilter ومراجعة ListVehiclesRequest.filter.
    • بالنسبة إلى المهام المُجدوَلة، استخدِم "deliveryVehicleFilter" وراجِعها. ListDeliveryVehiclesRequest.filter
  3. ضبط الحدود لعرض المركبة استخدام locationRestriction من أجل تحديد المنطقة التي يتم فيها عرض المركبات على الخريطة. الموقع الجغرافي لن يكون مقدِّم الخدمة نشطًا إلى أن يتم ضبطه. يمكنك ضبط حدود الموقع الجغرافي في الدالة الإنشائية أو بعد الدالة الإنشائية.

  4. إعداد عرض الخريطة.

توضح الأمثلة التالية كيفية إنشاء مثيل لموقع أسطول المركبات لكل من سيناريوهات المهام عند الطلب والمجدولة. توضح هذه الأمثلة أيضًا كيفية استخدام 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);

الاطّلاع على الأحداث ومعالجة الأخطاء

بعد البدء في متابعة مجموعة الأجهزة، عليك معرفة التغييرات التي تطرأ على الفعالية. والتعامل مع أي أخطاء قد تظهر كما هو موضح في الأقسام التالية.

الاستماع إلى أحداث التغيير

يمكنك استرداد معلومات تعريفية عن الأسطول من كائن المركبة باستخدام مزود الموقع. يؤدي إجراء تغييرات على المعلومات الوصفية إلى إجراء تحديث فعالية. تتضمن المعلومات الوصفية خصائص المركبة، مثل التنقل. والمسافة المتبقية والسمات المخصصة.

للتعرُّف على التفاصيل، يُرجى الرجوع إلى ما يلي:

توضّح الأمثلة التالية كيفية الاستماع إلى أحداث التغيير هذه.

رحلات عند الطلب

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);
});

التوقف عن تتبُّع أسطول المركبات

لإيقاف تتبع الأسطول، يمكنك تعيين حدود موفر خدمة الموقع على قيمة خالية، ثم أزِل موفِّر الموقع من عرض الخريطة كما هو موضح في الأقسام التالية.

ضبط حدود موفِّر الموقع الجغرافي على "خالٍ"

لمنع مقدم خدمة الموقع من تتبع الأسطول، اضبط الحدود من موفر الموقع إلى قيمة خالية.

رحلات عند الطلب

JavaScript

locationProvider.locationRestriction = null;

TypeScript

locationProvider.locationRestriction = null;

المهام المُجدوَلة

JavaScript

locationProvider.locationRestriction = null;

TypeScript

locationProvider.locationRestriction = null;

إزالة موفر الموقع من عرض الخريطة

يوضح المثال التالي كيفية إزالة مزوّد موقع جغرافي من عرض الخريطة.

JavaScript

mapView.removeLocationProvider(locationProvider);

TypeScript

mapView.removeLocationProvider(locationProvider);

تتبَّع مركبة توصيل أثناء عرض أسطول التسليم

إذا كنت تستخدم "خدمات الجوّال" لإنجاز مهام مُجدوَلة، يمكنك عرض أسطول المركبات وعرض المسار والمهام القادمة لمركبة توصيل معينة في عرض الخريطة نفسه. لإجراء ذلك، أنشئ مثيلاً لموقع شركة التسليم مقدّم الخدمة ومقدِّم الموقع الجغرافي لمركبة التسليم، وأضِفهما إلى الخريطة مشاهدة. بعد إنشاء مثيل، يبدأ مقدم خدمة الموقع لأسطول التسليم في عرض ومركبات التسليم على الخريطة. توضح الأمثلة التالية كيفية إنشاء مثيل كلا موفري الموقع:

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
});

استخدام تخصيص محدّد الموقع لتتبُّع مركبة توصيل

للسماح لمقدّم الموقع الجغرافي لمركبة التسليم بتتبُّع مركبة التسليم عند النقر على علامة مجموعة الأجهزة، اتبع الخطوات التالية:

  1. تخصيص علامة وإضافة إجراء نقر.

  2. إخفاء العلامة لمنع تكرار العلامات.

تتوفّر أمثلة على هذه الخطوات في الأقسام التالية.

تخصيص علامة وإضافة إجراء نقر

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();
      });
    }
  };

إخفاء العلامة لمنع تكرار العلامات

إخفاء العلامة من مقدِّم الموقع الجغرافي لمركبة التسليم لمنع العرض علامتان للمركبة نفسها:

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);
    }
  };

الخطوات التالية