Skip to content

Commit bb1ac19

Browse files
authored
Merge pull request espruino#3699 from thinkpoop/master
[mylocation] fix missing lon; add locate & find-marker buttons
2 parents 0662aa0 + 8cb20f7 commit bb1ac19

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

apps/mylocation/interface.html

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<head>
33
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
44
<link rel="stylesheet" href="../../css/spectre.min.css">
5+
<link rel="stylesheet" href="../../css/spectre-icons.min.css">
56
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/geosearch.css"/>
67
</head>
78
<style>
@@ -34,7 +35,9 @@
3435
</div>
3536
<div id="controls">
3637
<span id="select-hint">Click the map to select a location</span>
37-
<button id="select" class="btn btn-primary" style="display:none">Save</button><br/>
38+
<button id="locate-me" class="btn" title="Locate me">&#x26ef;</button>
39+
<button id="locate-marker" class="btn" style="display:none" title="Locate marker"><i class="icon icon-location"></i></button>
40+
<button id="select" class="btn btn-primary" style="display:none" title="Save to device">Save</button><br/>
3841
</div>
3942

4043
<script src="../../core/lib/interface.js"></script>
@@ -76,18 +79,36 @@
7679
map.removeLayer(marker);
7780
}
7881
marker = new L.marker(latlon).addTo(map);
82+
7983
document.getElementById("select-hint").style.display="none";
8084
document.getElementById("select").style.display="";
85+
document.getElementById("locate-marker").style.display="";
8186
}
8287

8388
map.on('click', function(e){
8489
setPosition(e.latlng);
8590
});
8691

92+
function convertMapToFile(map) {
93+
return {lat: map.lat, lon: map.lng};
94+
}
95+
96+
function convertFileToMap(file) {
97+
return {lat: file.lat, lng: file.lon};
98+
}
99+
100+
document.getElementById("locate-me").addEventListener("click", function() {
101+
map.locate({setView: true, maxZoom: 16, enableHighAccuracy:true});
102+
});
103+
104+
document.getElementById("locate-marker").addEventListener("click", function() {
105+
if (latlon && latlon.lng != null && latlon.lat != null) {
106+
map.setView(latlon);
107+
}
108+
});
109+
87110
document.getElementById("select").addEventListener("click", function() {
88-
let settings = {}; // {"lat":48.8566,"lon":2.3522,"location":"Paris"}
89-
settings.lat = latlon.lat;
90-
settings.lon = latlon.lng;
111+
let settings = convertMapToFile(latlon); // {"lat":48.8566,"lon":2.3522,"location":"Paris"}
91112
settings.location = "custom";
92113
Util.showModal("Saving...");
93114
Util.writeStorage("mylocation.json", JSON.stringify(settings), ()=>{
@@ -101,7 +122,7 @@
101122
Util.readStorageJSON("mylocation.json", function(data) {
102123
if (data===undefined) return; // no file
103124
try {
104-
setPosition(data);
125+
setPosition(convertFileToMap(data));
105126
} catch (e) {
106127
console.error(e);
107128
}

0 commit comments

Comments
 (0)