Django地理位置视觉选择器django-map-location
django-map-location是当您只需要一个视觉位置选择器时,无需完整的安装GeoDjango,只需添加一个完全静态的位置字段来管理基于位置的数据。
1.安装
pip install django-map-location
2.添加到您的INSTALLED_APPS
INSTALLED_APPS = [...,'map_location',]
3.创建地图-位置字段
from map_location.fields import LocationFieldclass 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,# },})
选项参数
| 关键词 | 作用 |
|---|---|
| map | 地图选项(默认: {center: [20, -25], zoom: 2}) |
| markerZoom | 初始化缩放比例 (负载时) – (默认: 18) |
| tileLayer | TileLayer urlTemplate (默认: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png") |
| tileOptions | TileLayer选项 (默认: {}) |
| locate | Leaflet.Locate选项(默认: {returnToPrevBounds: true, showPopup: false}) |
如果将位置导出为 json,则可以使用完全静态的地图:
_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>
参考资料
项目github:https://github.com/relikd/django-map-location
THE END




