ulogger-server/js/api_openlayers.js

205 lines
7.1 KiB
JavaScript
Raw Normal View History

2017-01-30 21:36:44 +01:00
/* μlogger
2013-06-21 11:15:09 +02:00
*
2017-01-30 21:36:44 +01:00
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
2013-06-21 11:15:09 +02:00
*
* This is free software; you can redistribute it and/or modify it under
2017-04-07 00:05:28 +02:00
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2013-06-21 11:15:09 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
2017-04-07 00:05:28 +02:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
2013-06-21 11:15:09 +02:00
*/
2013-06-21 11:15:09 +02:00
// openlayers
var map;
var layerTrack;
var layerMarkers;
2017-04-09 23:35:55 +02:00
var lineStyle = { strokeColor: '#FF0000', strokeOpacity: 1, strokeWidth: 2 };
2013-06-21 11:15:09 +02:00
var wgs84;
var mercator;
var loadedAPI = 'openlayers';
function init() {
wgs84 = new OpenLayers.Projection('EPSG:4326'); // from WGS 1984
2017-04-09 23:35:55 +02:00
mercator = new OpenLayers.Projection('EPSG:900913'); // to Mercator
var options = {
controls: [
new OpenLayers.Control.ArgParser(), // default
new OpenLayers.Control.Attribution(), // default
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.Navigation(), // default
new OpenLayers.Control.PanZoomBar(),// do we need it?
new OpenLayers.Control.ScaleLine()
]
};
map = new OpenLayers.Map('map-canvas', options);
// default layer: OpenStreetMap
var mapnik = new OpenLayers.Layer.OSM('OpenStreetMap',
2017-04-09 23:35:55 +02:00
['//a.tile.openstreetmap.org/${z}/${x}/${y}.png',
'//b.tile.openstreetmap.org/${z}/${x}/${y}.png',
'//c.tile.openstreetmap.org/${z}/${x}/${y}.png']);
map.addLayer(mapnik);
2017-04-09 23:35:55 +02:00
if (layer_ocm == 1) {
// OpenCycleMap
2013-06-23 00:17:28 +02:00
var ocm = new OpenLayers.Layer.OSM('OpenCycleMap',
['//a.tile.thunderforest.com/cycle/${z}/${x}/${y}.png',
2017-04-09 23:35:55 +02:00
'//b.tile.thunderforest.com/cycle/${z}/${x}/${y}.png',
'//c.tile.thunderforest.com/cycle/${z}/${x}/${y}.png']);
2013-06-23 00:17:28 +02:00
map.addLayer(ocm);
}
2017-04-09 23:35:55 +02:00
if (layer_mq == 1) {
// MapQuest-OSM
2013-06-23 00:17:28 +02:00
var mq = new OpenLayers.Layer.OSM('MapQuest-OSM',
['//otile1.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg',
2017-04-09 23:35:55 +02:00
'//otile2.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg',
'//otile3.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg',
'//otile4.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg']);
2013-06-23 00:17:28 +02:00
map.addLayer(mq);
}
2017-04-09 23:35:55 +02:00
if (layer_osmapa == 1) {
// osmapa.pl
var osmapa = new OpenLayers.Layer.OSM('osmapa.pl',
['//a.osm.trail.pl/osmapa.pl/${z}/${x}/${y}.png',
2017-04-09 23:35:55 +02:00
'//b.osm.trail.pl/osmapa.pl/${z}/${x}/${y}.png',
'//c.osm.trail.pl/osmapa.pl/${z}/${x}/${y}.png']);
2013-06-23 00:17:28 +02:00
map.addLayer(osmapa);
}
2017-04-09 23:35:55 +02:00
if (layer_ump == 1) {
// UMP
var ump = new OpenLayers.Layer.OSM('UMP',
['//1.tiles.ump.waw.pl/ump_tiles/${z}/${x}/${y}.png',
2017-04-09 23:35:55 +02:00
'//2.tiles.ump.waw.pl/ump_tiles/${z}/${x}/${y}.png',
'//3.tiles.ump.waw.pl/ump_tiles/${z}/${x}/${y}.png']);
2013-06-23 23:43:09 +02:00
map.addLayer(ump);
}
2017-04-09 23:35:55 +02:00
var position = new OpenLayers.LonLat(init_longitude, init_latitude).transform(wgs84, mercator);
var zoom = 8;
map.setCenter(position, zoom);
2013-06-23 00:17:28 +02:00
// init layers
2013-06-23 23:43:09 +02:00
layerTrack = new OpenLayers.Layer.Vector('Track');
layerMarkers = new OpenLayers.Layer.Markers('Markers');
2013-06-21 11:15:09 +02:00
}
2017-04-09 23:35:55 +02:00
function displayTrack(xml, update) {
2013-06-21 11:15:09 +02:00
altitudes.length = 0;
var totalMeters = 0;
var totalSeconds = 0;
var points = new Array();
var latlngbounds = new OpenLayers.Bounds();
var positions = xml.getElementsByTagName('position');
var posLen = positions.length;
2017-04-09 23:35:55 +02:00
for (var i = 0; i < posLen; i++) {
2013-06-21 11:15:09 +02:00
var p = parsePosition(positions[i]);
totalMeters += p.distance;
totalSeconds += p.seconds;
p['totalMeters'] = totalMeters;
p['totalSeconds'] = totalSeconds;
// set marker
2017-04-09 23:35:55 +02:00
setMarker(p, i, posLen);
2013-06-21 11:15:09 +02:00
// update polyline
2017-04-09 23:35:55 +02:00
var point = new OpenLayers.Geometry.Point(p.longitude, p.latitude).transform(wgs84, mercator);
2013-06-21 11:15:09 +02:00
latlngbounds.extend(point);
points.push(point);
}
var lineString = new OpenLayers.Geometry.LineString(points);
var lineFeature = new OpenLayers.Feature.Vector(lineString, null, lineStyle);
layerTrack.addFeatures([lineFeature]);
map.addLayer(layerTrack);
map.addLayer(layerMarkers);
2013-06-21 11:15:09 +02:00
if (update) {
map.zoomToExtent(latlngbounds);
2017-04-09 23:35:55 +02:00
if (i == 1) {
// only one point, zoom out
2013-06-21 11:15:09 +02:00
map.zoomOut();
}
}
2017-05-22 13:39:40 +02:00
updateSummary(p.timestamp, totalMeters, totalSeconds);
2017-04-09 23:35:55 +02:00
if (p.tid != trackid) {
trackid = p.tid;
2013-06-21 11:15:09 +02:00
setTrack(trackid);
}
2017-04-09 23:35:55 +02:00
if (document.getElementById('bottom').style.display == 'block') {
2013-06-21 11:15:09 +02:00
// update altitudes chart
chart.clearChart();
displayChart();
}
}
2013-06-23 00:17:28 +02:00
function clearMap() {
2017-04-09 23:35:55 +02:00
if (layerTrack) {
2013-06-21 11:15:09 +02:00
layerTrack.removeAllFeatures();
}
2017-04-09 23:35:55 +02:00
if (layerMarkers) {
2013-06-21 11:15:09 +02:00
layerMarkers.clearMarkers();
}
}
2017-04-09 23:35:55 +02:00
function setMarker(p, i, posLen) {
2013-06-21 11:15:09 +02:00
// marker
2017-04-09 23:35:55 +02:00
var lonLat = new OpenLayers.LonLat(p.longitude, p.latitude).transform(wgs84, mercator);
2013-06-21 11:15:09 +02:00
var size = new OpenLayers.Size(21, 25);
2017-04-09 23:35:55 +02:00
var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
if (latest == 1) { var icon = new OpenLayers.Icon('//www.openstreetmap.org/openlayers/img/marker.png', size, offset); }
else if (i == 0) { var icon = new OpenLayers.Icon('//www.openstreetmap.org/openlayers/img/marker-green.png', size, offset); }
else if (i == posLen - 1) { var icon = new OpenLayers.Icon('//www.openstreetmap.org/openlayers/img/marker.png', size, offset); }
else {
size = new OpenLayers.Size(12, 20);
2017-04-09 23:35:55 +02:00
offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
var icon = new OpenLayers.Icon('//maps.gstatic.com/mapfiles/ridefinder-images/mm_20_gray.png', size, offset);
2013-06-21 11:15:09 +02:00
}
2017-04-09 23:35:55 +02:00
var marker = new OpenLayers.Marker(lonLat, icon);
layerMarkers.addMarker(marker);
2013-06-21 11:15:09 +02:00
// popup
2017-04-09 23:35:55 +02:00
var content = getPopupHtml(p, i, posLen);
marker.events.register("mousedown", marker, (function () {
return function () {
// remove popups
if (map.popups.length > 0) {
for (var j = map.popups.length - 1; j >= 0; j--) {
map.removePopup(map.popups[j])
};
}
// show popup
var popup = new OpenLayers.Popup.FramedCloud("popup_" + (i + 1), lonLat, null, content, icon, true);
map.addPopup(popup);
if (document.getElementById('bottom').style.display == 'block') {
chart.setSelection([{ row: i, column: null }]);
}
}
})());
2013-06-21 11:15:09 +02:00
}
function addChartEvent(chart) {
2017-04-09 23:35:55 +02:00
google.visualization.events.addListener(chart, 'select', function () {
var selection = chart.getSelection()[0];
if (selection) {
var id = selection.row;
var marker = layerMarkers.markers[id];
var url = marker.icon.url;
marker.setUrl('//www.openstreetmap.org/openlayers/img/marker-gold.png');
2017-04-09 23:35:55 +02:00
altTimeout = setTimeout(function () { marker.setUrl(url); }, 2000);
}
});
2013-06-21 11:15:09 +02:00
}
//20.597985430276808,52.15547181298076,21.363595171488573,52.33750879522563
function getBounds() {
2017-04-09 23:35:55 +02:00
var b = map.getExtent().transform(mercator, wgs84).toString();
var bounds = b.split(',', 4);
var lon_sw = bounds[0];
var lat_sw = bounds[1];
var lon_ne = bounds[2];
var lat_ne = bounds[3];
2017-04-09 23:35:55 +02:00
return [lon_sw, lat_sw, lon_ne, lat_ne];
}
function zoomToBounds(b) {
2017-04-09 23:35:55 +02:00
var bounds = new OpenLayers.Bounds(b).transform(wgs84, mercator);
map.zoomToExtent(bounds);
}