an open-source JavaScript library for mobile-friendly interactive maps
# urls.py
urlpatterns = [
path("", views.render_map, name="index"),
]
# views.py
def render_map(request):
message = "Hello World"
return render(request, "index.html", {"message": message})
Import Leaflet to template file
<!DOCTYPE html>
<html lang="en">
<head>
<title>Leaflet Map</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
type="text/css"
href="https://unpkg.com/leaflet/dist/leaflet.css"
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet/dist/leaflet.js"
crossorigin=""
></script>
</head>
<body>
{{message}}
</body>
</html>
Create leaflet script and stylesheet
static/leaflet-map.js
const copy =
"© OpenStreetMap";
const url = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
const layer = L.tileLayer(url, { attribution: copy });
const map = L.map("map", { layers: [layer] });
map.fitBounds([[40.470060621973026, -86.99269856365936]]);
static/leaflet-map.css
html, body {
height: 100%;
margin: 0; }
#map {
height: 100%;
width: 100%; }
Adding map into template file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Leaflet Map</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
type="text/css"
href="https://unpkg.com/leaflet/dist/leaflet.css"
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet/dist/leaflet.js"
crossorigin=""
></script>
<link rel="stylesheet" type="text/css" href="{% static 'leaflet-map.css' %}" />
<script src="{% static 'leaflet-map.js' %}" defer></script>
</head>
<body>
<div id="map"></div>
</body>
</html>
Adding geometry to the map
static/leaflet-map.js
const copy =
"© OpenStreetMap";
const url = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
const layer = L.tileLayer(url, { attribution: copy });
const map = L.map("map", { layers: [layer] });
map.fitBounds([[40.470060621973026, -86.99269856365936]]);
let marker = L.marker([40.470060621973026, -86.99269856365936])
.addTo(map)
.bindPopup("This is a popup");
Adding markers from Django side
views.py
import geojson
import shapely.geometry as geo
from django.shortcuts import render
# Create your views here.
def render_map(request):
point = geo.Point(([-86.99269856365936, 40.470060621973026]))
marker = geojson.Feature(geometry=point, properties={"message": "Hello World"})
data = geojson.FeatureCollection(marker)
return render(request, "index.html", {"data": data})
Passing data to the template file
index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Leaflet Map</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
type="text/css"
href="https://unpkg.com/leaflet/dist/leaflet.css"
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet/dist/leaflet.js"
crossorigin=""
></script>
<link rel="stylesheet" type="text/css" href="{% static 'leaflet-map.css' %}" />
<script src="{% static 'leaflet-map.js' %}" defer></script>
</head>
<body>
{{ data|json_script:"data_geojson" }}
<div id="map"></div>
</body>
</html>
Update JavaScript file
static/leaflet-map.js
const copy =
"© OpenStreetMap";
const url = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
const layer = L.tileLayer(url, { attribution: copy });
const map = L.map("map", { layers: [layer] });
const data = JSON.parse(document.getElementById("data_geojson").textContent);
let feature = L.geoJSON(data.features)
.bindPopup(function (layer) {
return layer.feature.properties.message;
})
.addTo(map);
map.fitBounds(feature.getBounds());
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5]
},
"properties": {
"prop0": "value0"
}
}, {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0]
]
]
},
"properties": {
"prop0": "value0",
"prop1": {
"this": "that"
}
}
}]
}
This course is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license. This is a human-readable summary of (and not a substitute for) the license. Official translations of this license are available in other languages.
You are free to:
Under the following terms: