JSFiddle - React, Tailwind, and code Playground
HTML
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>
CSS
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
var routeMapArray = [];
var map;
var defaultZoom = 11;
var directionsService;
var directionDisplayRenderEngineArray = [];
var iconColor = '';
function initMap() {
routeMapArray = [];
directionsService = new google.maps.DirectionsService();
// center initially on NYC
loadMap(new google.maps.LatLng(40.7128, -74.0060));
}
var loadMap = function(center) {
var mapOptions = {
zoom: 11,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
if (center !== undefined && center !== null) {
mapOptions.center = center;
}
// Some basic map setup (from the API docs)
map = new google.maps.Map(document.getElementById('map'), mapOptions);
// Start the request making
var requestArray = [{
"origin": "New York, NY",
"destination": "Seattle, WA",
"waypoints": [{
"location": "Detroit, MI",
"stopover": true
},
{
"location": "Denver, CO",
"stopover": true
}
],
"travelMode": "DRIVING"
},
{
"origin": "Boston, MA",
"destination": "San Diego, CA",
"waypoints": [{
"location": "Dayton, OH",
"stopover": true
},
{
"location": "Las Vegas, NV",
"stopover": true
}
],
"travelMode": "DRIVING"
}
];
processRequests(requestArray);
};
var processRequests = function(requestArray) {
var bounds = new google.maps.LatLngBounds();
var infoWindow = new google.maps.InfoWindow();
// Counter to track request submission and process one at a time;
var i = 0;
var position = "";
// Used to submit the request 'i'
var submitRequest = function() {
if (requestArray.length === 0) return;
directionsService.route(requestArray[i], processDirectionResults);
};
// Used as callback for the above request for current 'i'
function processDirectionResults(result, status) {
if (status ===...