Click on the map to add points. See the console for lat/long output.
Last active
May 10, 2018 20:12
-
-
Save hepplerj/2f405df580f66923091ad604ccf2fffc to your computer and use it in GitHub Desktop.
Add leaflet points on click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Adding or moving markers on a Leaflet map</title> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" /> | |
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script> | |
<style type="text/css"> | |
html, body, #map{ | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script type="text/javascript"> | |
var options = { | |
center: [37.7749, -122.4194], | |
zoom: 10 | |
} | |
var map = L.map('map', options); | |
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: 'OSM'}) | |
.addTo(map); | |
map.on('click', | |
function(e){ | |
var coord = e.latlng.toString().split(','); | |
var lat = coord[0].split('('); | |
var lng = coord[1].split(')'); | |
console.log("You clicked the map at LAT: " + lat[1] + " and LONG: " + lng[0]); | |
L.marker(e.latlng).addTo(map); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment