Skip to content

Instantly share code, notes, and snippets.

@wrobstory
Last active August 29, 2022 07:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrobstory/5609762 to your computer and use it in GitHub Desktop.
Save wrobstory/5609762 to your computer and use it in GitHub Desktop.
Folium Click-for-marker

A Leaflet.js map created with Folium- click on the map to add markers, double-click to remove them. This map was generated with the following Python code:

map_4 = folium.Map(location=[46.8527, -121.7649], tiles='Stamen Terrain',
                   zoom_start=13)
map_4.simple_marker(location=[46.8354, -121.7325], popup='Camp Muir')
map_4.click_for_marker(popup='Waypoint')
map_4.create_map(path='mtrainier.html')
import folium
map_4 = folium.Map(location=[46.8527, -121.7649], tiles='Stamen Terrain',
zoom_start=13)
map_4.simple_marker(location=[46.8354, -121.7325], popup='Camp Muir')
map_4.click_for_marker(popup='Waypoint')
map_4.create_map(path='mtrainier.html')
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.5/leaflet.js"></script>
<style>
#map {
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
}
</style>
</head>
<body>
<div id="map" style="width: 960px; height: 500px"></div>
<script>
var map = L.map('map').setView([46.8527, -121.7649], 13);
L.tileLayer('http://{s}.tile.stamen.com/terrain/{z}/{x}/{y}.jpg', {
maxZoom: 18,
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.'
}).addTo(map);
var marker_1 = L.marker([46.8354, -121.7325]);
marker_1.bindPopup("Camp Muir");
map.addLayer(marker_1)
function newMarker(e){
var new_mark = L.marker().setLatLng(e.latlng).addTo(map);
new_mark.dragging.enable();
new_mark.on('dblclick', function(e){map.removeLayer(e.target)})
var lat = e.latlng.lat.toFixed(4),
lng = e.latlng.lng.toFixed(4);
new_mark.bindPopup("Waypoint");
};
map.on('click', newMarker)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment