ulogger-server/js/api_gmaps.js

183 lines
5.5 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 = new Array();
var markers = new Array();
var popups = new Array();
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: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
}
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
};
2013-06-21 11:15:09 +02:00
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
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;
// 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++) {
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;
2017-04-09 23:35:55 +02:00
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);
// save altitudes for chart
altitudes[i] = p.altitude;
}
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
}
}
latestTime = p.dateoccured;
polies.push(poly);
2017-04-09 23:35:55 +02:00
updateSummary(p.dateoccured, totalMeters, totalSeconds);
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;
}
var popup;
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: p.coordinates,
title: p.dateoccured
});
2017-04-09 23:35:55 +02:00
if (latest == 1) { marker.setIcon('//maps.google.com/mapfiles/dd-end.png') }
else if (i == 0) { marker.setIcon('//maps.google.com/mapfiles/marker_greenA.png') }
else if (i == posLen - 1) { marker.setIcon('//maps.google.com/mapfiles/markerB.png') }
else { marker.setIcon('//maps.gstatic.com/mapfiles/ridefinder-images/mm_20_gray.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') {
chart.setSelection([{ row: i, column: null }]);
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) {
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 = selection.row;
var icon = markers[id].getIcon();
markers[id].setIcon('//maps.google.com/mapfiles/marker_orange.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() {
2017-04-09 23:35:55 +02:00
var b = map.getBounds().toString();
var bounds = b.split(',', 4);
var lat_sw = bounds[0].replace(/\(/g, '');
var lon_sw = bounds[1].replace(/[ )]/g, '');
var lat_ne = bounds[2].replace(/[ (]/g, '');
var lon_ne = bounds[3].replace(/[ )]/g, '');
return [lon_sw, lat_sw, lon_ne, lat_ne];
}
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;
message = sprintf(lang['apifailure'], "Google Maps");
message += '<br><br>' + lang['gmauthfailure'];
message += '<br><br>' + lang['gmapilink'];
showModal(message);
};