我正在开发一个基于 postgres 的 django 项目来创建平面图。我有 python3 的经验,但 js 对我来说是新的。我需要它在接下来的 6 天内以非常基本的功能运行。所以是的:我的汗水变酸了。
目标非常简单:带有更新覆盖的平面图(例如蓝色矩形标记免费存储等)。
我需要的:
- 平面图作为底图
- 矩形(更新)覆盖
- 标记
Django-leaflet 工作正常,但 rastercoord 正在杀死我。我只是尝试将演示代码实现到我的模板 html 中,它似乎只是默默地无法执行。Django-leaflet 本身就像一个魅力,显示标记等等。
我修改了原始示例代码以使用来自维基百科的平铺银河图像,效果很好。
/*
* @copyright 2015 commenthol
* @license MIT
*/
/* global L */
;(function (window) {
function init (mapid) {
var minZoom = 0
var maxZoom = 5
var img = [
6000, // original width of image `karta.jpg`
3000 // original height of image
]
// create the map
var map = L.map(mapid, {
minZoom: minZoom,
maxZoom: maxZoom
})
// assign map and image dimensions
var rc = new L.RasterCoords(map, img)
// set the view on a marker ...
map.setView(rc.unproject([1, 1447]), 4)
// the tile layer containing the image generated with gdal2tiles --leaflet ...
L.tileLayer('./tiles/{z}/{x}/{y}.png', {
noWrap: true,
attribution: 'VOID'
}).addTo(map)
}
init('map')
}(window))
但是在 django 中实现没有任何反应。地图以默认设置显示。模板 HTML:
<!doctype html>
<html lang="de">
{% load leaflet_tags %}
<head>
<meta name=Locator charset="utf-8" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/tachyons/css/tachyons.min.css">
<title>EUT Locator</title>
{% leaflet_js plugins="ALL" %}
{% leaflet_css plugins="ALL" %}
</head>
<style>
* {
font-family: avenir;
margin: 1%;
}
.leaflet-container { /* all maps */
width: 600px;
height: 600px;
}
</style>
<body>
<h1>This is what you get</h1>
<p>Not much, really</p>
<script type="text/javascript">
function map_init_raster (mapid, options) {
var minZoom = 0
var maxZoom = 5
var img = [
6000, // original width of image `karta.jpg`
3000 // original height of image
]
// create the map
var map = L.map(mapid, {
minZoom: minZoom,
maxZoom: maxZoom
})
// assign map and image dimensions
var rc = new L.rastercoords(map, img)
// set the view on a marker ...
map.setView(rc.unproject([1, 1447]), 4)
// the tile layer containing the image generated with gdal2tiles --leaflet ...
L.tileLayer('./milkyway/{z}/{x}/{y}.png', {
noWrap: true,
attribution: 'VOID'
}).addTo(map)
}
init('map')
}
</script>
{% leaflet_map "yourmap" callback="window.map_init_raster" %}
</body>
</html>
settings.py 中有趣的部分:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
LEAFLET_CONFIG = {
'SRID' : 3857,
'DEFAULT_CENTER': (0, 0),
'DEFAULT_ZOOM': 1,
'MIN_ZOOM': 0,
'MAX_ZOOM': 5,
'RESET_VIEW': False,
'PLUGINS': {
'rastercoords': {
'css': '',
'js': 'leaflet-rastercoords/rastercoords.js',
'auto-include': True,
},
}
}
含泪欢呼霍贝尔