Skip to content

Commit 8a25ebd

Browse files
committed
add generate behaviour
1 parent d014541 commit 8a25ebd

File tree

2 files changed

+83
-42
lines changed

2 files changed

+83
-42
lines changed

app.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
var map = L.map('map').setView([-0.2200455, -78.5009917], 10),
22
wayPoints = [
3-
L.latLng(-0.128265, -78.362528),
4-
L.latLng(-0.9322002, -78.6225983),
5-
L.latLng(-0.95585, -78.850288),
6-
L.latLng(-0.8700321, -78.8993113),
7-
L.latLng(-1.6746425, -78.6502389),
8-
L.latLng(-1.4693018, -78.8169396),
9-
L.latLng(-2.0556571, -78.76386),
10-
L.latLng(-2.201431, -78.846513),
11-
L.latLng(-2.2013104, -78.8477451),
3+
// L.latLng(-0.128265, -78.362528),
4+
// L.latLng(-0.9322002, -78.6225983),
5+
// L.latLng(-0.95585, -78.850288),
6+
// L.latLng(-0.8700321, -78.8993113),
7+
// L.latLng(-1.6746425, -78.6502389),
8+
// L.latLng(-1.4693018, -78.8169396),
9+
// L.latLng(-2.0556571, -78.76386),
10+
// L.latLng(-2.201431, -78.846513),
11+
// L.latLng(-2.2013104, -78.8477451),
1212
],
1313
myStyle = {
1414
color: '#ff7800',
@@ -18,40 +18,52 @@ var map = L.map('map').setView([-0.2200455, -78.5009917], 10),
1818

1919
L.tileLayer.provider('CartoDB.Voyager').addTo(map);
2020

21-
let routing = L.Routing.control({
22-
waypoints: wayPoints,
23-
autoRoute: true,
24-
routeWhileDragging: true,
25-
}).addTo(map);
26-
routing.hide();
27-
28-
routing.on('routeselected', function (e) {
29-
let r = e.route,
30-
line = L.Routing.line(r),
31-
bounds = line.getBounds();
32-
33-
var result = {
34-
type: 'FeatureCollection',
35-
features: [
36-
{
37-
type: 'Feature',
38-
properties: {},
39-
geometry: {
40-
type: 'LineString',
41-
coordinates: [],
21+
function generateResult() {
22+
let tempArray = JSON.parse(document.getElementById('coordinates').value);
23+
24+
tempArray.map(function (item) {
25+
wayPoints.push(L.latLng(item.lat, item.lng));
26+
});
27+
28+
let routing = L.Routing.control({
29+
waypoints: wayPoints,
30+
autoRoute: true,
31+
routeWhileDragging: true,
32+
}).addTo(map);
33+
routing.hide();
34+
35+
routing.on('routeselected', function (e) {
36+
let r = e.route,
37+
line = L.Routing.line(r),
38+
bounds = line.getBounds();
39+
40+
var result = {
41+
type: 'FeatureCollection',
42+
features: [
43+
{
44+
type: 'Feature',
45+
properties: {},
46+
geometry: {
47+
type: 'LineString',
48+
coordinates: [],
49+
},
4250
},
43-
},
44-
],
45-
};
51+
],
52+
};
53+
54+
map.fitBounds(bounds);
4655

47-
map.fitBounds(bounds);
56+
r.coordinates.map(function (item) {
57+
result.features[0].geometry.coordinates.push([item.lng, item.lat]);
58+
});
4859

49-
r.coordinates.map(function (item) {
50-
result.features[0].geometry.coordinates.push([item.lng, item.lat]);
60+
document.getElementById('result').value = JSON.stringify(result);
5161
});
62+
}
5263

53-
document.getElementById('result').value = JSON.stringify(result);
54-
});
64+
function cleanTextarea() {
65+
document.getElementById('coordinates').value = '';
66+
}
5567

5668
function SelectAll(id) {
5769
document.getElementById(id).focus();

index.html

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Generate GeoJSON from cordinates</title>
7+
<title>Generate GeoJSON from coordinates</title>
88
<link
99
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
1010
rel="stylesheet"
@@ -40,7 +40,7 @@
4040
<div class="container-fluid">
4141
<header class="row justify-content-between bg-primary text-white py-2">
4242
<div class="col">
43-
<h1 class="font-source-sans">Generate GeoJSON from cordinates</h1>
43+
<h1 class="font-source-sans">Generate GeoJSON from coordinates</h1>
4444
</div>
4545
</header>
4646
<div class="container">
@@ -50,8 +50,37 @@ <h1 class="font-source-sans">Generate GeoJSON from cordinates</h1>
5050
This tool uses Leaflet and ES6 to generate a route via OSMR using
5151
the coordinates entered below.
5252
</p>
53-
<textarea name="" id="" rows="10" class="form-control"></textarea>
54-
<button class="btn btn-lg btn-success mt-2">Generate</button>
53+
<textarea
54+
name=""
55+
id="coordinates"
56+
rows="10"
57+
class="form-control"
58+
placeholder="Add waypoints in this format [{lat: x, lng: y}, {lat: x, lng: y} ...]"
59+
>
60+
[{"lat": -0.128265, "lng": -78.362528},
61+
{"lat": -0.9322002, "lng": -78.6225983},
62+
{"lat": -0.95585, "lng": -78.850288},
63+
{"lat": -0.8700321, "lng": -78.8993113},
64+
{"lat": -1.6746425, "lng": -78.6502389},
65+
{"lat": -1.4693018, "lng": -78.8169396},
66+
{"lat": -2.0556571, "lng": -78.76386},
67+
{"lat": -2.201431, "lng": -78.846513},
68+
{"lat": -2.2013104, "lng": -78.8477451}]
69+
</textarea>
70+
<div class="w-100 d-flex">
71+
<button
72+
class="btn btn-lg btn-success mt-2 me-2"
73+
onClick="generateResult()"
74+
>
75+
Generate
76+
</button>
77+
<button
78+
class="btn btn-lg btn-secondary mt-2"
79+
onClick="cleanTextarea()"
80+
>
81+
Clean
82+
</button>
83+
</div>
5584
</div>
5685
<div class="col">
5786
<h5 class="font-source-sans">Result:</h5>

0 commit comments

Comments
 (0)