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
| <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>
Google Maps JavaScript API v3 Example: Directions Waypoints</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var geocoder;
var gl_start;
var gl_end;
function initialize() {
geocoder = new google.maps.Geocoder();
directionsDisplay = new google.maps.DirectionsRenderer();
var kiev = new google.maps.LatLng(49.9935,36.230383);
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: kiev
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
gl_start = null;
gl_end = null;
var start = document.getElementById("start").value;
geocoder.geocode( { 'address': start}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
gl_start = results[0].geometry.location;
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
checkArgs();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
var end = document.getElementById("end").value;
geocoder.geocode( { 'address': end}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
gl_end = results[0].geometry.location;
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
checkArgs();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function checkArgs () {
if (gl_start == null || gl_end == null) {
return;
}
var waypts = [];
var request = {
origin: gl_start,
destination: gl_end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, 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>Расстояние: " + routeSegment + "</b><br />";
summaryPanel.innerHTML += route.legs[i].start_address + " to ";
summaryPanel.innerHTML += route.legs[i].end_address + "<br />";
summaryPanel.innerHTML += route.legs[i].distance.text + "<br /> <br />";
summaryPanel.innerHTML += route.legs[i].distance.text + "<br /> <br />";
var cena = parseInt(route.legs[i].distance.text)*15;
summaryPanel.innerHTML += "объём " + cena.toFixed(0) + "лит.<br />";
}
}
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="float:left;width:70%;height:100%;">
</div>
<div id="control_panel" style="float:right;width:30%;text-align:left;padding-top:20px">
<div style="margin:20px;border-width:2px;">
<b>Откуда:</b>
<input type="text" id="start" value="Тула" />
<br />
<b>Куда:</b>
<input type="text" id="end" value="Рязань" />
<br />
<input type="submit" onclick="calcRoute();"
value="путь"; />
</div>
<div id="directions_panel" style="margin:40px;background-color:#ffffff;"></div>
</div>
</body>
</html> |