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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Trajet avec markers</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
var EarthRadiusMeters = 6378137.0; // meters
var lat1 = this.lat();
var lon1 = this.lng();
var lat2 = newLatLng.lat();
var lon2 = newLatLng.lng();
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = EarthRadiusMeters * c;
return d;
}
</script>
<script type="text/javascript">
var mArray = new Array();
var points = new Array();
var coordinates = new Array();
var markers = new Array();
var map;
var bounds;
var infowindow = new google.maps.InfoWindow({size: new google.maps.Size(150, 50)});
var resultRequest;
var directionDisplay = new google.maps.DirectionsRenderer();
function estVide(nom) {
if (nom !== undefined && nom !== null && nom !== '' && nom !== false) {
return false;
}
return true;
}
function load() {
var myOptions = {
zoom: 7,
center: new google.maps.LatLng(48, 1),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
calculItineraire();
}
function calculItineraire() {
var rendererOptions = {
draggable: false,
suppressInfoWindows: false,
suppressMarkers: true,
suppressPolylines: false,
};
directionDisplay.setMap(null);
directionDisplay = new google.maps.DirectionsRenderer(rendererOptions);
var request = {
origin: document.getElementById('start').value,
destination: document.getElementById('end').value,
unitSystem: google.maps.DirectionsUnitSystem.METRIC,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
};
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(result, status) {
if (status === google.maps.DirectionsStatus.OK) {
resultRequest = result.routes[0];
directionDisplay.setDirections(result);
directionDisplay.setMap(map);
putMarkersDistance();
}
});
}
function getPointAtDistance(metres) {
var myroute = resultRequest;
var arr = new google.maps.MVCArray(myroute.overview_path);
if (metres == 0)
return arr.getAt(0);
if (metres < 0)
return null;
if (arr.getLength() < 2)
return null;
var dist = 0;
var olddist = 0;
for (var i = 1; (i < arr.getLength() && dist < metres); i++) {
olddist = dist;
dist += arr.getAt(i).distanceFrom(arr.getAt(i - 1));
}
if (dist < metres) {
return null;
}
var p1 = arr.getAt(i - 2);
var p2 = arr.getAt(i - 1);
var m = (metres - olddist) / (dist - olddist);
return new google.maps.LatLng(p1.lat() + (p2.lat() - p1.lat()) * m, p1.lng() + (p2.lng() - p1.lng()) * m);
}
function createMarker(point, html) {
var contentString = html;
var marker = new google.maps.Marker({
position: point,
map: map,
title: name,
zIndex: Math.round(point.lat() * -100000) << 5
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map, marker);
});
return marker;
}
function putMarkerDistance(distance) {
var km_point = getPointAtDistance(distance);
if (km_point) {
var infoWindowContent = "Kilomètre " + distance / 1000 + " sur " + (resultRequest.legs[0].distance.value / 1000).toFixed(2);
var marker = createMarker(km_point, infoWindowContent);
markers.push(marker);
}
}
function putMarkersDistance() {
deleteMarkers();
var increment = parseInt(document.getElementById('distanceMarker').value * 1000);
var distance = resultRequest.legs[0].distance.value;
for (var i = 0; i < distance; i += increment) {
putMarkerDistance(i);
}
}
function deleteMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers = new Array();
}
</script>
</head>
<body onload="load()" >
<div id="map_canvas" style="width: 1000px; height: 500px; margin-bottom: 40px;"></div>
<form action="get" method="post">
<label>Départ</label><input type="text" name="start" id="start" value="Paris" />
<label>Arrivée</label><input type="text" name="end" id="end" value="Nantes" />
<label>Marker tous les : </label><input type="text" name="distanceMarker" id="distanceMarker" value="25" size="4"/> kms
<input type="button" name="Reset" value="Envoyer" onclick="calculItineraire()" />
<input type="button" name="Reset" value="Effacer" onclick="deleteMarkers()" />
</form>
</body>
</html> |
Partager