ดูกลุ่มอุปกรณ์

ส่วนนี้แสดงวิธีใช้ไลบรารีการติดตามกลุ่มยานพาหนะ 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);
});

หยุดติดตามกลุ่มยานพาหนะ

หากต้องการหยุดติดตามกลุ่มยานพาหนะ ให้ตั้งค่าขอบเขตของผู้ให้บริการตำแหน่งเป็น 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 สำหรับงานที่กำหนดเวลาไว้ คุณจะดูทั้งกลุ่มยานพาหนะและแสดงเส้นทางและงานที่กำลังจะมาถึงสำหรับยานพาหนะนำส่งที่เฉพาะเจาะจงในมุมมองแผนที่เดียวกันได้ โดยให้สร้างอินสแตนซ์ทั้ง DeliveryFleetLocation Provider และ DeliveryVehicleLocationProvider แล้วเพิ่มทั้ง 2 รายการลงใน Map View เมื่อสร้างอินสแตนซ์แล้ว ผู้ให้บริการตำแหน่งกองยานนำส่งจะเริ่มแสดง ยานพาหนะนำส่งบนแผนที่ ตัวอย่างต่อไปนี้แสดงวิธีสร้างอินสแตนซ์ ทั้งผู้ให้บริการตำแหน่ง

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

ซ่อนเครื่องหมายเพื่อป้องกันไม่ให้มีเครื่องหมายซ้ำ

ซ่อนเครื่องหมายจากผู้ให้บริการตำแหน่งยานพาหนะนำส่งเพื่อป้องกันไม่ให้แสดงเครื่องหมาย 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);
    }
  };

ขั้นตอนถัดไป