All you need to create a working webpage is about 20 lines of html and JavaScript code:
<html>
<head>
<title>Leaflet Part1 - Minimum Required</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@0.7.3/dist/leaflet.css"/>
<script src="https://unpkg.com/leaflet@0.7.3/dist/leaflet.js"></script>
<style>
#map{ height: 100% }
</style>
</head>
<body>
<div id="map"></div>
<script>
var lfmap = L.map('map').setView([39.82835 , -98.5816684], 5);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(lfmap);
</script>
</body>
</html>
That is all it takes to display an interactive map on your own webpage.
Part 1 – Simple Example links: Preview | GitHub | JSFiddle |
In the “Part 1 – Simple” example the getPolygons() and getMarkers() JavaScript functions are where you return the map objects you wish to draw. Usually these would get their data from a web service or from the middle tier of you web application. In this example, though, I put some sample objects in to show you the format of data they should return.
getMarkers() should return an array of markers that are composed of 2 or more elements: Latitude, Longitude, Title, and data elements to display in the Popup when a marker is clicked. In my example, I showed that you can store HTML here to display in the Popup, but it would be better practice to store the different data elements as multiple elements and then pass them all to the formatPopupText() function that will then transform it into the desired HTML you want displayed.
getPolygons() should also return an array of data composed of the polygon definition optionally followed by the title, Popup display data, and color of the polygon. The polygon definition in Leaflet is itself an array or 3 or more arrays of a latitude value and a longitude value. For example:
[ [32.800807, -117.226163], [32.801618, -117.222419], [32.798083, -117.221303], [32.797325, -117.225219] ]
drawMap() must be called with the latitude and the longitude of the center of map to display as well as the zoom level. It then calls drawLayers() which iterates the getMarkers() and getPolygons() returned arrays, sets defaults, then draws each object using drawMarker() or drawPolygon(), and assigns Popups for when you click on the drawn object.
In the next article, I will show you how you can have the page automatically center and zoom to display the objects you draw on the map so that you will not have to manually set those values.