Skip to content

Commit

Permalink
follow gpx
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Jul 18, 2023
1 parent 3afe79d commit 5473aae
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 90 deletions.
23 changes: 15 additions & 8 deletions application/assets/js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ const module = (() => {

if (gpx_selection_count > gpx_selection.length - 1) gpx_selection_count = 0;
map.fitBounds(gpx_selection[gpx_selection_count].getBounds());
let m = gpx_selection[gpx_selection_count].getLayers();
const keys = Object.keys(m[0]._layers);
const firstKey = keys[0];
general.gpx_selection_latlng = m[0]._layers[firstKey]._latlngs;

//store info in object
gpx_selection_info.duration =
Expand All @@ -403,6 +407,7 @@ const module = (() => {
document.getElementById("gpx-time").innerText = format_ms(
gpx_selection_info.duration
);

document.querySelector("#gpx-evo-up span").innerText =
gpx_selection_info.elevation_gain.toFixed(2);

Expand All @@ -419,14 +424,13 @@ const module = (() => {
let get_closest_point = function (route) {
if (!mainmarker.device_lat) return false;

//let r = route.map((row) => row.reverse());
let m = L.polyline(route);

let latlng = [mainmarker.device_lat, mainmarker.device_lng];

let k = L.GeometryUtil.closest(map, m, latlng, true);

L.marker(k).addTo(map);
// L.marker(k).addTo(map);

let f = calc_distance(
mainmarker.device_lat,
Expand All @@ -442,6 +446,7 @@ const module = (() => {
if (mainmarker.accuracy < 22) {
// Add the current value of f to the closest_average array
closest_average.push(f);
console.log(closest_average);
}

// Calculate the average of the closest_average array if it has more than 48 elements
Expand All @@ -457,16 +462,17 @@ const module = (() => {
}

// If the routing_notification setting is off, exit early
if (!setting.routing_notification) {
if (setting.routing_notification == false) {
return false;
}

// If the average is above 0.5, trigger a vibration and show a toaster message
if (result > 0.5) {
helper.side_toaster("Too far " + result, 3000);

try {
navigator.vibrate([1000, 500, 1000]);
} catch (e) {}
helper.toaster("Too far " + result, 3000);
}
}
};
Expand Down Expand Up @@ -765,8 +771,6 @@ const module = (() => {
timestamp: ts.toISOString(),
});

//console.log(tracking_cache);

// Update the view with tracking data

if (tracking_cache.length > 2) {
Expand All @@ -780,6 +784,7 @@ const module = (() => {
//get tracking data to display in view
new L.GPX(toGPX(), { async: true }).on("loaded", function (e) {
//meter

if (general.measurement_unit == "km") {
// Calculate the distance along the polyline
let a = calcDistance(polyline_tracking);
Expand Down Expand Up @@ -824,12 +829,14 @@ const module = (() => {
document.querySelector("#tracking-speed-average-time").innerText =
d;
}

/*
let d = e.target.get_duration_string(
e.target.get_total_time(),
false
);
document.querySelector("#tracking-moving-time span").innerText = d;
*/
document.querySelector("#tracking-moving-time span").innerText =
format_ms(e.target.get_total_time());
});
}

Expand Down
7 changes: 6 additions & 1 deletion application/assets/js/route-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ const rs = ((_) => {
});
};

let routing_profile = ["cycling-road", "foot-hiking", "driving-car"];
let routing_profile = [
"cycling-mountain",
"cycling-road",
"foot-hiking",
"driving-car",
];
let m = routing_profile.indexOf(settings.profile);

let change_type = function () {
Expand Down
24 changes: 12 additions & 12 deletions application/assets/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const settings = ((_) => {
"cache-zoom",
document.getElementById("cache-zoom").value
);
localStorage.setItem(
"export-path",
document.getElementById("export-path").value
);
//add a slash to the path
let f = document.getElementById("export-path").value;
if (f.substr(f.length - 1) != "/") f = f + "/";
localStorage.setItem("export-path", f);

localStorage.setItem("osm-tag", document.getElementById("osm-tag").value);

Expand Down Expand Up @@ -60,7 +60,7 @@ const settings = ((_) => {
setting = {
export_path: !localStorage.getItem("export-path")
? ""
: localStorage.getItem("export-path") + "/",
: localStorage.getItem("export-path"),
osm_tag: localStorage.getItem("osm-tag"),

cache_time: localStorage["cache-time"] || "10",
Expand Down Expand Up @@ -186,7 +186,7 @@ const settings = ((_) => {
};

let load_settings_from_file = function () {
helper.toaster("search setting file", 2000);
helper.side_toaster("search setting file", 2000);
try {
//search gpx
let load_file = new Applait.Finder({
Expand All @@ -197,7 +197,7 @@ const settings = ((_) => {
load_file.search("omap_settings.json");
load_file.on("searchComplete", function (needle, filematchcount) {});
load_file.on("error", function (message, err) {
helper.toaster("file not found", 2000);
helper.side_toaster("file not found!", 2000);
});

load_file.on("fileFound", function (file, fileinfo, storageName) {
Expand All @@ -222,9 +222,9 @@ const settings = ((_) => {
document.getElementById("export-path").value = setting.export_path;
document.getElementById("osm-tag").value = setting.osm_tag;

helper.toaster(
helper.side_toaster(
"the settings were loaded from the file, if you want to use them permanently don't forget to save.",
3000
5000
);
}, 1500);
};
Expand Down Expand Up @@ -273,7 +273,7 @@ const settings = ((_) => {
document.getElementById("osm-tag").value =
setting.osm_tag;

helper.toaster(
helper.side_toaster(
"the settings were loaded from the file, if you want to use them permanently don't forget to save.",
3000
);
Expand Down Expand Up @@ -313,11 +313,11 @@ const settings = ((_) => {
var request = sdcard.addNamed(file, "omap_settings.json");

request.onsuccess = function () {
helper.toaster("settings exported, omap_settings.json", 5000);
helper.side_toaster("settings exported, omap_settings.json", 5000);
};

request.onerror = function () {
helper.toaster("Unable to write the file", 2000);
helper.side_toaster("Unable to write the file", 2000);
};
};
} catch (e) {}
Expand Down
11 changes: 6 additions & 5 deletions application/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -468,21 +468,22 @@ <h2>Export</h2>
<!--GPX-->
<div class="panel menu-box" id="gpx-file-info" name="GPX File-Info">
<section>
<div id="gpx-name"></div>
<div class="item" id="gpx-name"></div>
</section>
<section>
<strong><u>Distance</u></strong>
<div id="gpx-distance"></div>
<div class="item" id="gpx-distance"></div>
</section>
<section>
<strong><u>Evolution</u></strong>
<div id="gpx-evo-up"><span></span></div>
<div id="gpx-evo-down"><span></span></div>
<div class="item" id="gpx-evo-up"><span></span></div>
<div class="item" id="gpx-evo-down"><span></span></div>
</section>
<section>
<strong><u>Time</u></strong>
<div id="gpx-time">duration <span></span></div>
<div class="item" id="gpx-time">duration <span></span></div>
</section>
<button class="item" data-map="follow-path">Follow path</button>
</div>
<!--ROUTING-->
<div class="panel menu-box" id="routing" name="Routing">
Expand Down
52 changes: 35 additions & 17 deletions application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ let routing = {
closest: "",
loaded: false,
auto_routing: false,
auto_update: false,
};

let mainmarker = {
target_marker: "",
selected_marker: "",
gpx_selection_latlng: [],
startup_markers:
localStorage.getItem("startup_markers") != null
? JSON.parse(localStorage.getItem("startup_markers"))
Expand Down Expand Up @@ -102,6 +104,7 @@ let status = {
tracking_paused: false,
keylock: false,
screenOff: false,
follow_path: false,
};

if (!navigator.geolocation) {
Expand Down Expand Up @@ -316,7 +319,7 @@ document.addEventListener("DOMContentLoaded", function () {
load_ads();
let manifest = function (a) {
document.getElementById("intro-footer").innerText =
"O.MAP Version " + a.version;
"Version " + a.version;
};
helper.getManifest(manifest);
} else {
Expand All @@ -340,7 +343,6 @@ document.addEventListener("DOMContentLoaded", function () {
);
mainmarker.current_lat = data[0];
mainmarker.current_lng = data[1];

mainmarker.device_lat = data[0];
mainmarker.device_lng = data[1];
myMarker.setLatLng([mainmarker.device_lat, mainmarker.device_lng]).update();
Expand Down Expand Up @@ -1235,7 +1237,7 @@ document.addEventListener("DOMContentLoaded", function () {

localStorage.setItem("last_location", b);

if (routing.active) {
if (routing.active && routing.auto_update) {
routing_auto_update();
}

Expand All @@ -1250,13 +1252,17 @@ document.addEventListener("DOMContentLoaded", function () {
}

//distances
distance_to_target();

if (routing.active) {
console.log("active");
if (!routing.auto_routing)
module.get_closest_point(routing.coordinates);
if (routing.active && routing.auto_routing == false) {
alert("calc");
module.get_closest_point(routing.coordinates);
}

if (status.follow_path == true) {
module.get_closest_point(gpx_selection_latlng);
}

distance_to_target();
}

function errorHandler(err) {
Expand Down Expand Up @@ -1305,12 +1311,7 @@ document.addEventListener("DOMContentLoaded", function () {
}
last_update = dayjs();

if (
!routing.active &&
!mainmarker.positionHasChanged &&
!routing.auto_routing
)
return false;
if (routing.auto_routing == false) return false;

routing.start = mainmarker.device_lng + "," + mainmarker.device_lat;

Expand Down Expand Up @@ -1595,24 +1596,39 @@ document.addEventListener("DOMContentLoaded", function () {
if (item_value == "resetrouting") {
rs.reset_routing();
}
if (item_value == "follow-path") {
if (setting.routing_notification == false) {
helper.side_toaster(
"You have to activate the notification in the settings of the app",
3000
);
return false;
}
status.follow_path = true;
document.querySelector("div#finder").style.display = "none";
status.windowOpen = "map";
helper.side_toaster(
"you will get a warning if you are too far off the path",
3000
);
}

if (item_value === "startrouting") {
// If item_value is "startrouting"
// Call the auto_update_view function
auto_update_view();
status.auto_routing = false;

// Check the value of routing.active and perform the appropriate action
switch (routing.active) {
case true:
console.log(routing.active);
routing.active = false;
helper.side_toaster("Routing paused", 2000);

document.activeElement.innerText = "start";
break;

case false:
console.log(routing.active);
routing.active = true;
helper.side_toaster("Routing started", 2000);
document.activeElement.innerText = "stop";
Expand Down Expand Up @@ -1889,7 +1905,7 @@ document.addEventListener("DOMContentLoaded", function () {
finder_panels = finder_panels.filter((e) => e.id != "weather");
}

if (!gpx_selection_info.name) {
if (gpx_group.length == 0) {
finder_panels = finder_panels.filter((e) => e.id != "gpx-file-info");
}

Expand Down Expand Up @@ -1942,6 +1958,8 @@ document.addEventListener("DOMContentLoaded", function () {

if (finder_panels[count].id == "gpx-file-info") {
bottom_bar("", "", "");
console.log(document.querySelectorAll("#gpx-file-info > .item").length);
document.querySelectorAll("#gpx-file-info > .item")[0].focus();
}
if (finder_panels[count].id == "tracking") {
bottom_bar("", "", "");
Expand Down
2 changes: 1 addition & 1 deletion application/manifest.webapp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.8.0",
"version": "1.9.2",
"version_name": "together we are strong",
"name": "o.map",
"description": "O.map, your ultimate navigation companion for KaiOS-powered devices. O.map is a lightweight and feature-rich map application designed specifically for KaiOS, enabling you to explore and navigate the world with ease. Whether you're a local resident, a tourist, or an adventurer, O.map is here to enhance your journey and keep you on the right track.",
Expand Down
2 changes: 1 addition & 1 deletion application/manifest.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],

"b2g_features": {
"version": "2.0.0",
"version": "2.0.1",
"id": "o.map",
"core": true,
"categories": ["utilities"],
Expand Down
Loading

0 comments on commit 5473aae

Please sign in to comment.