Created
May 26, 2015 01:48
-
-
Save BaconSoap/daa8d7216499e60b3488 to your computer and use it in GitHub Desktop.
blog-leaflet-mapping: iteration 3
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
(function(){ | |
var map; | |
var nextMarkerNumber = 1; | |
var catImg = '<img src="http://thecatapi.com/api/images/get?format=src&type=png&size=small">'; | |
function init() { | |
// create a new map with no base layer | |
map = new L.Map("map", { | |
center: new L.LatLng(42.3964631, -71.1205171), | |
zoom: 16 | |
}); | |
// use Stamen's 'terrain' base layer | |
var layer = new L.StamenTileLayer("terrain"); | |
map.addLayer(layer); | |
// add a marker at the center of the map | |
addMarker([42.3964631, -71.1205171]); | |
// add a marker when the map is clicked | |
map.on('singleclick', onClick); | |
} | |
/** | |
* add a marker with a popup at the specified coordinates | |
*/ | |
function addMarker(coords) { | |
var marker = L.marker(coords); | |
marker.bindPopup('<h3>I\'m #' + nextMarkerNumber++ + '</h3>' + catImg); | |
marker.addTo(map); | |
} | |
function onClick(e) { | |
addMarker(e.latlng); | |
} | |
document.addEventListener('DOMContentLoaded', init); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment