ulogger-server/js/api_openlayers.js

432 lines
11 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
*/
2018-11-09 16:16:26 +01:00
// openlayers 3+
2013-06-21 11:15:09 +02:00
var map;
var layerTrack;
var layerMarkers;
2018-11-09 16:16:26 +01:00
var selectedLayer;
var olStyles;
2013-06-21 11:15:09 +02:00
var loadedAPI = 'openlayers';
2013-06-21 11:15:09 +02:00
function init() {
2018-11-09 16:16:26 +01:00
addCss('css/ol.css', 'ol_css');
var controls = [
new ol.control.Zoom(),
new ol.control.Rotate(),
new ol.control.ScaleLine(),
new ol.control.ZoomToExtent({ label: getExtentImg() }),
];
var view = new ol.View({
center: ol.proj.fromLonLat([init_longitude, init_latitude]),
zoom: 8
});
map = new ol.Map({
target: 'map-canvas',
controls: controls,
view: view
});
// default layer: OpenStreetMap
2018-11-09 16:16:26 +01:00
var osm = new ol.layer.Tile({
name: 'OpenStreetMap',
visible: true,
source: new ol.source.OSM()
});
map.addLayer(osm);
2018-11-09 16:16:26 +01:00
selectedLayer = osm;
// add extra layers
for (var layerName in ol_layers) {
if (ol_layers.hasOwnProperty(layerName)) {
var layerUrl = ol_layers[layerName];
2018-11-09 16:16:26 +01:00
var ol_layer = new ol.layer.Tile({
name: layerName,
visible: false,
source: new ol.source.XYZ({
url: layerUrl
})
});
map.addLayer(ol_layer);
}
}
2013-06-23 00:17:28 +02:00
// init layers
2018-11-09 16:16:26 +01:00
var lineStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: hexToRGBA(strokeColor, strokeOpacity),
width: strokeWeight
})
});
layerTrack = new ol.layer.Vector({
name: 'Track',
type: 'data',
source: new ol.source.Vector(),
style: lineStyle
});
layerMarkers = new ol.layer.Vector({
name: 'Markers',
type: 'data',
source: new ol.source.Vector()
});
map.addLayer(layerTrack);
map.addLayer(layerMarkers);
// styles
olStyles = {};
var iconRed = new ol.style.Icon({
anchor: [ 0.5, 1 ],
src: 'images/marker-red.png'
});
var iconGreen = new ol.style.Icon({
anchor: [ 0.5, 1 ],
src: 'images/marker-green.png'
});
var iconWhite = new ol.style.Icon({
anchor: [ 0.5, 1 ],
opacity: 0.7,
src: 'images/marker-white.png'
});
var iconGold = new ol.style.Icon({
anchor: [ 0.5, 1 ],
src: 'images/marker-gold.png'
});
olStyles['red'] = new ol.style.Style({
image: iconRed
});
olStyles['green'] = new ol.style.Style({
image: iconGreen
});
olStyles['white'] = new ol.style.Style({
image: iconWhite
});
olStyles['gold'] = new ol.style.Style({
image: iconGold
});
// popups
var popupContainer = document.createElement('div');
popupContainer.id = 'popup';
popupContainer.className = 'ol-popup';
document.getElementsByTagName('body')[0].appendChild(popupContainer);
var popupCloser = document.createElement('a');
popupCloser.id = 'popup-closer';
popupCloser.className = 'ol-popup-closer';
popupCloser.href = '#';
popupContainer.appendChild(popupCloser);
var popupContent = document.createElement('div');
popupContent.id = 'popup-content';
popupContainer.appendChild(popupContent);
var popup = new ol.Overlay({
element: popupContainer,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
popupCloser.onclick = function() {
popup.setPosition(undefined);
popupCloser.blur();
return false;
};
// add click handler to map to show popup
map.on('click', function(e) {
var coordinate = e.coordinate;
var feature = map.forEachFeatureAtPixel(e.pixel,
function(feature, layer) {
if (layer.get('name') == 'Markers') {
return feature;
}
});
if (feature) {
var p = feature.get('p');
var i = feature.getId();
var posLen = feature.get('posLen');
// popup show
popup.setPosition(coordinate);
popupContent.innerHTML = getPopupHtml(p, i, posLen);
map.addOverlay(popup);
if (document.getElementById('bottom').style.display == 'block') {
var index = 0;
for (var key in altitudes) {
if (altitudes.hasOwnProperty(key) && key == i) {
chart.setSelection([{ row: index, column: null }]);
break;
}
index++;
}
}
} else {
// popup destroy
popup.setPosition(undefined);
}
});
// change mouse cursor when over marker
2018-11-09 20:39:28 +01:00
map.on('pointermove', function(e) {
2018-11-09 16:16:26 +01:00
var hit = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
if (layer.get('name') == 'Markers') {
return true;
} else {
return false;
}
});
if (hit) {
this.getTargetElement().style.cursor = 'pointer';
} else {
this.getTargetElement().style.cursor = '';
}
});
// layer switcher
var switcher = document.createElement('div');
switcher.id = 'switcher';
switcher.className = 'ol-control';
document.getElementsByTagName('body')[0].appendChild(switcher);
var switcherContent = document.createElement('div');
switcherContent.id = 'switcher-content';
switcherContent.className = 'ol-layerswitcher';
switcher.appendChild(switcherContent);
map.getLayers().forEach(function (layer) {
var layerLabel = document.createElement('label');
layerLabel.innerHTML = layer.get('name');
switcherContent.appendChild(layerLabel);
var layerRadio = document.createElement('input');
if (layer.get('type') === 'data') {
layerRadio.type = 'checkbox';
layerLabel.className = 'ol-datalayer';
} else {
layerRadio.type = 'radio';
}
layerRadio.name = 'layer';
layerRadio.value = layer.get('name');
layerRadio.onclick = switchLayer;
if (layer.getVisible()) {
layerRadio.checked = true;
}
layerLabel.insertBefore(layerRadio, layerLabel.childNodes[0]);
});
function switchLayer() {
var layerName = this.value;
map.getLayers().forEach(function (layer) {
if (layer.get('name') === layerName) {
if (layer.get('type') === 'data') {
if (layer.getVisible()) {
layer.setVisible(false);
} else {
layer.setVisible(true);
}
} else {
selectedLayer.setVisible(false);
selectedLayer = layer;
layer.setVisible(true);
}
return;
}
});
};
var switcherButton = document.createElement('button');
var layerImg = document.createElement('img');
layerImg.src = 'images/layers.svg';
layerImg.style.width = '60%';
switcherButton.appendChild(layerImg);
var switcherHandle = function() {
var el = document.getElementById('switcher');
if (el.style.display === 'block') {
el.style.display = 'none';
} else {
el.style.display = 'block';
}
};
switcherButton.addEventListener('click', switcherHandle, false);
switcherButton.addEventListener('touchstart', switcherHandle, false);
var element = document.createElement('div');
element.className = 'ol-switcher-button ol-unselectable ol-control';
element.appendChild(switcherButton);
var switcherControl = new ol.control.Control({
element: element
});
map.addControl(switcherControl);
2013-06-21 11:15:09 +02:00
}
function cleanup() {
map = undefined;
layerTrack = undefined;
layerMarkers = undefined;
2018-11-09 16:16:26 +01:00
selectedLayer = undefined;
olStyles = undefined;
removeElementById('popup');
removeElementById('switcher');
2018-11-09 20:39:28 +01:00
document.getElementById('map-canvas').innerHTML = '';
}
2017-04-09 23:35:55 +02:00
function displayTrack(xml, update) {
altitudes = {};
2013-06-21 11:15:09 +02:00
var totalMeters = 0;
var totalSeconds = 0;
var points = [];
2013-06-21 11:15:09 +02:00
var positions = xml.getElementsByTagName('position');
var posLen = positions.length;
2017-04-09 23:35:55 +02:00
for (var i = 0; i < posLen; i++) {
var p = parsePosition(positions[i], i);
2013-06-21 11:15:09 +02:00
totalMeters += p.distance;
totalSeconds += p.seconds;
p.totalMeters = totalMeters;
p.totalSeconds = totalSeconds;
2013-06-21 11:15:09 +02:00
// set marker
2017-04-09 23:35:55 +02:00
setMarker(p, i, posLen);
2013-06-21 11:15:09 +02:00
// update polyline
2018-11-09 16:16:26 +01:00
var point = ol.proj.fromLonLat([p.longitude, p.latitude]);
2013-06-21 11:15:09 +02:00
points.push(point);
}
2018-11-09 16:16:26 +01:00
var lineString = new ol.geom.LineString(points);
var lineFeature = new ol.Feature({
geometry: lineString,
});
layerTrack.getSource().addFeature(lineFeature);
var extent = layerTrack.getSource().getExtent();
map.getControls().forEach(function (el) {
if (el instanceof ol.control.ZoomToExtent) {
map.removeControl(el);
}
});
2013-06-21 11:15:09 +02:00
if (update) {
2018-11-09 16:16:26 +01:00
map.getView().fit(extent);
var zoom = map.getView().getZoom();
if (zoom > 20) {
map.getView().setZoom(20);
extent = map.getView().calculateExtent(map.getSize());
2013-06-21 11:15:09 +02:00
}
}
2018-11-09 16:16:26 +01:00
var zoomToExtentControl = new ol.control.ZoomToExtent({
extent: extent,
label: getExtentImg()
});
map.addControl(zoomToExtentControl);
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) {
2018-11-09 16:16:26 +01:00
layerTrack.getSource().clear();
2013-06-21 11:15:09 +02:00
}
2017-04-09 23:35:55 +02:00
if (layerMarkers) {
2018-11-09 16:16:26 +01:00
layerMarkers.getSource().clear();
2013-06-21 11:15:09 +02:00
}
}
2017-04-09 23:35:55 +02:00
function setMarker(p, i, posLen) {
2013-06-21 11:15:09 +02:00
// marker
2018-11-09 16:16:26 +01:00
var marker = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([p.longitude, p.latitude]))
});
if (latest == 1) {
var iconStyle = olStyles['red'];
} else if (i == 0) {
var iconStyle = olStyles['green'];
} else if (i == posLen - 1) {
var iconStyle = olStyles['red'];
} else {
var iconStyle = olStyles['white'];
2013-06-21 11:15:09 +02:00
}
2018-11-09 16:16:26 +01:00
marker.setStyle(iconStyle);
marker.setId(i);
marker.set('p', p);
marker.set('posLen', posLen);
layerMarkers.getSource().addFeature(marker);
2013-06-21 11:15:09 +02:00
}
function addChartEvent(chart, data) {
2017-04-09 23:35:55 +02:00
google.visualization.events.addListener(chart, 'select', function () {
var selection = chart.getSelection()[0];
if (selection) {
var id = data.getValue(selection.row, 0) - 1;
2018-11-09 16:16:26 +01:00
var marker = layerMarkers.getSource().getFeatureById(id);
var url = marker.get('src');
var initStyle = marker.getStyle();
var iconStyle = olStyles['gold'];
marker.setStyle(iconStyle);
altTimeout = setTimeout(function () { marker.setStyle(initStyle); }, 2000);
}
});
2013-06-21 11:15:09 +02:00
}
2018-11-09 16:16:26 +01:00
//20.597985430276808,52.15547181298076,21.363595171488573,52.33750879522563
function getBounds() {
2018-11-09 16:16:26 +01:00
var extent = map.getView().calculateExtent(map.getSize());
var bounds = ol.proj.transformExtent(extent, 'EPSG:900913', 'EPSG:4326');
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 zoomToExtent() {
map.getView().fit(layerMarkers.getSource().getExtent())
}
function zoomToBounds(b) {
2018-11-09 16:16:26 +01:00
var bounds = ol.proj.transformExtent(b, 'EPSG:4326', 'EPSG:900913');
map.getView().fit(bounds);
}
2018-11-09 16:16:26 +01:00
function updateSize() {
map.updateSize();
}
2018-11-09 16:16:26 +01:00
function getExtentImg() {
var extentImg = document.createElement('img');
extentImg.src = 'images/extent.svg';
extentImg.style.width = '60%';
return extentImg;
2018-01-29 18:52:46 +01:00
}