ulogger-server/js/api_gmaps.js

209 lines
6.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
// google maps
var map;
var polies = [];
var markers = [];
var popups = [];
var popup;
2013-06-21 11:15:09 +02:00
var polyOptions;
var mapOptions;
var loadedAPI = 'gmaps';
2017-04-09 23:35:55 +02:00
2013-06-21 11:15:09 +02:00
function init() {
if (gm_error) { return gm_authFailure(); }
2013-06-23 00:17:28 +02:00
google.maps.visualRefresh = true;
2013-06-21 11:15:09 +02:00
polyOptions = {
strokeColor: strokeColor,
strokeOpacity: strokeOpacity,
strokeWeight: strokeWeight
}
2013-06-21 11:15:09 +02:00
mapOptions = {
2017-04-09 23:35:55 +02:00
center: new google.maps.LatLng(init_latitude, init_longitude),
2013-06-21 11:15:09 +02:00
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scaleControl: true
};
2018-11-09 20:39:28 +01:00
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
2013-06-21 11:15:09 +02:00
}
function cleanup() {
map = undefined;
polies = undefined;
markers = undefined;
popups = undefined;
popup = undefined;
polyOptions = undefined;
mapOptions = undefined;
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;
// init polyline
var poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
var path = poly.getPath();
2017-04-09 23:35:55 +02:00
var latlngbounds = new google.maps.LatLngBounds();
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;
p.coordinates = new google.maps.LatLng(p.latitude, p.longitude);
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
path.push(p.coordinates);
2013-06-21 11:15:09 +02:00
latlngbounds.extend(p.coordinates);
}
2013-06-21 11:15:09 +02:00
if (update) {
map.fitBounds(latlngbounds);
2017-04-09 23:35:55 +02:00
if (i == 1) {
2013-06-21 11:15:09 +02:00
// only one point, zoom out
zListener =
2017-04-09 23:35:55 +02:00
google.maps.event.addListenerOnce(map, 'bounds_changed', function (event) {
if (this.getZoom()) {
this.setZoom(15);
}
});
setTimeout(function () { google.maps.event.removeListener(zListener) }, 2000);
2013-06-21 11:15:09 +02:00
}
}
polies.push(poly);
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 (polies) {
for (var i = 0; i < polies.length; i++) {
2013-06-21 11:15:09 +02:00
polies[i].setMap(null);
}
}
2017-04-09 23:35:55 +02:00
if (markers) {
for (var i = 0; i < markers.length; i++) {
2013-06-21 11:15:09 +02:00
google.maps.event.removeListener(popups[i].listener);
popups[i].setMap(null);
markers[i].setMap(null);
}
}
markers.length = 0;
polies.length = 0;
popups.lentgth = 0;
}
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 marker = new google.maps.Marker({
2013-06-21 11:15:09 +02:00
map: map,
position: new google.maps.LatLng(p.latitude, p.longitude),
2017-05-22 13:39:40 +02:00
title: (new Date(p.timestamp * 1000)).toLocaleString()
2013-06-21 11:15:09 +02:00
});
if (latest == 1) { marker.setIcon('images/marker-red.png') }
else if (i == 0) { marker.setIcon('images/marker-green.png') }
else if (i == posLen - 1) { marker.setIcon('images/marker-red.png') }
else { marker.setIcon('images/marker-white.png') }
2013-06-21 11:15:09 +02:00
// popup
2017-04-09 23:35:55 +02:00
var content = getPopupHtml(p, i, posLen);
2013-06-21 11:15:09 +02:00
popup = new google.maps.InfoWindow();
2017-04-09 23:35:55 +02:00
popup.listener = google.maps.event.addListener(marker, 'click', (function (marker, content) {
return function () {
2013-06-21 11:15:09 +02:00
popup.setContent(content);
popup.open(map, marker);
2017-04-09 23:35:55 +02:00
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++;
}
2013-06-21 11:15:09 +02:00
}
}
2017-04-09 23:35:55 +02:00
})(marker, content));
markers.push(marker);
popups.push(popup);
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 () {
if (popup) { popup.close(); clearTimeout(altTimeout); }
var selection = chart.getSelection()[0];
if (selection) {
var id = data.getValue(selection.row, 0) - 1;
var icon = markers[id].getIcon();
markers[id].setIcon('images/marker-gold.png');
2017-04-09 23:35:55 +02:00
altTimeout = setTimeout(function () { markers[id].setIcon(icon); }, 2000);
}
});
2013-06-21 11:15:09 +02:00
}
2017-04-09 23:35:55 +02:00
//((52.20105108685229, 20.789387865580238), (52.292069558807135, 21.172192736185707))
function getBounds() {
var bounds = map.getBounds();
var lat_sw = bounds.getSouthWest().lat();
var lon_sw = bounds.getSouthWest().lng();
var lat_ne = bounds.getNorthEast().lat();
var lon_ne = bounds.getNorthEast().lng();
2017-04-09 23:35:55 +02:00
return [lon_sw, lat_sw, lon_ne, lat_ne];
}
function zoomToExtent() {
var latlngbounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
var coordinates = new google.maps.LatLng(markers[i].position.lat(), markers[i].position.lng());
latlngbounds.extend(coordinates);
}
map.fitBounds(latlngbounds);
}
function zoomToBounds(b) {
2017-04-09 23:35:55 +02:00
var sw = new google.maps.LatLng(b[1], b[0]);
var ne = new google.maps.LatLng(b[3], b[2]);
var bounds = new google.maps.LatLngBounds(sw, ne);
map.fitBounds(bounds);
}
function gm_authFailure() {
gm_error = true;
2018-11-09 20:39:28 +01:00
message = sprintf(lang['apifailure'], 'Google Maps');
message += '<br><br>' + lang['gmauthfailure'];
message += '<br><br>' + lang['gmapilink'];
showModal(message);
};
2018-01-29 18:52:46 +01:00
function updateSize() {
// ignore
}