Improved www.ttss.krakow.pl
Jacek Kowalski
2017-04-14 b1154d8239000ea2593285e9ead9b7920653289d
commit | author | age
e5c5bd 1 <!DOCTYPE HTML>
JK 2 <html>
3 <head>
8338a5 4 <title>MPK Kraków Map</title>
e5c5bd 5 <style type="text/css">
JK 6 html, body, #map {
7     width: 100%;
8     height: 100%;
9     margin: 0;
10 }
8338a5 11 #popup {
JK 12     color: black;
13     background: white;
14     padding: 5px;
15     border: 1px solid black;
16     border-radius: 10px;
17     font: 12px sans-serif;
18 }
19 #popup p {
20     margin: 0;
21     padding: 5px;
22 }
23 #popup .line-direction {
24     font-weight: bold;
25 }
e5c5bd 26 </style>
8338a5 27 <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
e5c5bd 28 <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList"></script>
8338a5 29 <script src="https://openlayers.org/en/v4.0.1/build/ol.js" integrity="sha384-DjqoiOTAYD0bxdY6K1Ep7Xd4QQc46AKzKmkgC4+rnKX47PniPHIjRt02uhT8C04h" crossorigin="anonymous"></script>
e5c5bd 30 <script tyle="text/javascript" src="lang_en.js" id="lang_script"></script>
8338a5 31 <script tyle="text/javascript" src="common.js"></script>
e5c5bd 32 </head>
JK 33 <body>
34 <div id="map"><div id="popup"></div></div>
35
36 <script tyle="text/javascript">
37 //var ttss_base = 'http://www.ttss.krakow.pl/internetservice';
38 var ttss_base = '/proxy.php';
39 var ttss_refresh = 5000; // 5 seconds
40
41 var vehicles_source = null;
42 var vehicles_xhr = null;
43 var vehicles_timer = null;
8338a5 44 var vehicles_last_update = 0;
e5c5bd 45
JK 46 var map = null;
8338a5 47 var popup_feature_id = null;
e5c5bd 48 var popup_element = document.getElementById('popup');
JK 49 var popup = null;
50
51 function fail(msg) {
52     console.log(msg);
53     alert(msg);
54 }
55
56 function fail_ajax(data) {
57     // abort() is not a failure
58     if(data.readyState == 0 && data.statusText == 'abort') return;
59     
60     if(data.status == 0) {
61         fail(lang.error_request_failed_connectivity, data);
62     } else if (data.statusText) {
63         fail(lang.error_request_failed_status.replace('$status', data.statusText), data);
64     } else {
65         fail(lang.error_request_failed, data);
66     }
67 }
68
d696cf 69 function popupHide() {
JK 70     popup.setPosition(undefined);
71     popup_feature_id = null;
72 }
73
74 function popupShow(coordinates, id) {
75     popup.setPosition(coordinates);
76     if(id) {
77         popup_feature_id = id;
78     }
79 }
80
e5c5bd 81 function init() {
JK 82     if(!window.jQuery) {
83         fail(lang.jquery_not_loaded);
84         return;
85     }
86     
87     $.ajaxSetup({
88         dataType: 'json',
89         timeout: 10000,
90     });
91     
92     
93     vehicles_source = new ol.source.Vector({
94         features: []
95     });
96     
97     popup = new ol.Overlay({
98         element: popup_element,
99         positioning: 'bottom-center',
100         stopEvent: false,
8338a5 101         offset: [0, -12]
e5c5bd 102     });
JK 103     
104     map = new ol.Map({
105         target: 'map',
106         layers: [
107             new ol.layer.Tile({
108                 source: new ol.source.OSM()
109             }),
110             new ol.layer.Vector({
111                 source: vehicles_source,
112             }),
113         ],
114         overlays: [popup],
115         view: new ol.View({
116             center: ol.proj.fromLonLat([19.94, 50.06]),
117             zoom: 13
118         })
119     });
120     
121     // display popup on click
d696cf 122     map.on('singleclick', function(e) {
JK 123         var feature = map.forEachFeatureAtPixel(e.pixel, function(feature) { return feature; });
8338a5 124         if(feature) {
e5c5bd 125             var coordinates = feature.getGeometry().getCoordinates();
8338a5 126             
JK 127             deleteChildren(popup_element);
128             addParaWithText(popup_element, feature.get('name')).className = 'line-direction';
129             
130             var vehicle_type = parseVehicle(feature.get('id'));
131             if(vehicle_type) {
132                 addParaWithText(popup_element, vehicle_type.num + ' ' + vehicle_type.type);
133             }
134             
d696cf 135             popupShow(coordinates, feature.getId());
e5c5bd 136         } else {
d696cf 137             popupHide();
e5c5bd 138         }
JK 139     });
140
141     // change mouse cursor when over marker
142     map.on('pointermove', function(e) {
8c66c4 143         var hit = map.hasFeatureAtPixel(e.pixel);
JK 144         var target = map.getTargetElement();
145         target.style.cursor = hit ? 'pointer' : '';
e5c5bd 146     });
JK 147 }
148
149 function updateVehicles() {
150     if(vehicles_timer)clearTimeout(vehicles_timer);
151     if(vehicles_xhr) vehicles_xhr.abort();
152     
153     vehicles_xhr = $.get(
154         ttss_base + '/geoserviceDispatcher/services/vehicleinfo/vehicles' 
155             + '?positionType=CORRECTED'
156             + '&colorType=ROUTE_BASED'
8338a5 157             + '&lastUpdate=' + encodeURIComponent(vehicles_last_update)
e5c5bd 158     ).done(function(data) {
8338a5 159         vehicles_last_update = data.lastUpdate;
JK 160         
e5c5bd 161         for(var i = 0; i < data.vehicles.length; i++) {
JK 162             var vehicle = data.vehicles[i];
163             
164             var vehicle_feature = vehicles_source.getFeatureById(vehicle.id);
165             if(vehicle.isDeleted) {
166                 if(vehicle_feature) {
167                     vehicles_source.removeFeature(vehicle_feature);
b1154d 168                     if(popup_feature_id == vehicle.id) {
JK 169                         popupHide();
170                     }
e5c5bd 171                 }
JK 172                 continue;
173             }
174             
175             var vehicle_name_space = vehicle.name.indexOf(' ');
176             vehicle.line = vehicle.name.substr(0, vehicle_name_space);
177             vehicle.direction = vehicle.name.substr(vehicle_name_space+1);
8338a5 178             if(special_directions[vehicle.direction]) {
JK 179                 vehicle.line = special_directions[vehicle.direction];
180             }
181             
e5c5bd 182             vehicle.geometry = new ol.geom.Point(ol.proj.fromLonLat([vehicle.longitude / 3600000.0, vehicle.latitude / 3600000.0]));
8338a5 183             vehicle.vehicle_type = parseVehicle(vehicle.id);
e5c5bd 184             
JK 185             if(!vehicle_feature) {
186                 vehicle_feature = new ol.Feature(vehicle);
187                 vehicle_feature.setId(vehicle.id);
8338a5 188                 
JK 189                 var color_type = 'black';
190                 if(vehicle.vehicle_type) {
191                     switch(vehicle.vehicle_type.low) {
192                         case 0:
193                             color_type = 'orange';
194                             break;
195                         case 1:
196                             color_type = 'blue';
197                             break;
198                         case 2:
199                             color_type = 'green';
200                             break;
201                     }
202                 }
203                 
e5c5bd 204                 vehicle_feature.setStyle(new ol.style.Style({
JK 205                     image: new ol.style.RegularShape({
206                         fill: new ol.style.Fill({color: '#3399ff'}),
8338a5 207                         stroke: new ol.style.Stroke({color: color_type, width: 2}),
e5c5bd 208                         points: 3,
JK 209                         radius: 12,
d4eb9e 210                         rotation: Math.PI * parseFloat(vehicle.heading) / 180.0,
JK 211                         rotateWithView: true,
e5c5bd 212                         angle: 0
JK 213                     }),
214                     text: new ol.style.Text({
8338a5 215                         font: 'bold 10px sans-serif',
e5c5bd 216                         text: vehicle.line,
JK 217                         fill: new ol.style.Fill({color: 'white'}),
218                     }),
219                 }));
220                 vehicles_source.addFeature(vehicle_feature);
221             } else {
222                 vehicle_feature.setProperties(vehicle);
d4eb9e 223                 vehicle_feature.getStyle().getImage().setRotation(Math.PI * parseFloat(vehicle.heading) / 180.0);
d696cf 224                 
JK 225                 if(popup_feature_id == vehicle_feature.getId()) {
226                     popupShow(vehicle_feature.getGeometry().getCoordinates());
8338a5 227                 }
e5c5bd 228             }
JK 229         }
230         
231         vehicles_timer = setTimeout(function(){ 
232             updateVehicles();
233         }, ttss_refresh);
234     }).fail(fail_ajax);
235 }
236
237 init();
238 updateVehicles();
239 </script>
240 </body>
241 </html>