1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| var rendererOptions = {
draggable: true
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize_form_map(){
var latLng = new google.maps.LatLng(48.8592940, 2.3490143); // Correspond au coordonnées de Paris
var myOptions = {
zoom : 10,
center : latLng,
mapTypeId : google.maps.MapTypeId.MAP, // Type de carte, MAP
maxZoom : 20
};
map=new google.maps.Map(document.getElementById('map'), myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('panel'));
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
computeTotalDistance(directionsDisplay.directions);
});
calculate();
};
calculate = function(){
origin = document.getElementById('begin_ad').value; // Le point départ
destination = document.getElementById('end_ad').value; // Le point d'arrivé
if(origin && destination){
var request = {
origin : origin,
destination : destination,
travelMode : google.maps.DirectionsTravelMode.DRIVING // Mode de conduite
}
// Service de calcul d'itinéraire de lAPI google
directionsService.route(request, function(reponse, status){
// Envoie de la requête pour calculer le parcours
if(status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(reponse);
itineraireAffichage.setDirections(reponse);
}
});
}
};
//Calcul la distannce en kilométre du trajet
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000.
document.getElementById('total').innerHTML = total + ' km';
} |
Partager