Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/web/www/routino/router.openlayers.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1260 - (hide annotations) (download) (as text)
Tue Feb 26 18:27:14 2013 UTC (12 years, 1 month ago) by amb
Original Path: trunk/web/www/routino/router.js
File MIME type: application/javascript
File size: 47553 byte(s)
Clarify comment.

1 amb 569 //
2     // Routino router web page Javascript
3     //
4     // Part of the Routino routing software.
5     //
6 amb 1239 // This file Copyright 2008-2013 Andrew M. Bishop
7 amb 569 //
8     // This program is free software: you can redistribute it and/or modify
9     // it under the terms of the GNU Affero General Public License as published by
10     // the Free Software Foundation, either version 3 of the License, or
11     // (at your option) any later version.
12     //
13     // This program is distributed in the hope that it will be useful,
14     // but WITHOUT ANY WARRANTY; without even the implied warranty of
15     // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     // GNU Affero General Public License for more details.
17     //
18     // You should have received a copy of the GNU Affero General Public License
19     // along with this program. If not, see <http://www.gnu.org/licenses/>.
20     //
21    
22 amb 574
23 amb 987 var vismarkers, markers, markersmoved, paramschanged;
24 amb 985 var homelat=null, homelon=null;
25    
26    
27     ////////////////////////////////////////////////////////////////////////////////
28     /////////////////////////////// Initialisation /////////////////////////////////
29     ////////////////////////////////////////////////////////////////////////////////
30    
31 amb 577 // Make a deep copy of the routino profile.
32 amb 569
33 amb 577 var routino_default={};
34     for(var l1 in routino)
35     if(typeof(routino[l1])!='object')
36     routino_default[l1]=routino[l1];
37     else
38     {
39     routino_default[l1]={};
40     for(var l2 in routino[l1])
41     if(typeof(routino[l1][l2])!='object')
42     routino_default[l1][l2]=Number(routino[l1][l2]);
43     else
44     {
45     routino_default[l1][l2]={};
46     for(var l3 in routino[l1][l2])
47     routino_default[l1][l2][l3]=Number(routino[l1][l2][l3]);
48     }
49     }
50    
51 amb 985 // Store the latitude and longitude in the routino variable
52 amb 577
53 amb 985 routino.point=[];
54 amb 1012 for(var marker=1;marker<=mapprops.maxmarkers;marker++)
55 amb 985 {
56     routino.point[marker]={};
57    
58     routino.point[marker].lon="";
59     routino.point[marker].lat="";
60 amb 1001 routino.point[marker].search="";
61 amb 985 routino.point[marker].active=false;
62 amb 1034 routino.point[marker].used=false;
63 amb 1036 routino.point[marker].home=false;
64 amb 985 }
65    
66     // Process the URL query string and extract the arguments
67    
68     var legal={"^lon" : "^[-0-9.]+$",
69     "^lat" : "^[-0-9.]+$",
70     "^zoom" : "^[0-9]+$",
71    
72     "^lon[1-9]" : "^[-0-9.]+$",
73     "^lat[1-9]" : "^[-0-9.]+$",
74 amb 1001 "^search[1-9]" : "^.+$",
75 amb 985 "^transport" : "^[a-z]+$",
76     "^highway-[a-z]+" : "^[0-9.]+$",
77     "^speed-[a-z]+" : "^[0-9.]+$",
78     "^property-[a-z]+" : "^[0-9.]+$",
79     "^oneway" : "^(1|0|true|false|on|off)$",
80     "^turns" : "^(1|0|true|false|on|off)$",
81     "^weight" : "^[0-9.]+$",
82     "^height" : "^[0-9.]+$",
83     "^width" : "^[0-9.]+$",
84     "^length" : "^[0-9.]+$",
85    
86     "^language" : "^[-a-zA-Z]+$"};
87    
88     var args={};
89    
90     if(location.search.length>1)
91     {
92     var query,queries;
93    
94     query=location.search.replace(/^\?/,"");
95     query=query.replace(/;/g,'&');
96     queries=query.split('&');
97    
98     for(var i=0;i<queries.length;i++)
99     {
100     queries[i].match(/^([^=]+)(=(.*))?$/);
101    
102     k=RegExp.$1;
103     v=unescape(RegExp.$3);
104    
105     for(var l in legal)
106     {
107     if(k.match(RegExp(l)) && v.match(RegExp(legal[l])))
108     args[k]=v;
109     }
110     }
111     }
112    
113    
114 amb 987 //
115     // Fill in the HTML - add the missing waypoints
116     //
117    
118 amb 1035 function html_init() // called from router.html
119 amb 987 {
120     var waypoints=document.getElementById("waypoints");
121 amb 1005
122 amb 987 var waypoint_html=waypoints.rows[0].innerHTML;
123 amb 1005 waypoints.deleteRow(0);
124 amb 987
125 amb 1005 var searchresults_html=waypoints.rows[0].innerHTML;
126 amb 987 waypoints.deleteRow(0);
127    
128 amb 1012 for(var marker=mapprops.maxmarkers;marker>=1;marker--)
129 amb 987 {
130 amb 1005 var searchresults=waypoints.insertRow(0);
131 amb 1011
132 amb 1037 searchresults.style.display="none";
133 amb 1005 searchresults.id="searchresults" + marker;
134 amb 1011 searchresults.innerHTML=searchresults_html.split('XXX').join(marker);
135 amb 987
136 amb 1011 var waypoint=waypoints.insertRow(0);
137 amb 1005
138 amb 1037 waypoint.style.display="none";
139 amb 1005 waypoint.id="waypoint" + marker;
140 amb 1011 waypoint.innerHTML=waypoint_html.split('XXX').join(marker);
141 amb 987 }
142     }
143    
144    
145 amb 577 ////////////////////////////////////////////////////////////////////////////////
146     //////////////////////////////// Form handling /////////////////////////////////
147     ////////////////////////////////////////////////////////////////////////////////
148    
149 amb 569 //
150     // Form initialisation - fill in the uninitialised parts
151     //
152    
153 amb 1035 function form_init() // called from router.html
154 amb 569 {
155 amb 985 // Fill in the waypoints
156 amb 569
157 amb 1039 vismarkers=0;
158 amb 569
159 amb 1012 for(var marker=mapprops.maxmarkers;marker>=1;marker--)
160 amb 569 {
161 amb 985 var lon=args["lon" + marker];
162     var lat=args["lat" + marker];
163 amb 1001 var search=args["search" + marker];
164 amb 577
165 amb 1001 if(lon != undefined && lat != undefined && search != undefined && lon != "" && lat != "" && search != "")
166 amb 569 {
167 amb 1037 markerAddForm(marker);
168    
169 amb 1001 formSetSearch(marker,search);
170 amb 1032 formSetCoords(marker,lon,lat);
171 amb 569
172 amb 1032 markerAddMap(marker);
173    
174 amb 1001 markerSearch(marker);
175    
176 amb 1039 vismarkers++;
177 amb 569 }
178 amb 1001 else if(lon != undefined && lat != undefined && lon != "" && lat != "")
179     {
180 amb 1037 markerAddForm(marker);
181    
182 amb 1032 formSetCoords(marker,lon,lat);
183 amb 1001
184 amb 1032 markerAddMap(marker);
185    
186 amb 1001 markerCoords(marker);
187    
188 amb 1039 vismarkers++;
189 amb 1001 }
190     else if(search != undefined && search != "")
191     {
192 amb 1037 markerAddForm(marker);
193    
194 amb 1001 formSetSearch(marker,search);
195    
196     markerSearch(marker);
197    
198     DoSearch(marker);
199    
200 amb 1039 vismarkers++;
201 amb 1001 }
202 amb 1039 else if(vismarkers || marker<=2)
203     {
204 amb 1037 markerAddForm(marker);
205 amb 1016
206 amb 1039 vismarkers++;
207     }
208    
209 amb 1016 var searchfield=document.forms["form"].elements["search" + marker];
210    
211     if(searchfield.addEventListener)
212     searchfield.addEventListener('keyup', searchOnReturnKey, false);
213     else if(searchfield.attachEvent)
214     searchfield.attachEvent('keyup', searchOnReturnKey); // Internet Explorer
215 amb 985 }
216 amb 569
217 amb 985 // Update the transport type with the URL settings which updates all HTML forms to defaults.
218 amb 574
219 amb 985 var transport=routino.transport;
220 amb 569
221 amb 985 if(args["transport"] != undefined)
222     transport=args["transport"];
223 amb 577
224 amb 985 formSetTransport(transport);
225 amb 577
226 amb 985 // Update the HTML with the URL settings
227    
228     if(args["language"] != undefined)
229     formSetLanguage(args["language"]);
230    
231     for(var key in routino.profile_highway)
232     if(args["highway-" + key] != undefined)
233     formSetHighway(key,args["highway-" + key]);
234    
235     for(var key in routino.profile_speed)
236     if(args["speed-" + key] != undefined)
237     formSetSpeed(key,args["speed-" + key]);
238    
239     for(var key in routino.profile_property)
240     if(args["property-" + key] != undefined)
241     formSetProperty(key,args["property-" + key]);
242    
243     for(var key in routino.restrictions)
244 amb 574 {
245 amb 985 if(key=="oneway" || key=="turns")
246 amb 577 {
247 amb 985 if(args[key] != undefined)
248     formSetRestriction(key,args[key]);
249 amb 577 }
250 amb 985 else
251     {
252     if(args["restrict-" + key] != undefined)
253     formSetRestriction(key,args["restrict-" + key]);
254     }
255 amb 574 }
256 amb 569
257 amb 577 // Get the home location cookie and compare to each waypoint
258    
259     var cookies=document.cookie.split('; ');
260    
261     for(var cookie=0;cookie<cookies.length;cookie++)
262     if(cookies[cookie].substr(0,"Routino-home".length)=="Routino-home")
263     {
264     var data=cookies[cookie].split(/[=:;]/);
265    
266     if(data[1]=="lon") homelon=Number(data[2]);
267     if(data[3]=="lat") homelat=Number(data[4]);
268     }
269    
270     if(homelon!=null && homelat!=null)
271     {
272 amb 1039 for(var m=1;m<=vismarkers;m++)
273     markerCheckHome(m);
274 amb 577
275     // If the first location is empty and the cookie is set then fill it.
276    
277 amb 1034 if(!routino.point[1].used)
278 amb 1036 markerMoveHome(1);
279 amb 577 }
280 amb 569 }
281    
282    
283     //
284 amb 1016 // Function to perform the search if the return key is pressed.
285     // (using 'onchange' only triggers once and is confusing when clicking outside the field).
286     //
287    
288     function searchOnReturnKey(ev)
289     {
290     if(ev.keyCode==13)
291     if(this.name.match(/^search([0-9]+)$/))
292     formSetSearch(RegExp.$1);
293    
294     return(true);
295     }
296    
297    
298     //
299 amb 577 // Change of language in the form
300     //
301    
302 amb 1035 function formSetLanguage(value) // called from router.html (with no arguments)
303 amb 577 {
304 amb 985 if(value == undefined)
305     {
306     for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++)
307     if(document.forms["form"].elements["language"][lang].checked)
308     routino.language=document.forms["form"].elements["language"][lang].value;
309     }
310     else
311     {
312     for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++)
313     if(document.forms["form"].elements["language"][lang].value==value)
314     document.forms["form"].elements["language"][lang].checked=true;
315     else
316     document.forms["form"].elements["language"][lang].checked=false;
317 amb 577
318 amb 985 routino.language=value;
319     }
320 amb 577 }
321    
322    
323     //
324 amb 569 // Change of transport in the form
325     //
326    
327 amb 1035 function formSetTransport(value) // called from router.html
328 amb 569 {
329 amb 985 routino.transport=value;
330 amb 569
331 amb 577 for(var key in routino.transports)
332     document.forms["form"].elements["transport"][routino.transports[key]-1].checked=(key==routino.transport);
333 amb 569
334 amb 577 for(var key in routino.profile_highway)
335     document.forms["form"].elements["highway-" + key].value=routino.profile_highway[key][routino.transport];
336 amb 569
337 amb 577 for(var key in routino.profile_speed)
338     document.forms["form"].elements["speed-" + key].value=routino.profile_speed[key][routino.transport];
339 amb 569
340 amb 577 for(var key in routino.profile_property)
341     document.forms["form"].elements["property-" + key].value=routino.profile_property[key][routino.transport];
342 amb 569
343 amb 577 for(var key in routino.restrictions)
344 amb 569 {
345 amb 622 if(key=="oneway" || key=="turns")
346 amb 577 document.forms["form"].elements["restrict-" + key].checked=routino.profile_restrictions[key][routino.transport];
347 amb 569 else
348 amb 577 document.forms["form"].elements["restrict-" + key].value=routino.profile_restrictions[key][routino.transport];
349 amb 569 }
350    
351     paramschanged=true;
352     }
353    
354    
355     //
356     // Change of highway in the form
357     //
358    
359 amb 1035 function formSetHighway(type,value) // called from router.html (with one argument)
360 amb 569 {
361 amb 985 if(value == undefined)
362     routino.profile_highway[type][routino.transport]=document.forms["form"].elements["highway-" + type].value;
363     else
364     {
365     document.forms["form"].elements["highway-" + type].value=value;
366     routino.profile_highway[type][routino.transport]=value;
367     }
368 amb 569
369     paramschanged=true;
370     }
371    
372    
373     //
374     // Change of Speed in the form
375     //
376    
377 amb 1035 function formSetSpeed(type,value) // called from router.html (with one argument)
378 amb 569 {
379 amb 985 if(value == undefined)
380     routino.profile_speed[type][routino.transport]=document.forms["form"].elements["speed-" + type].value;
381     else
382     {
383     document.forms["form"].elements["speed-" + type].value=value;
384     routino.profile_speed[type][routino.transport]=value;
385     }
386 amb 569
387     paramschanged=true;
388     }
389    
390    
391     //
392 amb 574 // Change of Property in the form
393     //
394    
395 amb 1035 function formSetProperty(type,value) // called from router.html (with one argument)
396 amb 574 {
397 amb 985 if(value == undefined)
398     routino.profile_property[type][routino.transport]=document.forms["form"].elements["property-" + type].value;
399     else
400     {
401     document.forms["form"].elements["property-" + type].value=value;
402     routino.profile_property[type][routino.transport]=value;
403     }
404 amb 574
405     paramschanged=true;
406     }
407    
408    
409     //
410 amb 622 // Change of Restriction rule in the form
411 amb 569 //
412    
413 amb 1035 function formSetRestriction(type,value) // called from router.html (with one argument)
414 amb 569 {
415 amb 985 if(value == undefined)
416     {
417     if(type=="oneway" || type=="turns")
418     routino.profile_restrictions[type][routino.transport]=document.forms["form"].elements["restrict-" + type].checked;
419     else
420     routino.profile_restrictions[type][routino.transport]=document.forms["form"].elements["restrict-" + type].value;
421     }
422 amb 569 else
423 amb 985 {
424     if(type=="oneway" || type=="turns")
425     document.forms["form"].elements["restrict-" + type].checked=value;
426     else
427     document.forms["form"].elements["restrict-" + type].value=value;
428 amb 569
429 amb 985 routino.profile_restrictions[type][routino.transport]=value;
430     }
431    
432 amb 569 paramschanged=true;
433     }
434    
435    
436     //
437 amb 577 // Set the feature coordinates from the form when the form changes.
438     //
439    
440 amb 1035 function formSetCoords(marker,lon,lat) // called from router.html (with one argument)
441 amb 577 {
442 amb 1005 clearSearchResult(marker);
443    
444 amb 1032 if(lon == undefined && lat == undefined)
445 amb 985 {
446 amb 1034 lon=document.forms["form"].elements["lon" + marker].value;
447     lat=document.forms["form"].elements["lat" + marker].value;
448 amb 985 }
449 amb 1034
450     if(lon == "" && lat == "")
451 amb 985 {
452 amb 1034 document.forms["form"].elements["lon" + marker].value="";
453     document.forms["form"].elements["lat" + marker].value="";
454 amb 985
455 amb 1034 routino.point[marker].lon="";
456     routino.point[marker].lat=""
457 amb 985 }
458 amb 1034 else
459     {
460     if(lon=="")
461     {
462     var lonlat=map.getCenter().clone();
463     lonlat.transform(epsg900913,epsg4326);
464 amb 985
465 amb 1034 lon=lonlat.lon;
466     }
467 amb 577
468 amb 1034 if(lon<-180) lon=-180;
469     if(lon>+180) lon=+180;
470 amb 577
471 amb 1034 if(lat=="")
472     {
473     var lonlat=map.getCenter().clone();
474     lonlat.transform(epsg900913,epsg4326);
475 amb 577
476 amb 1034 lat=lonlat.lat;
477     }
478 amb 577
479 amb 1034 if(lat<-90 ) lat=-90 ;
480     if(lat>+90 ) lat=+90 ;
481 amb 577
482 amb 1034 var lonlat = new OpenLayers.LonLat(lon,lat);
483     lonlat.transform(epsg4326,epsg900913);
484    
485     markers[marker].move(lonlat);
486    
487     markersmoved=true;
488    
489     document.forms["form"].elements["lon" + marker].value=format5f(lon);
490     document.forms["form"].elements["lat" + marker].value=format5f(lat);
491    
492     routino.point[marker].lon=lon;
493     routino.point[marker].lat=lat;
494     routino.point[marker].used=true;
495 amb 1039
496     markerCheckHome(marker);
497 amb 1034 }
498 amb 577 }
499    
500    
501     //
502 amb 1032 // Set the search field from the form when the form changes.
503 amb 1001 //
504    
505 amb 1035 function formSetSearch(marker,search) // called from event handler linked to router.html (with one argument)
506 amb 1001 {
507 amb 1005 clearSearchResult(marker);
508    
509 amb 1001 if(search == undefined)
510     {
511     routino.point[marker].search=document.forms["form"].elements["search" + marker].value;
512    
513     DoSearch(marker);
514     }
515     else
516     {
517     document.forms["form"].elements["search" + marker].value=search;
518    
519     routino.point[marker].search=search;
520     }
521     }
522    
523    
524     //
525 amb 577 // Format a number in printf("%.5f") format.
526     //
527    
528     function format5f(number)
529     {
530     var newnumber=Math.floor(number*100000+0.5);
531     var delta=0;
532    
533     if(newnumber>=0 && newnumber<100000) delta= 100000;
534     if(newnumber<0 && newnumber>-100000) delta=-100000;
535    
536     var string=String(newnumber+delta);
537    
538     var intpart =string.substring(0,string.length-5);
539     var fracpart=string.substring(string.length-5,string.length);
540    
541     if(delta>0) intpart="0";
542     if(delta<0) intpart="-0";
543    
544     return(intpart + "." + fracpart);
545     }
546    
547    
548     //
549     // Build a set of URL arguments
550     //
551    
552 amb 986 function buildURLArguments(lang)
553 amb 577 {
554 amb 986 var url= "transport=" + routino.transport;
555 amb 577
556     for(var marker=1;marker<=vismarkers;marker++)
557 amb 986 if(routino.point[marker].active)
558 amb 577 {
559 amb 985 url=url + ";lon" + marker + "=" + routino.point[marker].lon;
560     url=url + ";lat" + marker + "=" + routino.point[marker].lat;
561 amb 1001 if(routino.point[marker].search != "")
562     url=url + ";search" + marker + "=" + encodeURIComponent(routino.point[marker].search);
563 amb 577 }
564    
565     for(var key in routino.profile_highway)
566     if(routino.profile_highway[key][routino.transport]!=routino_default.profile_highway[key][routino.transport])
567     url=url + ";highway-" + key + "=" + routino.profile_highway[key][routino.transport];
568    
569     for(var key in routino.profile_speed)
570     if(routino.profile_speed[key][routino.transport]!=routino_default.profile_speed[key][routino.transport])
571     url=url + ";speed-" + key + "=" + routino.profile_speed[key][routino.transport];
572    
573     for(var key in routino.profile_property)
574     if(routino.profile_property[key][routino.transport]!=routino_default.profile_property[key][routino.transport])
575     url=url + ";property-" + key + "=" + routino.profile_property[key][routino.transport];
576    
577     for(var key in routino.restrictions)
578     if(routino.profile_restrictions[key][routino.transport]!=routino_default.profile_restrictions[key][routino.transport])
579     url=url + ";" + key + "=" + routino.profile_restrictions[key][routino.transport];
580    
581 amb 986 if(lang && routino.language)
582 amb 577 url=url + ";language=" + routino.language;
583    
584     return(url);
585     }
586    
587    
588     //
589 amb 986 // Build a set of URL arguments for the map location
590 amb 569 //
591    
592 amb 986 function buildMapArguments()
593 amb 569 {
594 amb 1009 var lonlat = map.getCenter().clone();
595     lonlat.transform(epsg900913,epsg4326);
596 amb 569
597 amb 986 var zoom = map.getZoom() + map.minZoomLevel;
598    
599     return "lat=" + format5f(lonlat.lat) + ";lon=" + format5f(lonlat.lon) + ";zoom=" + zoom;
600 amb 569 }
601    
602    
603     //
604 amb 986 // Update a URL
605     //
606    
607 amb 1035 function updateURL(element) // called from router.html
608 amb 986 {
609     if(element.id == "permalink_url")
610     element.href=location.pathname + "?" + buildURLArguments(true) + ";" + buildMapArguments();
611    
612     if(element.id == "visualiser_url")
613     element.href="visualiser.html" + "?" + buildMapArguments();
614    
615     if(element.id == "edit_url")
616     element.href="http://www.openstreetmap.org/edit" + "?" + buildMapArguments();
617    
618     if(element.id.match(/^lang_([a-zA-Z-]+)_url$/))
619     element.href="router.html" + "." + RegExp.$1 + "?" + buildURLArguments(false) + ";" + buildMapArguments();
620     }
621    
622    
623 amb 577 ////////////////////////////////////////////////////////////////////////////////
624     ///////////////////////////////// Map handling /////////////////////////////////
625     ////////////////////////////////////////////////////////////////////////////////
626 amb 569
627     var map;
628 amb 933 var layerMap=[], layerVectors, layerGPX;
629 amb 569 var epsg4326, epsg900913;
630    
631 amb 985 //
632 amb 569 // Initialise the 'map' object
633     //
634    
635 amb 1035 function map_init() // called from router.html
636 amb 569 {
637 amb 985 lon =args["lon"];
638     lat =args["lat"];
639     zoom=args["zoom"];
640    
641 amb 1260 // Map URLs and limits are in mapprops.js.
642 amb 577
643 amb 569 //
644     // Create the map
645     //
646    
647     epsg4326=new OpenLayers.Projection("EPSG:4326");
648     epsg900913=new OpenLayers.Projection("EPSG:900913");
649    
650     map = new OpenLayers.Map ("map",
651     {
652     controls:[
653     new OpenLayers.Control.Navigation(),
654     new OpenLayers.Control.PanZoomBar(),
655     new OpenLayers.Control.ScaleLine(),
656     new OpenLayers.Control.LayerSwitcher()
657     ],
658    
659     projection: epsg900913,
660     displayProjection: epsg4326,
661    
662 amb 933 minZoomLevel: mapprops.zoomout,
663     numZoomLevels: mapprops.zoomin-mapprops.zoomout+1,
664 amb 1018 maxResolution: 156543.03390625 / Math.pow(2,mapprops.zoomout),
665 amb 569
666 amb 1013 // These two lines are not needed with OpenLayers 2.12
667     units: "m",
668 amb 577 maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
669 amb 569
670 amb 1013 restrictedExtent: new OpenLayers.Bounds(mapprops.westedge,mapprops.southedge,mapprops.eastedge,mapprops.northedge).transform(epsg4326,epsg900913)
671 amb 569 });
672    
673 amb 933 // Add map tile layers
674 amb 569
675 amb 933 for(var l=0;l < mapprops.mapdata.length;l++)
676     {
677     layerMap[l] = new OpenLayers.Layer.TMS(mapprops.mapdata[l].label,
678 amb 1239 mapprops.mapdata[l].tileurl,
679 amb 933 {
680     getURL: limitedUrl,
681     displayOutsideMaxExtent: true,
682     buffer: 1
683     });
684     map.addLayer(layerMap[l]);
685     }
686 amb 569
687 amb 1239 // Update the attribution if the layer changes
688 amb 569
689 amb 1239 map.events.register("changelayer",layerMap,change_attribution_event);
690    
691     function change_attribution_event(event)
692 amb 569 {
693 amb 1239 for(var l=0;l < mapprops.mapdata.length;l++)
694     if(this[l] == event.layer)
695     change_attribution(l);
696     }
697 amb 569
698 amb 1239 function change_attribution(l)
699     {
700     var data_url =mapprops.mapdata[l].attribution.data_url;
701     var data_text=mapprops.mapdata[l].attribution.data_text;
702     var tile_url =mapprops.mapdata[l].attribution.tile_url;
703     var tile_text=mapprops.mapdata[l].attribution.tile_text;
704 amb 569
705 amb 1239 document.getElementById("attribution_data").innerHTML="<a href=\"" + data_url + "\" target=\"data_attribution\">" + data_text + "</a>";
706     document.getElementById("attribution_tile").innerHTML="<a href=\"" + tile_url + "\" target=\"tile_attribution\">" + tile_text + "</a>";
707     }
708    
709     change_attribution(0);
710    
711     // Get a URL for the tile (mostly copied from OpenLayers/Layer/XYZ.js).
712    
713     function limitedUrl(bounds)
714     {
715 amb 569 var res = map.getResolution();
716 amb 1239 var res = this.getServerResolution();
717    
718     var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
719 amb 569 var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
720 amb 1239 var z = map.getZoom() + map.minZoomLevel;
721    
722 amb 569 var limit = Math.pow(2, z);
723 amb 1239 x = ((x % limit) + limit) % limit;
724 amb 569
725 amb 1239 var xyz = {'x': x, 'y': y, 'z': z};
726     var url = this.url;
727 amb 569
728 amb 1239 if (OpenLayers.Util.isArray(url))
729     {
730     var s = '' + xyz.x + xyz.y + xyz.z;
731     url = this.selectUrl(s, url);
732     }
733    
734     return OpenLayers.String.format(url, xyz);
735 amb 569 }
736    
737     // Define a GPX layer but don't add it yet
738    
739     layerGPX={shortest: null, quickest: null};
740    
741     gpx_style={shortest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#00FF00"}),
742     quickest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#0000FF"})};
743 amb 985
744 amb 569 // Add a vectors layer
745 amb 985
746 amb 569 layerVectors = new OpenLayers.Layer.Vector("Markers");
747     map.addLayer(layerVectors);
748    
749     // A set of markers
750    
751 amb 574 markers={};
752 amb 569 markersmoved=false;
753     paramschanged=false;
754    
755 amb 1012 for(var marker=1;marker<=mapprops.maxmarkers;marker++)
756 amb 569 {
757 amb 987 markers[marker] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{},
758     new OpenLayers.Style({},{externalGraphic: 'icons/marker-' + marker + '-red.png',
759     fillColor: "white",
760     graphicYOffset: -25,
761     graphicWidth: 21,
762     graphicHeight: 25,
763     display: "none"}));
764 amb 569
765 amb 987 layerVectors.addFeatures([markers[marker]]);
766 amb 569 }
767    
768     // A function to drag the markers
769    
770     var drag = new OpenLayers.Control.DragFeature(layerVectors,
771     {onDrag: dragMove,
772     onComplete: dragComplete });
773     map.addControl(drag);
774     drag.activate();
775    
776     // Markers to highlight a selected point
777    
778 amb 577 for(var highlight in highlights)
779 amb 569 {
780     highlights[highlight] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{},
781 amb 577 new OpenLayers.Style({},{strokeColor: route_dark_colours[highlight],
782 amb 574 fillColor: "white",
783 amb 569 pointRadius: 10,
784     strokeWidth: 4,
785     fillOpacity: 0,
786     display: "none"}));
787    
788     layerVectors.addFeatures([highlights[highlight]]);
789     }
790    
791 amb 574 // A popup for routing results
792    
793 amb 577 for(var popup in popups)
794     popups[popup] = createPopup(popup);
795 amb 574
796 amb 572 // Set the map centre to the limited range specified
797    
798 amb 577 map.setCenter(map.restrictedExtent.getCenterLonLat(), map.getZoomForExtent(map.restrictedExtent,true));
799 amb 572 map.maxResolution = map.getResolution();
800    
801 amb 569 // Move the map
802    
803 amb 985 if(lon != undefined && lat != undefined && zoom != undefined)
804 amb 569 {
805 amb 933 if(lon<mapprops.westedge) lon=mapprops.westedge;
806     if(lon>mapprops.eastedge) lon=mapprops.eastedge;
807    
808     if(lat<mapprops.southedge) lat=mapprops.southedge;
809     if(lat>mapprops.northedge) lat=mapprops.northedge;
810    
811     if(zoom<mapprops.zoomout) zoom=mapprops.zoomout;
812     if(zoom>mapprops.zoomin) zoom=mapprops.zoomin;
813    
814 amb 1009 var lonlat = new OpenLayers.LonLat(lon,lat);
815     lonlat.transform(epsg4326,epsg900913);
816 amb 569
817     map.moveTo(lonlat,zoom-map.minZoomLevel);
818     }
819     }
820    
821    
822     //
823     // OpenLayers.Control.DragFeature callback for a drag occuring.
824     //
825    
826     function dragMove(feature,pixel)
827     {
828 amb 574 for(var marker in markers)
829 amb 569 if(feature==markers[marker])
830 amb 985 dragSetForm(marker);
831 amb 569 }
832    
833    
834     //
835     // OpenLayers.Control.DragFeature callback for completing a drag.
836     //
837    
838     function dragComplete(feature,pixel)
839     {
840 amb 574 for(var marker in markers)
841 amb 569 if(feature==markers[marker])
842 amb 985 dragSetForm(marker);
843 amb 569 }
844    
845    
846 amb 985 //
847     // Set the feature coordinates in the form after dragging.
848     //
849    
850     function dragSetForm(marker)
851     {
852     var lonlat = new OpenLayers.LonLat(markers[marker].geometry.x, markers[marker].geometry.y);
853 amb 1009 lonlat.transform(epsg900913,epsg4326);
854 amb 985
855     var lon=format5f(lonlat.lon);
856     var lat=format5f(lonlat.lat);
857    
858     formSetCoords(marker,lon,lat);
859     }
860    
861    
862 amb 577 ////////////////////////////////////////////////////////////////////////////////
863     /////////////////////////////// Marker handling ////////////////////////////////
864     ////////////////////////////////////////////////////////////////////////////////
865    
866    
867 amb 569 //
868 amb 574 // Toggle a marker on the map.
869 amb 569 //
870    
871 amb 1035 function markerToggleMap(marker) // called from router.html
872 amb 569 {
873 amb 1034 if(!routino.point[marker].used)
874     {
875     routino.point[marker].used=true;
876     markerCentre(marker);
877     markerCoords(marker);
878     }
879 amb 1005
880 amb 1032 markerAddRemoveMap(marker,!routino.point[marker].active);
881     }
882    
883    
884     //
885     // Show or hide a marker on the map.
886     //
887    
888     function markerAddRemoveMap(marker,active)
889     {
890     if(active)
891     markerAddMap(marker);
892     else
893 amb 574 markerRemoveMap(marker);
894     }
895    
896    
897     //
898     // Show a marker on the map.
899     //
900    
901     function markerAddMap(marker)
902     {
903 amb 1005 clearSearchResult(marker);
904    
905 amb 577 markers[marker].style.display = "";
906 amb 985 routino.point[marker].active=true;
907 amb 1034 routino.point[marker].used=true;
908 amb 574
909 amb 577 updateIcon(marker);
910 amb 574
911     markersmoved=true;
912     }
913    
914    
915     //
916     // Remove a marker from the map.
917     //
918    
919     function markerRemoveMap(marker)
920     {
921 amb 1005 clearSearchResult(marker);
922    
923 amb 577 markers[marker].style.display = "none";
924 amb 985 routino.point[marker].active=false;
925 amb 574
926 amb 577 updateIcon(marker);
927 amb 574
928 amb 577 markersmoved=true;
929     }
930 amb 574
931 amb 577
932     //
933 amb 1001 // Display search string for the marker
934     //
935    
936 amb 1035 function markerSearch(marker) // called from router.html
937 amb 1001 {
938 amb 1005 clearSearchResult(marker);
939    
940 amb 1011 document.getElementById("coords" + marker).style.display="none";
941     document.getElementById("search" + marker).style.display="";
942 amb 1001 }
943    
944    
945     //
946     // Display coordinates for the marker
947     //
948    
949 amb 1035 function markerCoords(marker) // called from router.html
950 amb 1001 {
951 amb 1005 clearSearchResult(marker);
952    
953 amb 1011 document.getElementById("search" + marker).style.display="none";
954     document.getElementById("coords" + marker).style.display="";
955 amb 1001 }
956    
957    
958     //
959 amb 577 // Centre the marker on the map
960     //
961    
962 amb 1035 function markerCentre(marker) // called from router.html
963 amb 577 {
964 amb 1034 if(!routino.point[marker].used)
965     return;
966    
967 amb 1005 clearSearchResult(marker);
968    
969 amb 985 var lonlat=map.getCenter().clone();
970 amb 1009 lonlat.transform(epsg900913,epsg4326);
971 amb 577
972 amb 1032 formSetCoords(marker,lonlat.lon,lonlat.lat);
973 amb 574 }
974    
975    
976     //
977 amb 984 // Centre the map on the marker
978     //
979    
980 amb 1035 function markerRecentre(marker) // called from router.html
981 amb 984 {
982 amb 1034 if(!routino.point[marker].used)
983     return;
984    
985 amb 1005 clearSearchResult(marker);
986    
987 amb 985 lon=routino.point[marker].lon;
988     lat=routino.point[marker].lat;
989 amb 984
990 amb 1009 var lonlat = new OpenLayers.LonLat(lon,lat);
991     lonlat.transform(epsg4326,epsg900913);
992 amb 984
993     map.panTo(lonlat);
994     }
995    
996    
997     //
998 amb 574 // Clear the current marker.
999     //
1000    
1001 amb 1035 function markerRemove(marker) // called from router.html
1002 amb 574 {
1003 amb 1005 clearSearchResult(marker);
1004    
1005 amb 1033 for(var m=marker;m<vismarkers;m++)
1006 amb 1039 markerCopy(m,m+1);
1007 amb 574
1008 amb 1033 markerRemoveForm(vismarkers--);
1009 amb 574
1010     if(vismarkers==1)
1011     markerAddAfter(1);
1012     }
1013    
1014    
1015     //
1016     // Add a marker before the current one.
1017     //
1018    
1019     function markerAddBefore(marker)
1020     {
1021 amb 1005 clearSearchResult(marker);
1022    
1023 amb 1012 if(vismarkers==mapprops.maxmarkers || marker==1)
1024 amb 574 return false;
1025    
1026 amb 1033 markerAddForm(++vismarkers);
1027 amb 574
1028 amb 1033 for(var m=vismarkers;m>marker;m--)
1029 amb 1039 markerCopy(m,m-1);
1030 amb 574
1031 amb 1033 markerClearForm(marker-1);
1032 amb 574 }
1033    
1034    
1035     //
1036     // Add a marker after the current one.
1037     //
1038    
1039 amb 1035 function markerAddAfter(marker) // called from router.html
1040 amb 574 {
1041 amb 1005 clearSearchResult(marker);
1042    
1043 amb 1012 if(vismarkers==mapprops.maxmarkers)
1044 amb 574 return false;
1045    
1046 amb 1033 markerAddForm(++vismarkers);
1047 amb 574
1048 amb 1033 for(var m=vismarkers;m>(marker+1);m--)
1049 amb 1039 markerCopy(m,m-1);
1050 amb 574
1051 amb 1033 markerClearForm(marker+1);
1052 amb 569 }
1053    
1054    
1055     //
1056 amb 577 // Set this marker as the home location.
1057     //
1058    
1059 amb 1035 function markerHome(marker) // called from router.html
1060 amb 577 {
1061 amb 1034 if(!routino.point[marker].used)
1062 amb 1036 {
1063     markerMoveHome(marker);
1064     }
1065 amb 1039 else
1066     {
1067     clearSearchResult(marker);
1068 amb 1034
1069 amb 1039 markerSetClearHome(marker,!routino.point[marker].home);
1070     }
1071 amb 577 }
1072    
1073    
1074     //
1075 amb 984 // Set this marker as the current location.
1076     //
1077    
1078 amb 1035 function markerLocate(marker) // called from router.html
1079 amb 984 {
1080 amb 1005 clearSearchResult(marker);
1081    
1082 amb 984 if(navigator.geolocation)
1083     navigator.geolocation.getCurrentPosition(
1084     function(position) {
1085 amb 1032 formSetCoords(marker,position.coords.longitude,position.coords.latitude);
1086     markerAddMap(marker);
1087 amb 984 });
1088     }
1089    
1090    
1091     //
1092 amb 577 // Update an icon to set colours and home or normal marker.
1093     //
1094    
1095     function updateIcon(marker)
1096     {
1097 amb 1036 if(routino.point[marker].home)
1098 amb 577 {
1099 amb 985 if(routino.point[marker].active)
1100 amb 577 document.images["waypoint" + marker].src="icons/marker-home-red.png";
1101     else
1102     document.images["waypoint" + marker].src="icons/marker-home-grey.png";
1103    
1104     markers[marker].style.externalGraphic="icons/marker-home-red.png";
1105     }
1106     else
1107     {
1108 amb 985 if(routino.point[marker].active)
1109 amb 577 document.images["waypoint" + marker].src="icons/marker-" + marker + "-red.png";
1110     else
1111     document.images["waypoint" + marker].src="icons/marker-" + marker + "-grey.png";
1112    
1113     markers[marker].style.externalGraphic="icons/marker-" + marker + "-red.png";
1114     }
1115    
1116     layerVectors.drawFeature(markers[marker]);
1117     }
1118    
1119    
1120     //
1121 amb 1036 // Move the marker to the home location
1122 amb 577 //
1123    
1124 amb 1036 function markerMoveHome(marker)
1125 amb 577 {
1126 amb 1036 if(homelon==null || homelat==null)
1127     return;
1128 amb 577
1129 amb 1036 routino.point[marker].home=true;
1130     routino.point[marker].used=true;
1131 amb 577
1132 amb 1036 formSetCoords(marker,homelon,homelat);
1133     markerAddMap(marker);
1134     }
1135    
1136    
1137     //
1138     // Set or clear the home marker icon
1139     //
1140    
1141     function markerSetClearHome(marker,home)
1142     {
1143 amb 577 var cookie;
1144     var date = new Date();
1145    
1146 amb 1036 if(home)
1147 amb 577 {
1148 amb 1036 homelat=routino.point[marker].lat;
1149     homelon=routino.point[marker].lon;
1150 amb 577
1151 amb 1036 cookie="Routino-home=lon:" + homelon + ":lat:" + homelat;
1152    
1153 amb 577 date.setUTCFullYear(date.getUTCFullYear()+5);
1154    
1155 amb 1036 routino.point[marker].home=true;
1156 amb 577 }
1157     else
1158     {
1159 amb 1036 homelat=null;
1160     homelon=null;
1161    
1162 amb 577 cookie="Routino-home=unset";
1163    
1164     date.setUTCFullYear(date.getUTCFullYear()-1);
1165    
1166 amb 1036 routino.point[marker].home=false;
1167 amb 577 }
1168    
1169     document.cookie=cookie + ";expires=" + date.toGMTString();
1170    
1171 amb 1039 updateIcon(marker);
1172    
1173 amb 1036 for(m=1;m<=mapprops.maxmarkers;m++)
1174 amb 1039 markerCheckHome(m);
1175     }
1176 amb 1036
1177 amb 1039
1178     //
1179     // Check if a marker is the home marker
1180     //
1181    
1182     function markerCheckHome(marker)
1183     {
1184     var home=routino.point[marker].home;
1185    
1186     if(routino.point[marker].lon==homelon && routino.point[marker].lat==homelat)
1187     routino.point[marker].home=true;
1188     else
1189     routino.point[marker].home=false;
1190    
1191     if(home!=routino.point[marker].home)
1192     updateIcon(marker);
1193 amb 577 }
1194    
1195    
1196     //
1197 amb 574 // Move this marker up.
1198     //
1199    
1200 amb 1035 function markerMoveUp(marker) // called from router.html
1201 amb 574 {
1202     if(marker==1)
1203 amb 984 {
1204     for(var m=1;m<vismarkers;m++)
1205     markerSwap(m,m+1);
1206     }
1207     else
1208     markerSwap(marker,marker-1);
1209 amb 574 }
1210    
1211    
1212     //
1213     // Move this marker down.
1214     //
1215    
1216 amb 1035 function markerMoveDown(marker) // called from router.html
1217 amb 574 {
1218     if(marker==vismarkers)
1219 amb 984 {
1220     for(var m=vismarkers;m>1;m--)
1221     markerSwap(m,m-1);
1222     }
1223     else
1224     markerSwap(marker,marker+1);
1225 amb 574 }
1226    
1227    
1228     //
1229 amb 1039 // Copy a marker from one place to another.
1230 amb 1033 //
1231    
1232 amb 1039 function markerCopy(marker1,marker2)
1233 amb 1033 {
1234     for(var element in routino.point[marker2])
1235     routino.point[marker1][element]=routino.point[marker2][element];
1236    
1237     document.getElementById("search" + marker1).style.display=document.getElementById("search" + marker2).style.display;
1238    
1239     document.getElementById("coords" + marker1).style.display=document.getElementById("coords" + marker2).style.display;
1240    
1241     document.forms["form"].elements["search" + marker1].value=document.forms["form"].elements["search" + marker2].value;
1242    
1243     formSetCoords(marker1,routino.point[marker1].lon,routino.point[marker1].lat);
1244    
1245     markerAddRemoveMap(marker1,routino.point[marker1].active);
1246     }
1247    
1248    
1249     //
1250 amb 574 // Swap a pair of markers.
1251     //
1252    
1253     function markerSwap(marker1,marker2)
1254     {
1255 amb 1033 for(var element in routino.point[marker2])
1256     {
1257     var temp=routino.point[marker1][element];
1258     routino.point[marker1][element]=routino.point[marker2][element];
1259     routino.point[marker2][element]=temp;
1260     }
1261 amb 574
1262 amb 1033 var search_display=document.getElementById("search" + marker1).style.display;
1263     document.getElementById("search" + marker1).style.display=document.getElementById("search" + marker2).style.display;
1264     document.getElementById("search" + marker2).style.display=search_display;
1265 amb 574
1266 amb 1033 var coords_display=document.getElementById("coords" + marker1).style.display;
1267     document.getElementById("coords" + marker1).style.display=document.getElementById("coords" + marker2).style.display;
1268     document.getElementById("coords" + marker2).style.display=coords_display;
1269    
1270     var search_value=document.forms["form"].elements["search" + marker1].value;
1271     document.forms["form"].elements["search" + marker1].value=document.forms["form"].elements["search" + marker2].value;
1272     document.forms["form"].elements["search" + marker2].value=search_value;
1273    
1274     formSetCoords(marker1,routino.point[marker1].lon,routino.point[marker1].lat);
1275     formSetCoords(marker2,routino.point[marker2].lon,routino.point[marker2].lat);
1276    
1277     markerAddRemoveMap(marker1,routino.point[marker1].active);
1278     markerAddRemoveMap(marker2,routino.point[marker2].active);
1279 amb 574 }
1280    
1281    
1282     //
1283     // Reverse the markers.
1284     //
1285    
1286 amb 1035 function markersReverse() // called from router.html
1287 amb 574 {
1288     for(var marker=1;marker<=vismarkers/2;marker++)
1289     markerSwap(marker,vismarkers+1-marker);
1290     }
1291    
1292    
1293 amb 1033 //
1294 amb 1038 // Close the loop.
1295     //
1296    
1297     function markersLoop() // called from router.html
1298     {
1299     if(vismarkers==mapprops.maxmarkers)
1300     return false;
1301    
1302     if(routino.point[vismarkers].lon==routino.point[1].lon && routino.point[vismarkers].lat==routino.point[1].lat)
1303     return false;
1304    
1305     if(routino.point[vismarkers].used)
1306     markerAddForm(++vismarkers);
1307    
1308 amb 1039 markerCopy(vismarkers,1);
1309 amb 1038 }
1310    
1311    
1312     //
1313 amb 1033 // Display the form for a marker
1314     //
1315    
1316     function markerAddForm(marker)
1317     {
1318     document.getElementById("waypoint" + marker).style.display="";
1319     }
1320    
1321    
1322     //
1323     // Hide the form for a marker
1324     //
1325    
1326     function markerRemoveForm(marker)
1327     {
1328     document.getElementById("waypoint" + marker).style.display="none";
1329    
1330     markerClearForm(marker);
1331     }
1332    
1333    
1334     //
1335     // Clear the form for a marker
1336     //
1337    
1338     function markerClearForm(marker)
1339     {
1340     markerRemoveMap(marker);
1341     markerCoords(marker);
1342    
1343     formSetCoords(marker,"","");
1344     formSetSearch(marker,"");
1345    
1346     updateIcon(marker);
1347    
1348     routino.point[marker].used=false;
1349 amb 1036 routino.point[marker].home=false;
1350 amb 1033 }
1351    
1352    
1353 amb 577 ////////////////////////////////////////////////////////////////////////////////
1354     //////////////////////////// Route results handling ////////////////////////////
1355     ////////////////////////////////////////////////////////////////////////////////
1356    
1357     var route_light_colours={shortest: "#60C060", quickest: "#6060C0"};
1358     var route_dark_colours ={shortest: "#408040", quickest: "#404080"};
1359    
1360     var highlights={shortest: null, quickest: null};
1361     var popups={shortest: null, quickest: null};
1362     var routepoints={shortest: {}, quickest: {}};
1363     var gpx_style={shortest: null, quickest: null};
1364    
1365 amb 574 //
1366 amb 577 // Zoom to a specific item in the route
1367 amb 569 //
1368    
1369 amb 577 function zoomTo(type,line)
1370 amb 569 {
1371 amb 1009 var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat);
1372     lonlat.transform(epsg4326,epsg900913);
1373 amb 569
1374 amb 577 map.moveTo(lonlat,map.numZoomLevels-2);
1375 amb 569 }
1376    
1377    
1378     //
1379 amb 577 // Highlight a specific item in the route
1380 amb 569 //
1381    
1382 amb 577 function highlight(type,line)
1383 amb 569 {
1384 amb 577 if(line==-1)
1385     {
1386     highlights[type].style.display = "none";
1387 amb 569
1388 amb 577 drawPopup(popups[type],null);
1389 amb 569 }
1390 amb 577 else
1391 amb 569 {
1392 amb 577 // Marker
1393 amb 569
1394 amb 1009 var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat);
1395     lonlat.transform(epsg4326,epsg900913);
1396 amb 569
1397 amb 577 highlights[type].move(lonlat);
1398 amb 569
1399 amb 577 if(highlights[type].style.display = "none")
1400     highlights[type].style.display = "";
1401 amb 569
1402 amb 577 // Popup
1403 amb 569
1404 amb 577 drawPopup(popups[type],"<table>" + routepoints[type][line].html + "</table>");
1405     }
1406    
1407     layerVectors.drawFeature(highlights[type]);
1408 amb 569 }
1409    
1410    
1411     //
1412 amb 577 // Create a popup - not using OpenLayers because want it fixed on screen not fixed on map.
1413 amb 569 //
1414    
1415 amb 577 function createPopup(type)
1416 amb 569 {
1417 amb 577 var popup=document.createElement('div');
1418 amb 569
1419 amb 577 popup.className = "popup";
1420 amb 569
1421 amb 577 popup.innerHTML = "<span></span>";
1422 amb 569
1423 amb 577 popup.style.display = "none";
1424 amb 569
1425 amb 577 popup.style.position = "fixed";
1426     popup.style.top = "-4000px";
1427     popup.style.left = "-4000px";
1428     popup.style.zIndex = "100";
1429 amb 569
1430 amb 577 popup.style.padding = "5px";
1431 amb 569
1432 amb 577 popup.style.opacity=0.85;
1433     popup.style.backgroundColor=route_light_colours[type];
1434     popup.style.border="4px solid " + route_dark_colours[type];
1435 amb 569
1436 amb 577 document.body.appendChild(popup);
1437 amb 569
1438 amb 577 return(popup);
1439 amb 569 }
1440    
1441    
1442     //
1443 amb 577 // Draw a popup - not using OpenLayers because want it fixed on screen not fixed on map.
1444 amb 569 //
1445    
1446 amb 577 function drawPopup(popup,html)
1447 amb 569 {
1448 amb 577 if(html==null)
1449 amb 569 {
1450 amb 577 popup.style.display="none";
1451     return;
1452     }
1453 amb 574
1454 amb 577 if(popup.style.display=="none")
1455 amb 569 {
1456 amb 577 var map_div=document.getElementById("map");
1457 amb 569
1458 amb 577 popup.style.left =map_div.offsetParent.offsetLeft+map_div.offsetLeft+60 + "px";
1459     popup.style.top = map_div.offsetTop +30 + "px";
1460     popup.style.width =map_div.clientWidth-100 + "px";
1461 amb 574
1462 amb 577 popup.style.display="";
1463 amb 569 }
1464    
1465 amb 577 popup.innerHTML=html;
1466 amb 569 }
1467    
1468    
1469     //
1470 amb 577 // Remove a GPX trace
1471 amb 569 //
1472    
1473 amb 577 function removeGPXTrace(type)
1474 amb 569 {
1475 amb 577 map.removeLayer(layerGPX[type]);
1476     layerGPX[type].destroy();
1477     layerGPX[type]=null;
1478 amb 569
1479 amb 577 displayStatus(type,"no_info");
1480 amb 569
1481 amb 1011 document.getElementById(type + "_links").style.display = "none";
1482 amb 569
1483 amb 1011 document.getElementById(type + "_route").innerHTML = "";
1484 amb 569
1485 amb 577 hideshow_hide(type);
1486     }
1487 amb 569
1488 amb 574
1489 amb 577 ////////////////////////////////////////////////////////////////////////////////
1490     /////////////////////////////// Server handling ////////////////////////////////
1491     ////////////////////////////////////////////////////////////////////////////////
1492 amb 569
1493     //
1494     // Display data statistics
1495     //
1496    
1497 amb 1035 function displayStatistics() // called from router.html
1498 amb 569 {
1499     // Use AJAX to get the statistics
1500    
1501 amb 1013 OpenLayers.Request.GET({url: "statistics.cgi", success: runStatisticsSuccess});
1502 amb 569 }
1503    
1504    
1505     //
1506 amb 577 // Success in running data statistics generation.
1507 amb 569 //
1508    
1509     function runStatisticsSuccess(response)
1510     {
1511 amb 1011 document.getElementById("statistics_data").innerHTML="<pre>" + response.responseText + "</pre>";
1512     document.getElementById("statistics_link").style.display="none";
1513 amb 569 }
1514    
1515    
1516     //
1517 amb 577 // Submit form - perform the routing
1518 amb 569 //
1519    
1520 amb 1035 function findRoute(type) // called from router.html
1521 amb 569 {
1522     tab_select("results");
1523    
1524 amb 572 hideshow_hide('help_options');
1525 amb 569 hideshow_hide('shortest');
1526     hideshow_hide('quickest');
1527    
1528 amb 577 displayStatus("result","running");
1529 amb 569
1530 amb 986 var url="router.cgi" + "?" + buildURLArguments(true) + ";type=" + type;
1531 amb 569
1532     // Destroy the existing layer(s)
1533    
1534     if(markersmoved || paramschanged)
1535     {
1536     if(layerGPX.shortest!=null)
1537     removeGPXTrace("shortest");
1538     if(layerGPX.quickest!=null)
1539     removeGPXTrace("quickest");
1540     markersmoved=false;
1541     paramschanged=false;
1542     }
1543     else if(layerGPX[type]!=null)
1544     removeGPXTrace(type);
1545    
1546     // Use AJAX to run the router
1547    
1548     routing_type=type;
1549    
1550 amb 1013 OpenLayers.Request.GET({url: url, success: runRouterSuccess, failure: runRouterFailure});
1551 amb 569 }
1552    
1553    
1554     //
1555     // Success in running router.
1556     //
1557    
1558     function runRouterSuccess(response)
1559     {
1560     var lines=response.responseText.split('\n');
1561    
1562     var uuid=lines[0];
1563 amb 1043 var success=lines[1];
1564 amb 569
1565 amb 629 var link;
1566    
1567 amb 569 // Update the status message
1568    
1569 amb 1043 if(success=="ERROR")
1570 amb 569 {
1571 amb 577 displayStatus("result","error");
1572 amb 572 hideshow_show('help_route');
1573 amb 629
1574     link=document.getElementById("router_log_error");
1575     link.href="results.cgi?uuid=" + uuid + ";type=router;format=log";
1576    
1577 amb 569 return;
1578     }
1579     else
1580     {
1581 amb 577 displayStatus("result","complete");
1582 amb 572 hideshow_hide('help_route');
1583 amb 629
1584     link=document.getElementById("router_log_complete");
1585     link.href="results.cgi?uuid=" + uuid + ";type=router;format=log";
1586 amb 569 }
1587    
1588     // Update the routing result message
1589    
1590 amb 577 link=document.getElementById(routing_type + "_html");
1591     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html";
1592 amb 1011
1593 amb 577 link=document.getElementById(routing_type + "_gpx_track");
1594     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track";
1595 amb 1011
1596 amb 577 link=document.getElementById(routing_type + "_gpx_route");
1597     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-route";
1598 amb 1011
1599 amb 577 link=document.getElementById(routing_type + "_text_all");
1600     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text-all";
1601 amb 1011
1602 amb 577 link=document.getElementById(routing_type + "_text");
1603     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text";
1604 amb 569
1605 amb 1011 links=document.getElementById(routing_type + "_links").style.display = "";
1606 amb 569
1607     // Add a GPX layer
1608    
1609     var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track";
1610    
1611 amb 1013 layerGPX[routing_type] = new OpenLayers.Layer.Vector("GPX (" + routing_type + ")",
1612     {
1613     protocol: new OpenLayers.Protocol.HTTP({url: url, format: new OpenLayers.Format.GPX()}),
1614     strategies: [new OpenLayers.Strategy.Fixed()],
1615     style: gpx_style[routing_type],
1616     projection: map.displayProjection
1617     });
1618 amb 569
1619     map.addLayer(layerGPX[routing_type]);
1620    
1621     hideshow_show(routing_type);
1622    
1623     displayResult(routing_type,uuid);
1624     }
1625    
1626    
1627     //
1628     // Failure in running router.
1629     //
1630    
1631     function runRouterFailure(response)
1632     {
1633 amb 577 displayStatus("result","failed");
1634 amb 569 }
1635    
1636    
1637     //
1638 amb 577 // Display the status
1639     //
1640    
1641     function displayStatus(type,subtype,content)
1642     {
1643 amb 1011 var child=document.getElementById(type + "_status").firstChild;
1644 amb 577
1645     do
1646     {
1647     if(child.id != undefined)
1648     child.style.display="none";
1649    
1650     child=child.nextSibling;
1651     }
1652     while(child != undefined);
1653    
1654 amb 936 var chosen_status=document.getElementById(type + "_status_" + subtype);
1655 amb 577
1656 amb 936 chosen_status.style.display="";
1657 amb 577
1658     if(content != null)
1659 amb 936 chosen_status.innerHTML=content;
1660 amb 577 }
1661    
1662    
1663     //
1664 amb 569 // Display the route
1665     //
1666    
1667     function displayResult(type,uuid)
1668     {
1669     routing_type = type;
1670    
1671     // Add the route
1672    
1673 amb 577 var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html";
1674 amb 569
1675     // Use AJAX to get the route
1676    
1677 amb 1013 OpenLayers.Request.GET({url: url, success: getRouteSuccess, failure: getRouteFailure});
1678 amb 569 }
1679    
1680    
1681     //
1682     // Success in getting route.
1683     //
1684    
1685     function getRouteSuccess(response)
1686     {
1687     var lines=response.responseText.split('\n');
1688    
1689 amb 577 routepoints[routing_type]=[];
1690 amb 569
1691 amb 577 var points=routepoints[routing_type];
1692 amb 569
1693 amb 577 var table=0;
1694     var point=0;
1695     var total_table,total_word;
1696 amb 569
1697 amb 577 for(var line=0;line<lines.length;line++)
1698 amb 569 {
1699 amb 577 var thisline=lines[line];
1700 amb 569
1701 amb 577 if(table==0)
1702     {
1703     if(thisline.match('<table>'))
1704     table=1;
1705     else
1706     continue;
1707     }
1708 amb 574
1709 amb 577 if(thisline.match('</table>'))
1710     break;
1711    
1712     if(thisline.match('<tr class=\'([a-z])\'>'))
1713 amb 569 {
1714 amb 577 var rowtype=RegExp.$1;
1715 amb 569
1716 amb 577 if(rowtype=='c')
1717     {
1718     thisline.match('<td class=\'r\'> *([-0-9.]+) *([-0-9.]+)');
1719     points[point]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), html: "", highway: "", distance: "", total: ""};
1720 amb 574
1721 amb 577 point++;
1722     }
1723     else if(rowtype=='n')
1724     {
1725     points[point-1].html += thisline;
1726     }
1727     else if(rowtype=='s')
1728     {
1729     thisline.match('<span class=\'h\'>([^<]+)</span>');
1730     points[point-1].highway = RegExp.$1;
1731 amb 574
1732 amb 577 thisline.match('<span class=\'d\'>([^<]+)</span>');
1733     points[point-1].distance = RegExp.$1;
1734 amb 574
1735 amb 577 thisline.match('(<span class=\'j\'>[^<]+</span>)');
1736     points[point-1].total = RegExp.$1;
1737    
1738     thisline.match('^(.*).<span class=\'j\'>');
1739    
1740     points[point-1].html += RegExp.$1;
1741     }
1742     else if(rowtype=='t')
1743     {
1744     points[point-1].html += thisline;
1745    
1746     thisline.match('^(.*<td class=\'r\'>)');
1747     total_table = RegExp.$1;
1748    
1749     thisline.match('<td class=\'l\'>([^<]+)<');
1750     total_word = RegExp.$1;
1751    
1752     thisline.match('<span class=\'j\'>([^<]+)</span>');
1753     points[point-1].total = RegExp.$1;
1754     }
1755 amb 569 }
1756     }
1757    
1758 amb 935 displayStatus(routing_type,"info",points[point-1].total.bold());
1759    
1760 amb 577 var result="<table onmouseout='highlight(\"" + routing_type + "\",-1)'>";
1761    
1762     for(var p=0;p<point-1;p++)
1763     {
1764     points[p].html += total_table + points[p].total;
1765    
1766     result=result + "<tr onclick='zoomTo(\"" + routing_type + "\"," + p + ")'" +
1767     " onmouseover='highlight(\"" + routing_type + "\"," + p + ")'>" +
1768     "<td class='distance' title='" + points[p].distance + "'>#" + (p+1) +
1769     "<td class='highway'>" + points[p].highway;
1770     }
1771    
1772     result=result + "<tr onclick='zoomTo(\"" + routing_type + "\"," + p + ")'" +
1773     " onmouseover='highlight(\"" + routing_type + "\"," + p + ")'>" +
1774     "<td colspan='2'>" + total_word + " " + points[p].total;
1775    
1776 amb 569 result=result + "</table>";
1777    
1778 amb 1011 document.getElementById(routing_type + "_route").innerHTML=result;
1779 amb 569 }
1780    
1781    
1782     //
1783     // Failure in getting route.
1784     //
1785    
1786     function getRouteFailure(response)
1787     {
1788 amb 1011 document.getElementById(routing_type + "_route").innerHTML = "";
1789 amb 569 }
1790 amb 1001
1791    
1792     //
1793     // Perform a search
1794     //
1795    
1796     function DoSearch(marker)
1797     {
1798     // Use AJAX to get the search result
1799    
1800     var search=routino.point[marker].search;
1801    
1802 amb 1009 var bounds=map.getExtent().clone();
1803     bounds.transform(epsg900913,epsg4326);
1804 amb 1001
1805 amb 1008 var url="search.cgi?marker=" + marker +
1806 amb 1009 ";left=" + format5f(bounds.left) +
1807     ";top=" + format5f(bounds.top) +
1808     ";right=" + format5f(bounds.right) +
1809     ";bottom=" + format5f(bounds.bottom) +
1810 amb 1008 ";search=" + encodeURIComponent(search);
1811    
1812 amb 1013 OpenLayers.Request.GET({url: url, success: runSearchSuccess});
1813 amb 1001 }
1814    
1815    
1816 amb 1005 var searchresults=[];
1817    
1818 amb 1001 //
1819     // Success in running search.
1820     //
1821    
1822     function runSearchSuccess(response)
1823     {
1824     var lines=response.responseText.split('\n');
1825    
1826     var marker=lines[0];
1827     var cpuinfo=lines[1]; // not used
1828 amb 1005 var message=lines[2]; // not used
1829 amb 1001
1830 amb 1005 if(message != "")
1831     {
1832     alert(message);
1833     return;
1834     }
1835 amb 1001
1836 amb 1005 searchresults[marker]=[];
1837    
1838     for(var line=3;line<lines.length;line++)
1839     {
1840     var thisline=lines[line];
1841    
1842     if(thisline=="")
1843     break;
1844    
1845     thisline.match('([-.0-9]+) ([-.0-9]+) (.*)');
1846    
1847     searchresults[marker][searchresults[marker].length]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), name: RegExp.$3};
1848     }
1849    
1850     if(searchresults[marker].length==1)
1851     {
1852     formSetSearch(marker,searchresults[marker][0].name);
1853 amb 1032 formSetCoords(marker,searchresults[marker][0].lon,searchresults[marker][0].lat);
1854     markerAddMap(marker);
1855 amb 1005 }
1856     else
1857     {
1858     var results=document.getElementById("searchresults" + marker);
1859    
1860     var innerHTML="<td colspan=\"3\">";
1861    
1862     for(var n=0;n<searchresults[marker].length;n++)
1863     {
1864     if(n>0)
1865     innerHTML+="<br>";
1866    
1867     innerHTML+="<a href=\"#\" onclick=\"choseSearchResult(" + marker + "," + n + ")\">" +
1868     searchresults[marker][n].name +
1869     "</a>";
1870     }
1871    
1872     results.innerHTML=innerHTML;
1873    
1874     results.style.display="";
1875     }
1876 amb 1001 }
1877 amb 1005
1878    
1879     //
1880     // Display search results.
1881     //
1882    
1883     function choseSearchResult(marker,n)
1884     {
1885     if(n>=0)
1886     {
1887     formSetSearch(marker,searchresults[marker][n].name);
1888 amb 1032 formSetCoords(marker,searchresults[marker][n].lon,searchresults[marker][n].lat);
1889     markerAddMap(marker);
1890 amb 1005 }
1891     }
1892    
1893    
1894     //
1895     // Clear search results.
1896     //
1897    
1898     function clearSearchResult(marker)
1899     {
1900 amb 1011 document.getElementById("searchresults" + marker).style.display="none";
1901 amb 1005 }