Z tego dokumentu dowiesz się, jak dostosować wygląd i działanie mapy oraz kontrolować widoczność danych i opcje widocznego obszaru. Możesz to zrobić na te sposoby:
- Korzystanie z definiowania stylów map w Google Cloud
- Ustawianie opcji stylu mapy bezpośrednio we własnym kodzie
Definiowanie stylów map w Google Cloud
Aby zastosować styl mapy do mapy udostępniania przejazdów konsumenta w JavaScript, podczas tworzenia JourneySharingMapView określ mapId i inne mapOptions.
Poniższe przykłady pokazują, jak zastosować styl mapy za pomocą identyfikatora mapy.
JavaScript
const mapView = new google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
mapOptions: {
mapId: 'YOUR_MAP_ID'
}
// Any other styling options.
});
TypeScript
const mapView = new google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
mapOptions: {
mapId: 'YOUR_MAP_ID'
}
// Any other styling options.
});
Mapy z własnym stylem bezpośrednio we własnym kodzie
Możesz też dostosować styl mapy, ustawiając opcje mapy podczas tworzenia obiektu JourneySharingMapView. Poniższe przykłady pokazują, jak stylizować mapę za pomocą opcji mapy. Więcej informacji o opcjach mapy, które możesz ustawić, znajdziesz w dokumentacji interfejsu Google Maps JavaScript API: mapOptions.
JavaScript
const mapView = new google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
mapOptions: {
styles: [
{
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [
{ "color": "#CCFFFF" }
]
}
]
}
});
TypeScript
const mapView = new google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
mapOptions: {
styles: [
{
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [
{ "color": "#CCFFFF" }
]
}
]
}
});
Wyświetlanie informacji na mapie
Wyświetlaj dodatkowe informacje o pojeździe lub znaczniku lokalizacji za pomocą InfoWindow. Więcej informacji: InfoWindow.
Poniższy przykład pokazuje, jak utworzyć InfoWindow i dołączyć go do znacznika pojazdu:
JavaScript
// 1. Create an info window.
const infoWindow = new google.maps.InfoWindow(
{disableAutoPan: true});
locationProvider.addListener('update', e => {
const stopsCount = e.trip.remainingWaypoints.length;
infoWindow.setContent(
`Your vehicle is ${stopsCount} stops away.`);
// 2. Attach the info window to a vehicle marker.
// This property can return multiple markers.
const marker = mapView.vehicleMarkers[0];
infoWindow.open(mapView.map, marker);
});
// 3. Close the info window.
infoWindow.close();
TypeScript
// 1. Create an info window.
const infoWindow = new google.maps.InfoWindow(
{disableAutoPan: true});
locationProvider.addListener('update', (e: google.maps.journeySharing.FleetEngineTripLocationProviderUpdateEvent) => {
const stopsCount = e.trip.remainingWaypoints.length;
infoWindow.setContent(
`Your vehicle is ${stopsCount} stops away.`);
// 2. Attach the info window to a vehicle marker.
// This property can return multiple markers.
const marker = mapView.vehicleMarkers[0];
infoWindow.open(mapView.map, marker);
});
// 3. Close the info window.
infoWindow.close();
Wyłącz automatyczne dopasowywanie
Możesz wyłączyć automatyczne dopasowywanie widocznego obszaru do pojazdu i przewidywanej trasy. Poniższy przykład pokazuje, jak wyłączyć automatyczne dopasowywanie podczas konfigurowania widoku mapy udostępniania przejazdu.
JavaScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
automaticViewportMode:
google.maps.journeySharing
.AutomaticViewportMode.NONE,
...
});
TypeScript
const mapView = new
google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map_canvas'),
locationProviders: [locationProvider],
automaticViewportMode:
google.maps.journeySharing
.AutomaticViewportMode.NONE,
...
});