כדי להתאים אישית את המראה והתחושה של רכיב המפות, מעצבים את המפה באמצעות עיצוב מפות מבוסס-ענן או על ידי הגדרת אפשרויות ישירות בקוד.
עיצוב המפה באמצעות עיצוב מפות מבוסס-ענן
כדי להחיל סגנון מפה על מפה של שיתוף נתוני נסיעה ב-JavaScript, צריך לציין mapId
וmapOptions
אחרים כשיוצרים את JourneySharingMapView
.
בדוגמאות הבאות מוצגות דרכים להחלת סגנון מפה באמצעות מזהה מפה.
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.
});
עיצוב מפות ישירות בקוד שלכם
אפשר גם להתאים אישית את הסגנון של המפה על ידי הגדרת אפשרויות המפה כשיוצרים את האובייקט JourneySharingMapView
. בדוגמאות הבאות מוצגות דרכים לעיצוב מפה באמצעות אפשרויות של מפה. מידע נוסף על אפשרויות המפה שאפשר להגדיר זמין במאמר בנושא mapOptions
במאמרי העזרה של Google Maps JavaScript API.
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" }
]
}
]
}
});
השבתת ההתאמה האוטומטית
כדי למנוע מהמפה להתאים באופן אוטומטי את אזור התצוגה לרכב ולמסלול הצפוי, אפשר להשבית את ההתאמה האוטומטית. בדוגמה הבאה מוצג איך להשבית את ההתאמה האוטומטית כשמגדירים את תצוגת המפה של שיתוף המסלול.
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,
...
});
החלפת מפה קיימת
אתם יכולים להחליף מפה קיימת שכוללת סמנים או התאמות אישיות אחרות בלי לאבד את ההתאמות האישיות האלה.
לדוגמה, נניח שיש לכם דף אינטרנט עם google.maps.Map
ישות רגילה שמוצג עליה סמן:
<!DOCTYPE html>
<html>
<head>
<style>
/* Set the size of the div element that contains the map */
#map {
height: 400px; /* The height is 400 pixels */
width: 100%; /* The width is the width of the web page */
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<!--The div element for the map -->
<div id="map"></div>
<script>
// Initialize and add the map
function initMap() {
// The location of Pier 39 in San Francisco
var pier39 = {lat: 37.809326, lng: -122.409981};
// The map, initially centered at Mountain View, CA.
var map = new google.maps.Map(document.getElementById('map'));
map.setOptions({center: {lat: 37.424069, lng: -122.0916944}, zoom: 14});
// The marker, now positioned at Pier 39
var marker = new google.maps.Marker({position: pier39, map: map});
}
</script>
<!-- Load the API from the specified URL.
* The async attribute allows the browser to render the page while the API loads.
* The key parameter will contain your own API key (which is not needed for this tutorial).
* The callback parameter executes the initMap() function.
-->
<script defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
</body>
</html>
כדי להוסיף את ספריית JavaScript fleet track:
- מוסיפים קוד למפעל של אסימוני האימות.
- מאתחלים ספק מיקום בפונקציה
initMap()
. - מאתחלים את תצוגת המפה בפונקציה
initMap()
. התצוגה מכילה את המפה. - מעבירים את ההתאמה האישית לפונקציית הקריאה החוזרת לאתחול תצוגת המפה.
- מוסיפים את ספריית המיקומים לטוען ה-API.
דוגמה להחלפת מיפוי באמצעות משימות מתוזמנות
בדוגמה הבאה מוצג שימוש במפה קיימת שבה מאתחלים את אובייקט ספק המיקום לתרחיש שימוש של משימה מתוזמנת. הקוד דומה לתרחישי שימוש בנסיעות על פי דרישה, אבל משתמשים ב-FleetEngineVehicleLocationProvider
במקום ב-FleetEngineDeliveryVehicleLocationProvider
.
<!DOCTYPE html>
<html>
<head>
<style>
/* Set the size of the div element that contains the map */
#map {
height: 400px; /* The height is 400 pixels */
width: 100%; /* The width is the width of the web page */
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<!--The div element for the map -->
<div id="map"></div>
<script>
let locationProvider;
// (1) Authentication Token Fetcher
function authTokenFetcher(options) {
// options is a record containing two keys called
// serviceType and context. The developer should
// generate the correct SERVER_TOKEN_URL and request
// based on the values of these fields.
const response = await fetch(SERVER_TOKEN_URL);
if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
return {
token: data.Token,
expiresInSeconds: data.ExpiresInSeconds
};
}
// Initialize and add the map
function initMap() {
// (2) Initialize location provider. Use FleetEngineDeliveryVehicleLocationProvider
// as appropriate.
locationProvider = new google.maps.journeySharing.FleetEngineDeliveryVehicleLocationProvider({
YOUR_PROVIDER_ID,
authTokenFetcher,
});
// (3) Initialize map view (which contains the map).
const mapView = new google.maps.journeySharing.JourneySharingMapView({
element: document.getElementById('map'),
locationProviders: [locationProvider],
// any styling options
});
mapView.addListener('ready', () => {
locationProvider.deliveryVehicleId = DELIVERY_VEHICLE_ID;
// (4) Add customizations like before.
// The location of Pier 39 in San Francisco
var pier39 = {lat: 37.809326, lng: -122.409981};
// The map, initially centered at Mountain View, CA.
var map = mapView.map;
map.setOptions({center: {lat: 37.424069, lng: -122.0916944}, zoom: 14});
// The marker, now positioned at Pier 39
var marker = new google.maps.Marker({position: pier39, map: map});
};
}
</script>
<!-- Load the API from the specified URL
* The async attribute allows the browser to render the page while the API loads
* The key parameter will contain your own API key (which is not needed for this tutorial)
* The callback parameter executes the initMap() function
*
* (5) Add the journey sharing library to the API loader, which includes Fleet Tracking functionality.
-->
<script defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=journeySharing">
</script>
</body>
</html>
אם אתם מפעילים רכב משלוחים עם המזהה שצוין בקרבת מזח 39, הרכב יוצג עכשיו במפה.