Z tej sekcji dowiesz się, jak za pomocą biblioteki śledzenia floty w JavaScript śledzić pojazd w przypadku przejazdów na żądanie lub zaplanowanych zadań.
Aby śledzić pojazd, wykonaj te czynności:
- Wczytaj bibliotekę i zainicjuj widok mapy
- Podawanie lokalizacji pojazdu i widoku mapy
- Nasłuchiwanie zdarzeń i obsługa błędów
- Zakończ śledzenie
Wczytywanie biblioteki i inicjowanie widoku mapy
Aby wyświetlić 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-u:
Źródło adresu URL: wywołuje interfejs API Map Google, aby za pomocą klucza API wysłać żądanie mapy.
callback
parametr: uruchamia funkcjęinitMap
po zwróceniu wywołania przez interfejs API.libraries
parameter: wczytuje bibliotekę śledzenia floty.Atrybut
defer
: umożliwia przeglądarce kontynuowanie renderowania 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>
podawać lokalizację pojazdu i wyświetlać mapę,
Aby rozpocząć śledzenie pojazdu, musisz utworzyć instancję dostawcy lokalizacji pojazdu i zainicjować widok mapy za pomocą identyfikatora pojazdu, jak opisano w kolejnych sekcjach.
Utwórz instancję dostawcy danych o lokalizacji pojazdu
Biblioteka śledzenia floty w JavaScript zawiera dostawcę lokalizacji dla interfejsu Fleet Engine API. Użyj identyfikatora projektu i odwołania do pobierania tokenów, aby utworzyć instancję, jak pokazano w przykładach poniżej.
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. Element <div> w przykładach poniżej 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ępy na mapie i obsługiwać błędy podczas jego przemieszczania się po trasie.
Nasłuchiwanie zdarzeń związanych z pojazdami
Aby śledzić postępy pojazdu w przypadku przejazdów na żądanie lub zaplanowanych zadań, musisz nasłuchiwać zdarzeń zmiany.
Metadane są pobierane z obiektu vehicle
lub deliveryVehicle
za pomocą dostawcy lokalizacji. Metadane zawierają szacowany czas przyjazdu i pozostałą odległość do następnego miejsca odbioru lub wysadzenia. Zmiany w metadanych powodują wywołanie zdarzenia update w usłudze lokalizacyjnej.
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ługuj błędy
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. Element <div> w przykładach poniżej 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);
Przestań śledzić pojazd
Aby przestać śledzić pojazd, musisz usunąć go z usługi lokalizacyjnej i usunąć usługę lokalizacyjną z widoku mapy, jak opisano w kolejnych sekcjach. Przykłady te dotyczą zarówno przejazdów na żądanie, jak i zaplanowanych zadań.
Usuwanie pojazdu od dostawcy lokalizacji
Aby dostawca lokalizacji przestał śledzić pojazd, usuń z niego identyfikator pojazdu dostawczego.
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);