Fahrzeug verfolgen

In diesem Abschnitt wird beschrieben, wie Sie die JavaScript-Bibliothek für das Flottentracking verwenden, um ein Fahrzeug für On-Demand-Fahrten oder geplante Aufgaben zu verfolgen.

So verfolgen Sie ein Fahrzeug:

  1. Bibliothek laden und Kartenansicht initialisieren
  2. Fahrzeugstandort und Kartenansicht angeben
  3. Auf Ereignisse warten und Fehler beheben
  4. Tracking beenden

Bibliothek laden und Kartenansicht initialisieren

Wenn Sie Ihre Flottenaktivitäten auf einer Karte auf Ihrer Webseite anzeigen möchten, verwenden Sie ein Script, das eine Karte mit Ihrem API-Schlüssel aufruft. Im folgenden Beispiel wird gezeigt, wie das in HTML möglich ist:

  • URL-Quelle: Ruft die Google Maps API auf, um eine Karte mit Ihrem API-Schlüssel anzufordern.

  • callback-Parameter: Führt die initMap-Funktion aus, nachdem die API den Aufruf zurückgegeben hat.

  • Parameter libraries: Lädt die Bibliothek für das Flottentracking.

  • Attribut defer: Ermöglicht dem Browser, den Rest der Seite weiter zu rendern, während die API geladen wird.

    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=journeySharing" defer></script>
    

Fahrzeugstandort und Kartenansicht bereitstellen

Wenn Sie ein Fahrzeug tracken möchten, müssen Sie sowohl einen Standortanbieter für das Fahrzeug instanziieren als auch eine Kartenansicht mit der Fahrzeug-ID initialisieren, wie in den folgenden Abschnitten beschrieben.

Anbieter für Fahrzeugstandorte instanziieren

Die JavaScript-Bibliothek für das Fahrten- und Liefertracking enthält einen Standortanbieter für die Fleet Engine API. Verwenden Sie Ihre Projekt-ID und einen Verweis auf Ihren Token-Fetcher, um ihn wie in den folgenden Beispielen zu instanziieren.

On-Demand-Fahrten

JavaScript

locationProvider =
    new google.maps.journeySharing
        .FleetEngineVehicleLocationProvider({
          projectId,
          authTokenFetcher,

          // Optionally, you may specify
          // vehicleId to immediately start
          // tracking.
          vehicleId: 'your-vehicle-id',
});

TypeScript

locationProvider =
    new google.maps.journeySharing
        .FleetEngineVehicleLocationProvider({
          projectId,
          authTokenFetcher,

          // Optionally, you may specify
          // vehicleId to immediately start
          // tracking.
          vehicleId: 'your-vehicle-id',
});

Geplante Aufgaben

JavaScript

locationProvider =
    new google.maps.journeySharing
        .FleetEngineDeliveryVehicleLocationProvider({
          projectId,
          authTokenFetcher,

          // Optionally, you may specify
          // deliveryVehicleId to immediately start
          // tracking.
          deliveryVehicleId: 'your-delivery-id',
});

TypeScript

locationProvider =
    new google.maps.journeySharing
        .FleetEngineDeliveryVehicleLocationProvider({
          projectId,
          authTokenFetcher,

          // Optionally, you may specify
          // deliveryVehicleId to immediately start
          // tracking.
          deliveryVehicleId: 'your-delivery-id',
});

Kartenansicht initialisieren

Nachdem Sie die JavaScript-Bibliothek für die Fahrtenfreigabe geladen haben, initialisieren Sie die Kartenansicht und fügen Sie sie der HTML-Seite hinzu. Ihre Seite sollte ein <div>-Element enthalten, das die Kartenansicht enthält. Das <div>-Element wird in den folgenden Beispielen map_canvas genannt.

On-Demand-Fahrten

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

Geplante Aufgaben

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

Auf Ereignisse warten und Fehler beheben

Nachdem Sie mit dem Tracking eines Fahrzeugs begonnen haben, können Sie den Fortschritt auf einer Karte aktualisieren und Fehler beheben, die während der Fahrt auftreten.

Auf Fahrzeugereignisse warten

Wenn Sie den Fortschritt eines Fahrzeugs bei On-Demand-Fahrten oder geplanten Aufgaben verfolgen möchten, müssen Sie auf Änderungsereignisse warten.

Sie rufen Metadaten aus dem vehicle- oder deliveryVehicle-Objekt über den Standortanbieter ab. Die Metainformationen umfassen die voraussichtliche Ankunftszeit und die verbleibende Entfernung bis zur nächsten Abholung oder Abgabe des Fahrzeugs. Änderungen an den Metainformationen lösen ein Aktualisierungsereignis im Standortanbieter aus.

Das folgende Beispiel zeigt, wie Sie auf diese Änderungsereignisse warten können.

On-Demand-Fahrten

JavaScript

locationProvider.addListener('update', e => {
  // e.vehicle contains data that may be
  // useful to the rest of the UI.
  if (e.vehicle) {
    console.log(e.vehicle.vehicleState);
  }
});

TypeScript

locationProvider.addListener('update',
    (e: google.maps.journeySharing.FleetEngineVehicleLocationProviderUpdateEvent) => {
  // e.vehicle contains data that may be
  // useful to the rest of the UI.
  if (e.vehicle) {
    console.log(e.vehicle.vehicleState);
  }
});

Geplante Aufgaben

JavaScript

locationProvider.addListener('update', e => {
  // e.deliveryVehicle contains data that may be
  // useful to the rest of the UI.
  if (e.deliveryVehicle) {
    console.log(e.deliveryVehicle.remainingDuration);
  }
});

TypeScript

locationProvider.addListener('update',
    (e: google.maps.journeySharing.FleetEngineDeliveryVehicleLocationProviderUpdateEvent) => {
  // e.deliveryVehicle contains data that may be
  // useful to the rest of the UI.
  if (e.deliveryVehicle) {
    console.log(e.deliveryVehicle.remainingDuration);
  }
});

Fehler verarbeiten

Nachdem Sie die JavaScript-Bibliothek für die Fahrtenfreigabe geladen haben, initialisieren Sie die Kartenansicht und fügen Sie sie der HTML-Seite hinzu. Ihre Seite sollte ein <div>-Element enthalten, das die Kartenansicht enthält. Das <div>-Element wird in den folgenden Beispielen als map_canvas bezeichnet.

On-Demand-Fahrten

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

Geplante Aufgaben

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

Fahrzeug nicht mehr beobachten

Wenn Sie ein Fahrzeug nicht mehr verfolgen möchten, müssen Sie es aus dem Standortanbieter entfernen und den Standortanbieter aus der Kartenansicht entfernen, wie in den folgenden Abschnitten beschrieben. Die Beispiele hier gelten sowohl für die Implementierung von On-Demand-Fahrten als auch für die Implementierung von geplanten Aufgaben.

Fahrzeug vom Standortanbieter entfernen

Wenn der Standortanbieter ein Fahrzeug nicht mehr verfolgen soll, entfernen Sie die Lieferfahrzeug-ID vom Standortanbieter.

On-Demand-Fahrten

JavaScript

locationProvider.vehicleId = '';

TypeScript

locationProvider.vehicleId = '';

Geplante Aufgaben

JavaScript

locationProvider.deliveryVehicleId = '';

TypeScript

locationProvider.deliveryVehicleId = '';

Standortanbieter aus der Kartenansicht entfernen

Im folgenden Beispiel wird gezeigt, wie ein Standortanbieter aus der Kartenansicht entfernt wird.

JavaScript

mapView.removeLocationProvider(locationProvider);

TypeScript

mapView.removeLocationProvider(locationProvider);

Nächste Schritte