Skip to content

Instantly share code, notes, and snippets.

@sinky
Created January 5, 2021 07:08
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 sinky/08e1127d62b3def85d49d41b28a4fd75 to your computer and use it in GitHub Desktop.
Save sinky/08e1127d62b3def85d49d41b28a4fd75 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/ronudux
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
#mapid { height: 500px; }
</style>
</head>
<body>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<div id="mapid"></div>
<script id="jsbin-javascript">
var map = L.map('mapid').setView([51.505, 7.09], 4);
var markers = []
var polylines = []
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
function addMarker(e){
var newMarker = new L.marker(e.latlng,{
draggable: true
}).addTo(map);
markers.push(newMarker)
newMarker.bindPopup('test', { //prompt("Beschriftung für den Marker"), {
closeOnClick: false,
autoClose: false
}).openPopup();
newMarker.on('dragend', function(e){
drawLine()
});
newMarker.on('click', function(e){
if(e.originalEvent.ctrlKey) {
map.removeLayer(newMarker)
removeIndex = markers.indexOf(newMarker)
markers.splice(removeIndex, 1);
drawLine()
}
});
drawLine()
}
function drawLine() {
removeLine()
if(markers.length > 1) {
for(i=0; i < markers.length-1; i++) {
var latlngs = Array();
latlngs.push(markers[i].getLatLng());
latlngs.push(markers[i+1].getLatLng());
var polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
polylines.push(polyline)
}
}
}
function removeLine() {
for(i=0; i < polylines.length; i++) {
map.removeLayer(polylines[i])
}
}
map.on('click', function(e){
addMarker(e)
});
</script>
<script id="jsbin-source-css" type="text/css">#mapid { height: 500px; }
</script>
<script id="jsbin-source-javascript" type="text/javascript">var map = L.map('mapid').setView([51.505, 7.09], 4);
var markers = []
var polylines = []
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
function addMarker(e){
var newMarker = new L.marker(e.latlng,{
draggable: true
}).addTo(map);
markers.push(newMarker)
newMarker.bindPopup('test', { //prompt("Beschriftung für den Marker"), {
closeOnClick: false,
autoClose: false
}).openPopup();
newMarker.on('dragend', function(e){
drawLine()
});
newMarker.on('click', function(e){
if(e.originalEvent.ctrlKey) {
map.removeLayer(newMarker)
removeIndex = markers.indexOf(newMarker)
markers.splice(removeIndex, 1);
drawLine()
}
});
drawLine()
}
function drawLine() {
removeLine()
if(markers.length > 1) {
for(i=0; i < markers.length-1; i++) {
var latlngs = Array();
latlngs.push(markers[i].getLatLng());
latlngs.push(markers[i+1].getLatLng());
var polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
polylines.push(polyline)
}
}
}
function removeLine() {
for(i=0; i < polylines.length; i++) {
map.removeLayer(polylines[i])
}
}
map.on('click', function(e){
addMarker(e)
});</script></body>
</html>
#mapid { height: 500px; }
var map = L.map('mapid').setView([51.505, 7.09], 4);
var markers = []
var polylines = []
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
function addMarker(e){
var newMarker = new L.marker(e.latlng,{
draggable: true
}).addTo(map);
markers.push(newMarker)
newMarker.bindPopup('test', { //prompt("Beschriftung für den Marker"), {
closeOnClick: false,
autoClose: false
}).openPopup();
newMarker.on('dragend', function(e){
drawLine()
});
newMarker.on('click', function(e){
if(e.originalEvent.ctrlKey) {
map.removeLayer(newMarker)
removeIndex = markers.indexOf(newMarker)
markers.splice(removeIndex, 1);
drawLine()
}
});
drawLine()
}
function drawLine() {
removeLine()
if(markers.length > 1) {
for(i=0; i < markers.length-1; i++) {
var latlngs = Array();
latlngs.push(markers[i].getLatLng());
latlngs.push(markers[i+1].getLatLng());
var polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
polylines.push(polyline)
}
}
}
function removeLine() {
for(i=0; i < polylines.length; i++) {
map.removeLayer(polylines[i])
}
}
map.on('click', function(e){
addMarker(e)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment