<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:view contentType="text/html" />
<h:head>
<title>Mapa</title>
<h:outputStylesheet library="css" name="mapa.css" />
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=Minha key">//
</script>
<script>
function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: {lat: -3.050764, lng: -59.943232}
});
directionsDisplay.setMap(map);
document.getElementById('submit').addEventListener('click', function() {
calculateAndDisplayRoute(directionsService, directionsDisplay);
});
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var waypts = [];
var checkboxArray = document.getElementById('waypoints');
for (var i = 0; i < checkboxArray.length; i++) {
//if (checkboxArray.options[i].selected)
{
waypts.push({
location: checkboxArray[i].value,
stopover: true
});
}
}
directionsService.route({
origin: document.getElementById('start').value,
destination: document.getElementById('end').value,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var summaryPanel = document.getElementById('directions-panel');
summaryPanel.innerHTML = '';
// For each route, display summary information.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += '<b>Trajeto da Rota: ' + routeSegment +
'</b><br>';
summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
summaryPanel.innerHTML += route.legs[i].duration.text + '<br>';
summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
}
} else {
window.alert('Erro ao gerar rota, motivo: ' + status);
}
});
}
</script>
</h:head>
<body onload="initMap()">
<div id="map"></div>
<div id="right-panel">
<div>
<b>Inicio da rota:</b>
<h:selectOneMenu id="start">
<f:selectItem
itemValue="Rua Nossa Senhora da Conceicao, Tancredo, Manaus, AM"
itemLabel="Cólegio" />
</h:selectOneMenu>
<br /> <b />Alunos: <br />
<h:selectManyMenu id="waypoints">
<f:selectItems value="#{alunoMB.listaAluno}" var="item"
itemValue="#{item.rua}" itemLabel="#{item.nome}" />
</h:selectManyMenu>
<br /> <b />Fim da rota:<b />
<h:selectOneMenu id="end">
<f:selectItem
itemValue="Rua Nossa Senhora da Conceicao, Tancredo, Manaus, AM"
itemLabel="Cólegio" />
</h:selectOneMenu>
<h:commandButton type="submit" value="Calcular Rota" id="submit" />
</div>
<div id="directions-panel"></div>
</div>
</body>
</html>
![]()