Improved www.ttss.krakow.pl
Jacek Kowalski
2017-04-10 e5c5bd859034bcc838f007ce2ea75eaf15b63cd7
commit | author | age
e5c5bd 1 <!DOCTYPE HTML>
JK 2 <html>
3 <head>
4 <title>OpenLayers Demo</title>
5 <style type="text/css">
6 html, body, #map {
7     width: 100%;
8     height: 100%;
9     margin: 0;
10 }
11 </style>
12 <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7"  crossorigin="anonymous"></script>
13 <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList"></script>
14 <script src="https://openlayers.org/en/v4.0.1/build/ol.js" type="text/javascript"></script>
15 <script tyle="text/javascript" src="lang_en.js" id="lang_script"></script>
16 </head>
17 <body>
18 <div id="map"><div id="popup"></div></div>
19
20 <script tyle="text/javascript">
21 //var ttss_base = 'http://www.ttss.krakow.pl/internetservice';
22 var ttss_base = '/proxy.php';
23 var ttss_refresh = 5000; // 5 seconds
24
25 var vehicles_source = null;
26 var vehicles_xhr = null;
27 var vehicles_timer = null;
28
29 var map = null;
30 var popup_element = document.getElementById('popup');
31 var popup = null;
32
33 function fail(msg) {
34     console.log(msg);
35     alert(msg);
36 }
37
38 function fail_ajax(data) {
39     // abort() is not a failure
40     if(data.readyState == 0 && data.statusText == 'abort') return;
41     
42     if(data.status == 0) {
43         fail(lang.error_request_failed_connectivity, data);
44     } else if (data.statusText) {
45         fail(lang.error_request_failed_status.replace('$status', data.statusText), data);
46     } else {
47         fail(lang.error_request_failed, data);
48     }
49 }
50
51 function init() {
52     if(!window.jQuery) {
53         fail(lang.jquery_not_loaded);
54         return;
55     }
56     
57     $.ajaxSetup({
58         dataType: 'json',
59         timeout: 10000,
60     });
61     
62     
63     vehicles_source = new ol.source.Vector({
64         features: []
65     });
66     
67     popup = new ol.Overlay({
68         element: popup_element,
69         positioning: 'bottom-center',
70         stopEvent: false,
71         offset: [0, -50]
72     });
73     
74     map = new ol.Map({
75         target: 'map',
76         layers: [
77             new ol.layer.Tile({
78                 source: new ol.source.OSM()
79             }),
80             new ol.layer.Vector({
81                 source: vehicles_source,
82             }),
83         ],
84         overlays: [popup],
85         view: new ol.View({
86             center: ol.proj.fromLonLat([19.94, 50.06]),
87             zoom: 13
88         })
89     });
90     
91     // display popup on click
92     map.on('singleclick', function(evt) {
93         var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature) { return feature; });
94         if (feature) {
95             var coordinates = feature.getGeometry().getCoordinates();
96             popup_element.innerHtml = feature.name;
97             popup.setPosition(coordinates);
98         } else {
99             popup.setPosition(undefined);
100         }
101     });
102
103     // change mouse cursor when over marker
104     map.on('pointermove', function(e) {
105         var pixel = map.getEventPixel(e.originalEvent);
106         var hit = map.hasFeatureAtPixel(pixel);
107         var target = map.getTarget();
108         if(target.style)
109             target.style.cursor = hit ? 'pointer' : '';
110     });
111 }
112
113 function updateVehicles() {
114     if(vehicles_timer)clearTimeout(vehicles_timer);
115     if(vehicles_xhr) vehicles_xhr.abort();
116     
117     vehicles_xhr = $.get(
118         ttss_base + '/geoserviceDispatcher/services/vehicleinfo/vehicles' 
119             + '?positionType=CORRECTED'
120             + '&colorType=ROUTE_BASED'
121     ).done(function(data) {
122         for(var i = 0; i < data.vehicles.length; i++) {
123             var vehicle = data.vehicles[i];
124             
125             var vehicle_feature = vehicles_source.getFeatureById(vehicle.id);
126             if(vehicle.isDeleted) {
127                 if(vehicle_feature) {
128                     vehicles_source.removeFeature(vehicle_feature);
129                 }
130                 continue;
131             }
132             
133             var vehicle_name_space = vehicle.name.indexOf(' ');
134             vehicle.line = vehicle.name.substr(0, vehicle_name_space);
135             vehicle.direction = vehicle.name.substr(vehicle_name_space+1);
136             vehicle.geometry = new ol.geom.Point(ol.proj.fromLonLat([vehicle.longitude / 3600000.0, vehicle.latitude / 3600000.0]));
137             
138             if(!vehicle_feature) {
139                 vehicle_feature = new ol.Feature(vehicle);
140                 vehicle_feature.setId(vehicle.id);
141                 vehicle_feature.setStyle(new ol.style.Style({
142                     image: new ol.style.RegularShape({
143                         fill: new ol.style.Fill({color: '#3399ff'}),
144                         stroke: new ol.style.Stroke({color: 'black', width: 1}),
145                         points: 3,
146                         radius: 12,
147                         rotation: Math.PI * 2.0 * vehicle.heading / 360.0,
148                         angle: 0
149                     }),
150                     text: new ol.style.Text({
151                         text: vehicle.line,
152                         fill: new ol.style.Fill({color: 'white'}),
153                     }),
154                 }));
155                 vehicles_source.addFeature(vehicle_feature);
156             } else {
157                 vehicle_feature.setProperties(vehicle);
158             }
159         }
160         
161         vehicles_timer = setTimeout(function(){ 
162             updateVehicles();
163         }, ttss_refresh);
164     }).fail(fail_ajax);
165 }
166
167 init();
168 updateVehicles();
169 </script>
170 </body>
171 </html>