Skip to content

Instantly share code, notes, and snippets.

@jesusnoseq
Last active December 21, 2019 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusnoseq/4dc20692ae6a6afe4caf34249271da6f to your computer and use it in GitHub Desktop.
Save jesusnoseq/4dc20692ae6a6afe4caf34249271da6f to your computer and use it in GitHub Desktop.
Simple line drawing beetwen markers with leaflet
<!DOCTYPE html>
<html>
<head>
<title>Leaflet nodes</title>
<meta charset="utf-8" />
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""
></script>
<script src="https://unpkg.com/leaflet-arc/bin/leaflet-arc.min.js"></script>
<style>
#mapid {
height: 280px;
}
</style>
</head>
<body>
<div id="mapid"></div>
<script>
var token =
"{{YOUR TOKEN GO HERE}}";
var mymap = L.map("mapid").setView([39.405, -3.79], 6);
L.tileLayer(
"https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=" +
token,
{
attribution:
'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: "mapbox/streets-v11",
accessToken: token
}
).addTo(mymap);
var madrid = newSite([40.405, -3.79]);
var cordoba = newSite([38.005, -4.59]);
var albacete = newSite([38.9, -1.7]);
var caceres = newSite([39.5, -6.5]);
var drawing = false;
var firstPoint = null;
var secondPoint = null;
function draw(e) {
if (!drawing) {
console.log("START DRAWING");
firstPoint = this.getLatLng();
drawing = true;
} else {
console.log("END DRAWING");
secondPoint = this.getLatLng();
drawLine(firstPoint, secondPoint);
drawing = false;
}
}
function drawLine(p1, p2) {
L.Polyline.Arc(p1, p2, {
color: "blue",
vertices: 0
})
.addTo(mymap)
.on("click", function() {
mymap.removeLayer(this);
});
}
function newSite(coords) {
return L.marker(coords)
.addTo(mymap)
.on("click", draw);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment