JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script>
<div id="map"></div>
CSS
#map {
width: 500px;
height: 500px;
}
JavaScript
var map;
var directionsService;
function loadRoute1() {
var request = {
origin: new google.maps.LatLng(30.244517, -97.892271),
destination: new google.maps.LatLng(30.244517, -97.892271),
waypoints: [
{ location: new google.maps.LatLng(30.241532, -97.894202) },
{ location: new google.maps.LatLng(30.239549, -97.894567) }
],
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
result.draggable = true;
var renderer = new google.maps.DirectionsRenderer({
draggable: true,
polylineOptions: { strokeColor: "#DD71D8" },
map: map
});
renderer.setDirections(result);
}
});
}
function loadRoute2() {
var request = {
origin: new google.maps.LatLng(30.244220, -97.890426),
destination: new google.maps.LatLng(30.244220, -97.890426),
waypoints: [
{ location: new google.maps.LatLng(30.243312, -97.890877) },
{ location: new google.maps.LatLng(30.242431, -97.891601 ) },
{ location: new google.maps.LatLng(30.243145, -97.893156) },
{ location: new google.maps.LatLng(30.242357, -97.893811) },
{ location: new google.maps.LatLng(30.241671, -97.891783) }
],
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
result.draggable = true;
var renderer = new google.maps.DirectionsRenderer({
draggable: true,
polylineOptions: { strokeColor: "#0000ff" },
map: map
});
renderer.setDirections(result);
}
});
}
function initialize() {
var mapOptions = {
zoom: 16,
draggable: true,
center: { lat: 30.241532, lng: -97.894202 }
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
directionsService = new google.maps.DirectionsService();
loadRoute1();
loadRoute2();
...