1
0

Moved marker/polyline code to MarkerMgr class. Use location_id instead

of epoch for operations.
This commit is contained in:
Markus Birth 2018-06-02 22:18:16 +02:00
parent aa5a9946b7
commit cd85618672
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
5 changed files with 132 additions and 176 deletions

View File

@ -194,18 +194,18 @@ window.setLiveMap = ->
else
$('#livemap_on').addClass('btn-default').removeClass('btn-primary').removeClass 'active'
window.geodecodeMarker = (tid, i) ->
console.log 'geodecodeMarker: %o, %o', tid, i
window.mymap.geodecodeMarker tid, i
window.geodecodeMarker = (lid) ->
console.log 'geodecodeMarker: %o', lid
window.mymap.geodecodeMarker lid
window.deleteMarker = (tid, i) ->
console.log 'deleteMarker: %o, %o', tid, i
if confirm "Do you really want to delete this marker for #{tid}?"
window.deleteMarker = (lid) ->
console.log 'deleteMarker: %o', lid
if confirm "Do you really want to delete this marker?"
console.log 'deleteMarker: Confirmation given'
window.mymap.deleteMarker tid, i
window.mymap.deleteMarker lid
window.showBoundingBox = (tid, i) ->
console.log 'showBoundingBox: %o, %o', tid, i
window.showBoundingBox = (lid) ->
console.log 'showBoundingBox: %o', lid
console.warn 'NOT YET IMPLEMENTED'
window.updateTrackerIDs = ->

View File

@ -7,6 +7,8 @@ class window.MarkerMgr
@dateFrom = null
@dateTo = null
@accuracy = null
@markers_drawn = {}
@lines_drawn = {}
fetchMarkers: (dateFromYMD, dateToYMD, accuracy) ->
console.log 'MarkerMgr::fetchMarkers(%o, %o, %o)', dateFromYMD, dateToYMD, accuracy
@ -28,10 +30,24 @@ class window.MarkerMgr
return @markers
getMarkerBounds: ->
pass
max_lat = -90
min_lat = 90
max_lon = -180
min_lon = 180
getMarkerTooltip: (mid, marker) ->
trackerIDString = "<br/>TrackerID: #{marker.tracker_id} / #{mid}"
for tid, tidmarkers of @markers
# TODO: Implement some way of filtering by tid
for tidmarker, i in tidmarkers
if max_lat < tidmarker.latitude then max_lat = tidmarker.latitude
if min_lat > tidmarker.latitude then min_lat = tidmarker.latitude
if max_lon < tidmarker.longitude then max_lon = tidmarker.longitude
if min_lon > tidmarker.longitude then min_lon = tidmarker.longitude
return [[min_lat, min_lon], [max_lat, max_lon]]
getMarkerTooltip: (marker) ->
console.log 'MarkerMgr::getMarkerTooltip(%o)', marker
trackerIDString = "<br/>TrackerID: #{marker.tracker_id} / #{marker.lid}"
dateString = marker.dt
if marker.epoch != 0
newDate = new Date()
@ -43,24 +59,90 @@ class window.MarkerMgr
velocityString = if marker.velocity? then "<br/>Velocity: #{marker.velocity} km/h" else ''
locationString = ''
if marker.display_name?
locationString = "<br/>Location: <a href=\"#\" onclick=\"showBoundingBox('#{marker.tracker_id}', #{mid});\" title=\"Show location bounding box\">#{marker.display_name}</a>"
locationString = "<br/>Location: <a href=\"#\" onclick=\"showBoundingBox(#{marker.lid});\" title=\"Show location bounding box\">#{marker.display_name}</a>"
else
locationString = "<br/>Location: <span id=\"loc_#{marker.tracker_id}_#{mid}\"><a href=\"#\" onclick=\"geodecodeMarker('#{marker.tracker_id}', #{mid});\" title=\"Get location (geodecode)\">Get location</a></span>"
locationString = "<br/>Location: <span id=\"loc_#{marker.lid}\"><a href=\"#\" onclick=\"geodecodeMarker(#{marker.lid});\" title=\"Get location (geodecode)\">Get location</a></span>"
removeString = "<br/><br/><a href=\"#\" onclick=\"deleteMarker('#{marker.tracker_id}', #{mid});\">Delete marker</a>"
removeString = "<br/><br/><a href=\"#\" onclick=\"deleteMarker(#{marker.lid});\">Delete marker</a>"
# prepare popup HTML code for marker
popupString = dateString + trackerIDString + accuracyString + headingString + velocityString + locationString + removeString
return popupString
getMarkerIcon: (tid, icon_is_first, icon_is_last) ->
console.log 'MarkerMgr::getMarkerIcon(%o, %o, %o)', tid, icon_is_first, icon_is_last
# TODO: make colour depending on tid?
#colours = ['blue', 'red', 'orange', 'green', 'purple', 'cadetblue', 'darkred', 'darkgreen', 'darkpurple']
#for fg, i in colours
# bg1 = if fg is 'green' then 'darkgreen' else 'green'
# bg2 = if fg is 'red' then 'darkred' else 'red'
colour = 'blue'
icon_type = 'user'
icon_colour = 'white'
if icon_is_first
icon_type = 'flag'
icon_colour = 'green'
else if icon_is_last
icon_type = 'flag-checkered'
icon_colour = 'red'
marker_icon = L.AwesomeMarkers.icon
icon: icon_type
prefix: 'fa'
markerColor: colour
iconColor: icon_colour
return marker_icon
addMarkersTo: (map) ->
console.log 'MarkerMgr::addMarkersTo(%o)', map
mapid = map.getContainer().id
if not @markers_drawn[mapid]?
@markers_drawn[mapid] = []
for tid, tidmarkers of @markers
# TODO: Implement some way of filtering by tid
for i, tidmarker of tidmarkers
tooltip_txt = @getMarkerTooltip i, tidmarker
for tidmarker, i in tidmarkers
tooltip_txt = @getMarkerTooltip tidmarker
icon_is_first = i is 0
icon_is_last = i+1 is tidmarkers.length
icon = @getMarkerIcon tid, icon_is_first, icon_is_last
marker = L.marker [tidmarker.latitude, tidmarker.longitude], {icon: icon}
.bindPopup tooltip_txt
.addTo map
@markers_drawn[mapid].push marker
removeMarkersFrom: (map) ->
console.log 'MarkerMgr::removeMarkersFrom(%o)', map
mapid = map.getContainer().id
if @markers_drawn[mapid]?
while marker = @markers_drawn[mapid].shift()
marker.remove()
addLinesTo: (map) ->
console.log 'MarkerMgr::addLinesTo(%o)', map
mapid = map.getContainer().id
if not @lines_drawn[mapid]?
@lines_drawn[mapid] = []
for tid, tidmarkers of @markers
# TODO: Implement some way of filtering by tid
line_track = []
for tidmarker, i in tidmarkers
line_track.push [tidmarker.latitude, tidmarker.longitude, i]
line = L.hotline line_track,
min: 0
max: tidmarkers.length
palette:
0.0: 'green'
0.5: 'yellow'
1.0: 'red'
weight: 4
outlineColor: '#000000'
outlineWidth: 0.5
.addTo map
@lines_drawn[mapid].push line
removeLinesFrom: (map) ->
console.log 'MarkerMgr::removeLinesFrom(%o)', map
mapid = map.getContainer().id
if @lines_drawn[mapid]?
while line = @lines_drawn[mapid].shift()
line.remove()

View File

@ -11,18 +11,6 @@ class window.OwnMap
show_markers = Cookies.get 'show_markers'
console.log 'initMap: show_markers = %o', show_markers
@marker_start_icons = {}
@marker_finish_icons = {}
@marker_icons = {}
colours = ['blue', 'red', 'orange', 'green', 'purple', 'cadetblue', 'darkred', 'darkgreen', 'darkpurple']
for fg, i in colours
bg1 = if fg is 'green' then 'darkgreen' else 'green'
bg2 = if fg is 'red' then 'darkred' else 'red'
@marker_start_icons[i] = L.AwesomeMarkers.icon({icon: 'play', prefix: 'fa', markerColor: fg, iconColor: bg1 })
@marker_finish_icons[i] = L.AwesomeMarkers.icon({icon: 'stop', prefix: 'fa', markerColor: fg, iconColor: bg2 })
@marker_icons[i] = L.AwesomeMarkers.icon({icon: 'user', prefix: 'fa', markerColor: fg })
# set checkbox
if show_markers is '1'
# hideMarkers();
@ -78,139 +66,25 @@ class window.OwnMap
console.log 'OwnMap::fetchMarkers()'
@markermgr.fetchMarkers window.dateFrom, window.dateTo, window.accuracy
.done (data) =>
console.log '### data=%o', data
jsonMarkers = data
window.updateTrackerIDs()
if @drawMap jsonMarkers
if @drawMap()
$('#mapid').css 'filter', 'blur(0px)'
eraseMap: ->
console.log 'OwnMap::eraseMap()'
for own _tid, markers of @my_markers
if _tid of @polylines
@polylines[_tid].removeFrom @mymap
for own _index2, _marker of markers
_marker.remove()
return true
drawMap: ->
console.log 'OwnMap::drawMap()'
if @map_drawn
@markermgr.removeMarkersFrom @mymap
@markermgr.removeLinesFrom @mymap
@map_drawn = false
@markermgr.addMarkersTo @mymap
@markermgr.addLinesTo @mymap
# TODO: Handle polyline
# TODO: Zoom to bounds
# LEGACY CODE:
tid_markers = @markermgr.getMarkers()
trackerIDs = @markermgr.getTrackerIds()
try
console.log 'drawMap: tid_markers = %o', tid_markers
# vars for map bounding
max_lat = -1000
min_lat = 1000
max_lon = -1000
min_lon = 1000
if @map_drawn
@eraseMap()
nb_markers = 0 # global markers counter
@my_markers = {}
my_latlngs = []
@polylines = []
if trackerIDs.length is 0
console.error 'drawMap: No location data found for any trackerID!'
alert 'No location data found for any trackerID!'
for tid, j in trackerIDs
console.log 'Handling trackers: %o, %o', tid, j
my_latlngs[tid] = []
@my_markers[tid] = []
if window.trackerID is 'all' or window.trackerID is tid
markers = tid_markers[tid]
console.log 'Markers set is: %o', markers
if markers.length is 0
console.error 'drawMap: No location data for trackerID "%o" found!', window.trackerID
alert "No location data for trackerID '#{window.trackerID}' found!"
for marker, i in markers
nb_markers += 1
# prepare popup HTML code for marker
popupString = @markermgr.getMarkerTooltip i, marker
# create leaflet marker object with custom icon based on tid index in array
if i == 0
# first marker
my_marker = L.marker( [markers[i].latitude, markers[i].longitude], {icon: @marker_start_icons[j]} ).bindPopup(popupString)
else if i == markers.length-1
# last marker
my_marker = L.marker( [markers[i].latitude, markers[i].longitude], {icon: @marker_finish_icons[j]} ).bindPopup(popupString)
else
# all other markers
my_marker = L.marker( [markers[i].latitude, markers[i].longitude], {icon: @marker_icons[j]} ).bindPopup(popupString)
if max_lat < markers[i].latitude then max_lat = markers[i].latitude
if min_lat > markers[i].latitude then min_lat = markers[i].latitude
if max_lon < markers[i].longitude then max_lon = markers[i].longitude
if min_lon > markers[i].longitude then min_lon = markers[i].longitude
# add marker to map only if cookie 'show_markers' says to or if 1st or last marker
if show_markers != '0' or i == 0 or i == markers.length-1
my_marker.addTo @mymap
# collect all markers location to prepare drawing track, per trackerID
my_latlngs[tid][i] = [markers[i].latitude, markers[i].longitude, i]
# todo : onmouseover marker, display accuracy radius
# if(markers[i].acc > 0){
#if(i+1 == markers.length && markers[i].acc > 0){
# var circle = L.circle(my_latlngs[i], {
# opacity: 0.2,
# radius: markers[i].acc
# }).addTo(mymap);
#}
# array of all markers for display / hide markers + initial auto zoom scale
my_marker.epoch = markers[i].epoch # needed for geocoding/deleting
@my_markers[tid][i] = my_marker
# var polylines[tid] = L.polyline(my_latlngs[tid]).addTo(mymap);
@polylines[tid] = L.hotline(my_latlngs[tid],
min: 0
max: markers.length
palette:
0.0: 'green'
0.5: 'yellow'
1.0: 'red'
weight: 4
outlineColor: '#000000'
outlineWidth: 0.5
).addTo @mymap
# save default zoom scale
@setDefaultZoom()
# auto zoom scale based on all markers location
@mymap.fitBounds [
[min_lat, min_lon],
[max_lat, max_lon]
]
# set map drawn flag
@map_drawn = true
return true
catch err
console.error 'drawMap: %o', err
alert err.message
@map_drawn = false
return false
# save default zoom scale
@setDefaultZoom()
# auto zoom scale based on all markers location
@mymap.fitBounds @markermgr.getMarkerBounds()
@map_drawn = true
setDefaultZoom: ->
console.log 'OwnMap::setDefaultZoom()'
@ -256,13 +130,13 @@ class window.OwnMap
clearTimeout @live_view_timer
return @live_view
geodecodeMarker: (tid, i) ->
console.log 'OwnMap::geodecodeMarker(%o, %o)', tid, i
geodecodeMarker: (lid) ->
console.log 'OwnMap::geodecodeMarker(%o)', lid
# ajax call to remove marker from backend
$.ajax
url: 'rpc.php'
data:
'epoch': @my_markers[tid][i].epoch
'lid': lid
'action': 'geoDecode'
type: 'get'
dataType: 'json'
@ -271,20 +145,20 @@ class window.OwnMap
console.log 'geodecodeMarker: Status=%o, Data=%o', status, data
# update marker data
$("#loc_#{tid}_#{i}").html "<a href='javascript:showBoundingBox(#{tid}, #{i});' title='Show location bounding box'>#{data.location}</a>"
$("#loc_#{lid}").html "<a href='javascript:showBoundingBox(#{lid});' title='Show location bounding box'>#{data.location}</a>"
else
console.error 'geodecodeMarker: Status=%o, Data=%o', status, data
error: (xhr, desc, err) ->
console.error 'geodecodeMarker: XHR=%o, Error=%o, Details=%o', xhr, err, desc
deleteMarker: (tid, i) ->
console.log 'OwnMap::deleteMarker(%o, %o)', tid, i
deleteMarker: (lid) ->
console.log 'OwnMap::deleteMarker(%o)', lid
# ajax call to remove marker from backend
$.ajax
url: 'rpc.php'
data:
'epoch': @my_markers[tid][i].epoch
'lid': lid
'action': 'deleteMarker'
type: 'get'
dataType: 'json'

View File

@ -73,24 +73,24 @@ class AbstractDatabase
return $markers;
}
public function getMarkerLatLon(int $epoch)
public function getMarkerLatLon(int $location_id)
{
$sql = 'SELECT latitude, longitude FROM ' . $this->prefix . 'locations WHERE epoch = ?';
$result = $this->query($sql, array($epoch));
$sql = 'SELECT latitude, longitude FROM ' . $this->prefix . 'locations WHERE lid = ?';
$result = $this->query($sql, array($location_id));
return $result[0];
}
public function deleteMarker(int $epoch)
public function deleteMarker(int $location_id)
{
$sql = 'DELETE FROM ' . $this->prefix . 'locations WHERE epoch = ?';
$result = $this->execute($sql, array($epoch));
$sql = 'DELETE FROM ' . $this->prefix . 'locations WHERE lid = ?';
$result = $this->execute($sql, array($location_id));
return $result;
}
public function updateLocationData(int $epoch, float $latitude, float $longitude, string $location_name, int $place_id, int $osm_id)
public function updateLocationData(int $location_id, float $latitude, float $longitude, string $location_name, int $place_id, int $osm_id)
{
$sql = 'UPDATE ' . $this->prefix . 'locations SET display_name = ?, place_id = ?, osm_id = ? WHERE epoch = ?';
$params = array($location_name, $place_id, $osm_id, $epoch);
$sql = 'UPDATE ' . $this->prefix . 'locations SET display_name = ?, place_id = ?, osm_id = ? WHERE lid = ?';
$params = array($location_name, $place_id, $osm_id, $location_id);
$result = $this->execute($sql, $params);
return $result;
}

View File

@ -70,14 +70,14 @@ class Rpc
public function deleteMarker()
{
if (!array_key_exists('epoch', $_REQUEST)) {
if (!array_key_exists('lid', $_REQUEST)) {
http_response_code(204);
return array(
'status' => false,
'error' => 'No epoch provided for marker removal',
'error' => 'No location_id provided for marker removal',
);
}
$result = $this->sql->deleteMarker($_REQUEST['epoch']);
$result = $this->sql->deleteMarker($_REQUEST['lid']);
if ($result === false) {
http_response_code(500);
return array(
@ -95,15 +95,15 @@ class Rpc
{
global $_config;
if (!array_key_exists('epoch', $_REQUEST)) {
if (!array_key_exists('lid', $_REQUEST)) {
http_response_code(204);
return array(
'status' => false,
'error' => 'No epoch provided for marker removal',
'error' => 'No location_id provided for marker removal',
);
}
// GET MARKER'S LAT & LONG DATA
$marker = $this->sql->getMarkerLatLon($_REQUEST['epoch']);
$marker = $this->sql->getMarkerLatLon($_REQUEST['lid']);
if ($marker === false) {
http_response_code(500);
@ -129,8 +129,8 @@ class Rpc
$location = @json_encode($geo_decode);
}
//UPDATE MARKER WITH GEODECODED LOCATION
$result = $this->sql->updateLocationData((int)$_REQUEST['epoch'], (float)$latitude, (float)$longitude, $location, $place_id, $osm_id);
// UPDATE MARKER WITH GEODECODED LOCATION
$result = $this->sql->updateLocationData((int)$_REQUEST['lid'], (float)$latitude, (float)$longitude, $location, $place_id, $osm_id);
if ($result === false) {
http_response_code(500);