-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCooLMAX2.html
228 lines (212 loc) · 9.7 KB
/
CooLMAX2.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Find Near Route Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
html
{
overflow:hidden;
font-family: Verdana;
font-size: 12px;
width:100%;
height:100%;
}
body
{
width:100%;
height:100%;
margin:0 0 0 0;
}
</style>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
window.onload = GetMap;
window.onresize = Resize;
var bmKey = "Al_MYHnzKW2V2HbZSprQ5i9C8SeZE5lvx5q9E_WtSAoKRJCg666TgjpeHYCyfED1";
var map = null;
var MM = Microsoft.Maps;
var directionsManager;
var directionsErrorEventObj;
var directionsUpdatedEventObj;
var trafficFlowVisible = false;
var trafficFlowLayer = null;
var trafficIncidentLayer = null;
var infoBoxLayer = null;
var startLat = null;
var startLon = null;
var endLat = null;
var endLon = null;
function GetMap() {
map = new MM.Map(document.getElementById('myMap'), {
credentials: bmKey,
mapTypeId: MM.MapTypeId.road,
bounds: MM.LocationRect.fromCorners(new MM.Location(49.0, -125.0), new MM.Location(25.0, -80.0)),
enableClickableLogo: false,
enableSearchLogo: false
});
Resize();
}
function createDirectionsManager() {
var displayMessage;
if (!directionsManager) {
directionsManager = new MM.Directions.DirectionsManager(map);
}
directionsManager.resetDirections();
directionsErrorEventObj = MM.Events.addHandler(directionsManager, 'directionsError', function (arg) { alert(arg.message) });
directionsUpdatedEventObj = MM.Events.addHandler(directionsManager, 'directionsUpdated', FindTrafficIncidentsNearRoute );
}
function createDrivingRoute() {
if (!directionsManager) { createDirectionsManager(); }
var options = directionsManager.getRequestOptions();
options.routeDraggable = false;
directionsManager.resetDirections();
directionsManager.setRequestOptions({ routeMode: MM.Directions.RouteMode.driving });
var myStart = new MM.Directions.Waypoint({ address: document.getElementById('txtStart').value });
directionsManager.addWaypoint(myStart);
var myEnd = new MM.Directions.Waypoint({ address: document.getElementById('txtEnd').value });
directionsManager.addWaypoint(myEnd);
directionsManager.setRequestOptions(options);
directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') });
directionsManager.calculateDirections();
}
function createDirections() {
if (!directionsManager) {
MM.loadModule('Microsoft.Maps.Directions', { callback: createDrivingRoute });
}
else {
createDrivingRoute();
}
}
function Resize() {
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
document.getElementById('directionsItinerary').style.height = (windowHeight - 155) + "px";
document.getElementById('myMap').style.width = (windowWidth - 310) + "px";
document.getElementById('myMap').style.height = (windowHeight - 10) + "px";
}
function ToggleTraffic() {
if (trafficFlowVisible == false) {
MM.loadModule('Microsoft.Maps.Traffic', {
callback: function () {
trafficFlowLayer = new MM.Traffic.TrafficLayer(map);
trafficFlowLayer.show();
}
});
trafficFlowVisible = true;
}
else {
trafficFlowLayer.hide();
trafficFlowVisible = false;
}
}
function FindTrafficIncidentsNearRoute(result) {
startLat = result.route[0].routeLegs[0].startWaypointLocation.latitude;
startLon = result.route[0].routeLegs[0].startWaypointLocation.longitude;
endLat = result.route[0].routeLegs[0].endWaypointLocation.latitude;
endLon = result.route[0].routeLegs[0].endWaypointLocation.longitude;
map.getCredentials(TrafficIncidentRequest);
}
function TrafficIncidentRequest(credentials) {
var request = "https://spatial.virtualearth.net/REST/v1/data/422c1524e82547218d6ea378a18ac535/Entities1/Entity?" +
"key=" + credentials +
"&$format=json" +
"&jsonp=callbackFindTrafficIncidentsNearRoute" +
"&spatialFilter=nearroute('" +
startLat + "," +
startLon + "','" +
endLat + "," +
endLon + "')";
CallRestService(request);
}
function callbackFindTrafficIncidentsNearRoute(result) {
if (result &&
result.d &&
result.d.results) {
try {
try {
trafficIncidentLayer.clear();
infoBoxLayer.clear();
}
catch (err) {
trafficIncidentLayer = new MM.EntityCollection;
infoBoxLayer = new MM.EntityCollection;
}
for (i = 0; i < result.d.results.length; i++) {
switch (result.d.results[i].Severity) {
case 'LowImpact':
var pushpinOptions = { icon: 'green.png', height: 30, width: 30, anchor: new MM.Point(15, 15) };
break;
case 'Minor':
var pushpinOptions = { icon: 'green.png', height: 30, width: 30, anchor: new MM.Point(15, 15) };
break;
case 'Moderate':
var pushpinOptions = { icon: 'green.png', height: 30, width: 30, anchor: new MM.Point(15, 15) };
break;
case 'Serious':
var pushpinOptions = { icon: 'red.png', height: 30, width: 30, anchor: new MM.Point(15, 15) };
break;
}
var pushpin = new MM.Pushpin(new MM.Location(result.d.results[i].Latitude, result.d.results[i].Longitude), pushpinOptions);
MM.Pushpin.prototype.title = null;
pushpin.title = result.d.results[i].Type;
MM.Pushpin.prototype.description = null;
pushpin.description = result.d.results[i].Description + '<br><br>' +
'From: ' + result.d.results[i].StartTimeUTC + '<br>' +
'To: ' + result.d.results[i].EndTimeUTC + '<br>' +
'Severity: ' + result.d.results[i].Severity;
pushpinClick = MM.Events.addHandler(pushpin, 'click', showInfoBox);
trafficIncidentLayer.push(pushpin);
}
map.entities.push(trafficIncidentLayer);
map.entities.push(infoBoxLayer);
}
catch (err) {
}
var script;
while (script = document.getElementById('JSONP')) {
script.parentNode.removeChild(script);
}
}
else {
alert('error');
}
}
function showInfoBox(e) {
if (e.targetType = "pushpin") {
infoBoxLayer.clear();
infobox = new MM.Infobox(e.target.getLocation(), { title: e.target.title, description: e.target.description });
infoBoxLayer.push(infobox);
}
}
function CallRestService(request) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
document.body.appendChild(script);
}
</script>
</head>
<body>
<div style='position:absolute; top:2px; left:5px; width:300px; height:300px;'>
<table style='width:100%;'>
<tr>
<td style='width:70px;'>Start</td>
<td><input id="txtStart" type="text" style='width:230px;'/></td>
</tr>
<tr>
<td style='width:70px;'>End</td>
<td><input id="txtEnd" type="text" style='width:230px;'/></td>
</tr>
<tr>
<td colspan='2'><input type="button" value="Get Directions" onclick="createDirections();" style="width:295px" /></td>
</tr>
<tr>
<td colspan='2'><input type="button" value="Toggle Traffic Flow" onclick="ToggleTraffic();" style="width:295px" /></td>
</tr>
</table>
</div>
<div id='directionsItinerary' style='position:absolute; top:150px; left:5px; width:295px; height:300px; overflow-y:auto;'></div>
<div id='myMap' style="position:absolute; top:5px; left:305px; width:400px; height:400px;"></div>
</body>
</html>