Django Map Widgets is a package that provides highly configurable, pluggable, and user-friendly map widgets for GeoDjango form fields. It simplifies the integration of interactive maps into GeoDjango applications, enhancing the overall development experience.
The primary goal of Django Map Widgets is to bridge the gap between powerful GeoDjango functionality and user-friendly map interactions, creating a more accessible and enjoyable experience for both developers and end-users of GeoDjango-powered applications. Currently, the package supports Google, Mapbox, and Leaflet mapping platforms.
![mapbox_interactive_preview](https://private-user-images.githubusercontent.com/1518272/342123152-f2df8654-b29a-4d64-9159-87a3790ede0b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQyNzY3MTAsIm5iZiI6MTcyNDI3NjQxMCwicGF0aCI6Ii8xNTE4MjcyLzM0MjEyMzE1Mi1mMmRmODY1NC1iMjlhLTRkNjQtOTE1OS04N2EzNzkwZWRlMGIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyMSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjFUMjE0MDEwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NDdkMzZhNzI0MTY5ODk4MzQ1NTIxZjZjOTU3YjhlNjU4MDM1MGU5ZmQ2NzU3YTkyMTIyNmM1MDVkN2IwNTkxNCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.X27q_uLOmqE9bDoR7NO6KonWisMX8TBT3mJ0yRHj2UA)
pip install django-map-widgets
Add ‘mapwidgets’ to your INSTALLED_APPS
in settings.py
INSTALLED_APPS = [
...
'django.contrib.sessions',
'django.contrib.staticfiles',
'mapwidgets',
]
Ensure collectstatic
Django admin command is run before using the widgets in production.
python manage.py collectstatic
Django Admin Usage
from django.contrib.gis.db import models
import mapwidgets
class CityAdmin(admin.ModelAdmin):
formfield_overrides = {
models.PointField: {"widget": mapwidgets.GoogleMapPointFieldWidget}
}
Django Forms Usage
from mapwidgets.widgets import GoogleMapPointFieldWidget, MapboxPointFieldWidget
class CityForm(forms.ModelForm):
class Meta:
model = City
fields = ("coordinates", "city_hall")
widgets = {
'coordinates': GoogleMapPointFieldWidget,
'city_hall': MapboxPointFieldWidget,
}
When the map widgets are used in Django web views with forms, Remember to include {{ form.media }}
template tag in the
view templates.
Settings
The JavaScript map rendering behavior of the widgets can be customized by providing MAP_WIDGETS
config in the
project's settings file. For detailed guidance on map customization options, check
the settings guide.
GOOGLE_MAP_API_KEY = os.getenv("GOOGLE_MAP_API_KEY")
MAPBOX_ACCESS_TOKEN = os.getenv("MAPBOX_ACCESS_TOKEN")
MAP_WIDGETS = {
"GoogleMap": {
"apiKey": GOOGLE_MAP_API_KEY,
"PointField": {
"interactive": {
"mapOptions": {
"zoom": 15, # set initial zoom
"streetViewControl": False,
},
"GooglePlaceAutocompleteOptions": {
"componentRestrictions": {"country": "uk"}
},
}
}
},
"Mapbox": {
"accessToken": MAPBOX_ACCESS_TOKEN,
"PointField": {
"interactive": {
"mapOptions": {"zoom": 12, "center": (51.515618, -0.091998)},
"markerFitZoom": 14,
}
},
},
"Leaflet": {
"PointField": {
"interactive": {
"mapOptions": {
"zoom": 12,
"scrollWheelZoom": False
}
}
},
"markerFitZoom": 14,
}
}
jQuery is required for Django Map Widgets to function in regular Django views. However, if the widgets is being used within the Django Admin, jQuery does not need to be provided separately. Any map widget class can be configured as described in the documentation, and they will work out of the box.
Preferable jQuery version is 3.7-slim
.
Django Map Widgets offers two types of widgets:
-
Interactive (Dynamic) Widgets: These widgets allow users to interact with the map, such as clicking to set a location or dragging a marker. They are ideal for data input and editing scenarios.
-
Static (Read-only) Widgets: These widgets display map data in a non-interactive format. They are useful for presenting location information without allowing modifications.
GeoDjango Field | GoogleMap | Mapbox | Leaflet | |||
---|---|---|---|---|---|---|
Interactive | Static | Interactive | Static | Interactive | Static | |
PointField | ✅ | ✅ | ✅ | ✅ | ✅ | N/A |
LineStringField | ✖️ | ✖️ | ✖️ | ✖️ | ✖️ | N/A |
PolygonField | ✖️ | ✖️ | ✖️ | ✖️ | ✖️ | N/A |
MultiPointField | ✖️ | ✖️ | ✖️ | ✖️ | ✖️ | N/A |
MultiLineStringField | ✖️ | ✖️ | ✖️ | ✖️ | ✖️ | N/A |
MultiPolygonField | ✖️ | ✖️ | ✖️ | ✖️ | ✖️ | N/A |
Currently, the package supports Google, Mapbox, and Leaflet mapping platforms. If you have ideas for additional map providers or new features, or even if you want to help extend support to other GeoDjango form fields, feel free to do so. We would be happy to review and merge your contributions.
For more info how to contribute, please check out the contribution guidelines.
and more...