Django Map-Location Field
Project description
Django: Map-Location
Adds a fully-static location field (Leaflet) to manage location-based data without the need for a full-fledged GeoDjango installation. ... when all you need is a visual position chooser.
Install
- Add to your INSTALLED_APPS
INSTALLED_APPS = [
...,
'map_location',
]
- Create Map-Location field
from map_location.fields import LocationField
class Place(models.Model):
location = LocationField('Pos', blank=True, null=True, options={
'map': {
'center': [52.52, 13.40],
'zoom': 12,
},
# 'markerZoom': 18
# 'tileLayer': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
# 'tileOptions': {
# attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
# },
# 'locate': {
# 'returnToPrevBounds': True,
# 'showPopup': False,
# },
})
Options Paramter
Key | Info |
---|---|
map | Map Options (default: {center: [20, -25], zoom: 2} ) |
markerZoom | Initial zoom scale (on load) – if a marker is set. (default: 18 ) |
tileLayer | TileLayer urlTemplate (default: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" ) |
tileOptions | TileLayer Options (default: {} ) |
locate | Leaflet.Locate Options (default: {returnToPrevBounds: true, showPopup: false} ) |
Usage
You can access the location by its parts (place.location.lat
& place.location.long
) or by its string value (str(place.location)
or just place.location
) which will return a comma-separated string (lat,long
). This string format is also used for database storage (with a precision of 6 digits, or up to 11 cm).
Example
If you export your location as json, you can use a fully static map:
_JSON_ = {'loc': [place.location.lat, place.location.long]}
<script src="/static/leaflet/leaflet.js"></script>
<script src="/static/leaflet/locate/L.Control.Locate.min.js"></script>
<link rel="stylesheet" href="/static/leaflet/leaflet.css" />
<link rel="stylesheet" href="/static/leaflet/locate/L.Control.Locate.css" />
...
<div id="map-id"></div>
...
<script>
const osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
const map = L.map('map-id', {
layers: [osm],
center: [52.52, 13.40],
zoom: 14,
});
L.control.locate({
returnToPrevBounds: true,
showPopup: false,
}).addTo(map);
L.marker(L.latLng(_JSON_.loc)).addTo(map);
...
</script>
See Leaflet docs for configuration options.
License
This project is licensed under MIT and includes:
- v1.9.4 Leaflet (BSD 2-Clause License)
- v0.81.1 Leaflet.Locate (MIT)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-map-location-0.9.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 414536e7e5a5593402ecf87cc962d5a78f631cd29bc9e6e3e8ed2dd9655fc99c |
|
MD5 | 3319f4a38dbcd7978db5900bc6ff91e1 |
|
BLAKE2b-256 | 9380a5b4a3b4b84df4567e4ef28390a195500a86aa82576e3c11023c88b9db43 |
Hashes for django_map_location-0.9.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c34971b13cd54aa6a774398076a49da59e134531fb22169eeeffa1e3202442dc |
|
MD5 | d31bd15c9d5f4ada94997534ab79e126 |
|
BLAKE2b-256 | 148deb1c5baee4ec8409d7b5410d455d8ad5b2386ca89d20ed45aa781f23d132 |