-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
227 lines (179 loc) · 6.84 KB
/
index.php
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
<!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>NoRoadBlockMtl</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 src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
//Set the path generated by Google as modifiable on the map by the user
var rendererOptions = {
draggable: true
};
//Function called when loading the page
function initialize() {
//Define the map
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
var myCentre = new google.maps.LatLng(45.50, -73.55);
var myOptions = {
zoom:9,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: myCentre
}
//Set the DIV to be used to display the Google Map
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
//Listener to track the changes in the path
//Disabled for the moment, it seems to create an infinite loop somewhere...
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
//calcRoute();
processRoute(directionsDisplay.directions);
});
}
function calcRoute() {
//Get the start and end point from the form
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
//Prepare and send request to Google
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
var mapBounds = map.getBounds();
//alert (mapBounds);
var northEast = mapBounds.getNorthEast();
var southWest = mapBounds.getSouthWest();
var neLat = northEast.lat();
var neLon = northEast.lng();
var swLat = southWest.lat();
var swLon = southWest.lng();
var url = "http://96.21.124.183:2089/NoRoadBlockMtl/api/works.json?q=toto";
// var query = "?nelat=" + neLat + "&nelon=" + neLon + "&swlat=" + swLat + "&swlon=" + swLon;
// alert(url + query);
$.getJSON(url,"",
function(data) {
for (i in data){
var travaux = [];
//alert(data[i]["name"]);
for (j in data[i]["coordinates"]){
var splited = data[i]["coordinates"][j].split(" ");
var pointpassage = new google.maps.LatLng(splited[1], splited[0]);
travaux.push(pointpassage);
// alert(formated);
}
//alert("Taille du array" + travaux.length);
var marker = new google.maps.Marker({
position: travaux[0],
map: map,
});
if (travaux.length > 1){
var zeLigne = new google.maps.Polyline({
path: travaux,
strokeColor: "#FF0000",
strokeOpacity: 0.5,
strokeWeight: 6
});
zeLigne.setMap(map);
}
}
});
}
function processRoute (result) {
//Retrieve and format the path. Target format is "long1 lat1, long2 lat2, ..."
var fullString = "";
var myRoute = result.routes[0].overview_path;
for (var i = 0; i < myRoute.length; i++) {
var exploded = (myRoute[i] + ",").split(",");
fullString += exploded[1].replace(")", "").substring(1, 11) + " " + exploded[0].replace("(", "").substring(0, 10) + ",";
/*
//Small code to print all the points of the path on the map.
var marker = new google.maps.Marker({position: myRoute[i], map: map});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});*/
}
fullString = fullString.replace(")", "");
fullString = fullString.replace("(", "");
//Set the value of an hidden form field to be sent to the server.
var postValue = document.getElementById('hiddenpath');
postValue.value = fullString.substring(0, fullString.length - 2);
}
function postNewRoute () {
myObject = new Object();
var postValue = document.getElementById('hiddenpath');
myObject.route = postValue.value;
myObject.userid = document.getElementById('userid').value;
myObject.name = document.getElementById('routename').value;
var theData = JSON.stringify(myObject);
alert(theData);
$.post('http://96.21.124.183:2089/NoRoadBlockMtl/api/routes.json', { data: theData});
}
function getListOfRoutes () {
var userid = document.getElementById('getuserid').value;
document.getElementById('listroutes').innerHTML = ' ';
document.getElementById('listtravaux').innerHTML = ' ';
$.getJSON('http://96.21.124.183:2089/NoRoadBlockMtl/api/routes.json?userid=' + userid, function(data) {
var futureHTML = "";
for (i in data){
var name = data[i]["name"];
futureHTML += "<p>" + name + "("+ data[i]["id"] +")<br/>";
futureHTML += '<input type="button" value="Check roadwork on this route" onclick="getWorkWithId('+ data[i]["id"] +')"/>';
futureHTML += "</p>";
//alert(name);
}
document.getElementById('listroutes').innerHTML = futureHTML;
});
}
function getWorkWithId (workId) {
document.getElementById('listroutes').innerHTML = ' ';
document.getElementById('listtravaux').innerHTML = ' ';
$.getJSON('http://96.21.124.183:2089/NoRoadBlockMtl/api/works.json?routeid=' + workId , function(data) {
var futureHTML = "";
for (i in data){
var name = data[i]["name"];
futureHTML += "<p>" + name + "<br/>";
futureHTML += "</p>";
//alert(name);
}
document.getElementById('listtravaux').innerHTML = futureHTML;
});
}
</script>
</head>
<body onload="initialize()">
<div>
<b>Start: </b> <input id="start" type="text" name="start" value="Montréal, qc"/>
<b>End: </b> <input id="end" type="text" name="end" value="Laval, qc"/>
<input type="button" value="Trace my route" onclick="calcRoute();"/>
<hr/>
<form action="postme.php" method="post">
<input type="hidden" name="hiddenpath" id="hiddenpath" value=""/>
<input type="hidden" name="distance" id="distance" value="distance" />
User id (for insert) : <input type="text" name="userid" id="userid" value="1" /><br/>
Name of the route : <input type="text" name="routename" id="routename" value="Montreal to Laval" /><br/>
<input type="button" value="Submit My Route" onclick="postNewRoute()"/>
</form>
<hr/>
</div>
<div> </div>
<div id="leftone" style="width:30%; float:left">
Get list of routes for user
<input type="text" name="getuserid" id="getuserid" size="2" value="1">
<input type="button" value="Get the list!" onclick="getListOfRoutes()"/>
<div id="listroutes"></div>
<div id="listtravaux"></div>
</div>
<div style="width:68%; height:500px" id="map_canvas"></div>
</body>
</html>