Z tej sekcji dowiesz się, jak śledzić pojazd w przypadku przejazdów na żądanie lub zaplanowanych zadań za pomocą biblioteki JavaScript do śledzenia floty.
Aby śledzić pojazd, wykonaj te czynności:
- Wczytaj bibliotekę i zainicjuj widok mapy.
- Podaj lokalizację pojazdu i widok mapy.
- Nasłuchuj zdarzeń i obsługuj błędy.
- Zatrzymaj śledzenie.
Wczytywanie biblioteki i inicjowanie widoku mapy
Aby wyświetlać operacje floty na mapie na stronie internetowej, użyj skryptu, który wywołuje mapę za pomocą klucza interfejsu API. Poniższy przykład pokazuje, jak to zrobić w HTML:
Źródło adresu URL: wywołuje interfejs Google Maps API, aby poprosić o mapę za pomocą klucza interfejsu API.
Parametr
callback: uruchamia funkcjęinitMap, gdy interfejs API zwróci wywołanie.Parametr
libraries: wczytuje bibliotekę śledzenia floty.Atrybut
defer: umożliwia przeglądarce dalsze renderowanie pozostałej części strony podczas wczytywania interfejsu API.<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=journeySharing" defer></script>
Podawanie lokalizacji pojazdu i widoku mapy
Aby rozpocząć śledzenie pojazdu, musisz utworzyć instancję dostawcy lokalizacji pojazdu i zainicjować widok mapy z identyfikatorem pojazdu, jak opisano w kolejnych sekcjach.
Tworzenie instancji dostawcy lokalizacji pojazdu
Biblioteka JavaScript do śledzenia floty zawiera dostawcę lokalizacji dla interfejsu Fleet Engine API. Aby utworzyć jego instancję, użyj identyfikatora projektu i odwołania do narzędzia do pobierania tokenów, jak pokazano w tych przykładach.
Przejazdy na żądanie
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',
});
Zaplanowane zadania
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',
});
Inicjowanie widoku mapy
Po wczytaniu biblioteki JavaScript Journey Sharing zainicjuj widok mapy i dodaj go do strony HTML. Strona powinna zawierać element <div>, który zawiera widok mapy. W tych przykładach element <div> ma nazwę map_canvas.
Przejazdy na żądanie
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);
Zaplanowane zadania
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);
Nasłuchiwanie zdarzeń i obsługa błędów
Po rozpoczęciu śledzenia pojazdu możesz aktualizować jego postęp na mapie i obsługiwać błędy podczas podróży.
Nasłuchiwanie zdarzeń pojazdu
Aby śledzić postęp pojazdu w przypadku przejazdów na żądanie lub zaplanowanych zadań, musisz nasłuchiwać zdarzeń zmiany.
Metadane pobierasz z obiektu vehicle lub deliveryVehicle za pomocą dostawcy lokalizacji. Metadane zawierają szacowany czas dotarcia na miejsce i pozostałą odległość do następnego miejsca odbioru lub wysiadki pojazdu. Zmiany w metadanych wywołują zdarzenie update u dostawcy lokalizacji.
Poniższy przykład pokazuje, jak nasłuchiwać tych zdarzeń zmiany.
Przejazdy na żądanie
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);
}
});
Zaplanowane zadania
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);
}
});
Obsługa błędów
Po wczytaniu biblioteki JavaScript Journey Sharing zainicjuj widok mapy i dodaj go do strony HTML. Strona powinna zawierać element <div>, który zawiera widok mapy. W tych przykładach element <div> ma nazwę map_canvas.
Przejazdy na żądanie
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);
Zaplanowane zadania
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);
Zatrzymywanie śledzenia pojazdu
Aby zatrzymać śledzenie pojazdu, musisz usunąć go z dostawcy lokalizacji i usunąć dostawcę lokalizacji z widoku mapy, jak opisano w kolejnych sekcjach. Przykłady dotyczą zarówno przejazdów na żądanie, jak i zaplanowanych zadań.
Usuwanie pojazdu z dostawcy lokalizacji
Aby dostawca lokalizacji przestał śledzić pojazd, usuń identyfikator pojazdu dostawczego z dostawcy lokalizacji.
Przejazdy na żądanie
JavaScript
locationProvider.vehicleId = '';
TypeScript
locationProvider.vehicleId = '';
Zaplanowane zadania
JavaScript
locationProvider.deliveryVehicleId = '';
TypeScript
locationProvider.deliveryVehicleId = '';
Usuwanie dostawcy lokalizacji z widoku mapy
Poniższy przykład pokazuje, jak usunąć dostawcę lokalizacji z widoku mapy.
JavaScript
mapView.removeLocationProvider(locationProvider);
TypeScript
mapView.removeLocationProvider(locationProvider);