Django Admin - Leaflet:以编程方式向现有地图添加标记
Django Admin - Leaflet: Programmatically add a marker to existing map
models.py:
class Point(models.Model):
point = models.PointField()
admin.py:
admin.site.register(Point, LeafletGeoAdmin)
一切都很好。
但是,我想在按下按钮时手动添加指向管理地图的指针。
templates/admin/myapp/point/change_form.html:
{% extends "admin/change_form.html" %}
{% block after_field_sets %}
<input type="button" id="startWatchButton" value="Show marker" />
{% endblock %}
javascript:
$(function() {
$("#startWatchButton").click(function() {
var mymap = <How to select the initialized django map?>
var marker = L.marker([51.5, -0.09]).addTo(mymap);
});
});
如何 select 已经存在并已初始化的 django 管理映射?
成功的步骤:
- 睡一觉
- Read the Docs
- 实施
You can use the Leaflet API as usual. There are two ways to grab a
reference on the just initialized map and options.
...
<script type="text/javascript">
window.addEventListener("map:init", function (e) {
var detail = e.detail;
...
L.marker([50.5, 30.5]).addTo(detail.map);
...
}, false);
</script>
models.py:
class Point(models.Model):
point = models.PointField()
admin.py:
admin.site.register(Point, LeafletGeoAdmin)
一切都很好。
但是,我想在按下按钮时手动添加指向管理地图的指针。
templates/admin/myapp/point/change_form.html:
{% extends "admin/change_form.html" %}
{% block after_field_sets %}
<input type="button" id="startWatchButton" value="Show marker" />
{% endblock %}
javascript:
$(function() {
$("#startWatchButton").click(function() {
var mymap = <How to select the initialized django map?>
var marker = L.marker([51.5, -0.09]).addTo(mymap);
});
});
如何 select 已经存在并已初始化的 django 管理映射?
成功的步骤:
- 睡一觉
- Read the Docs
- 实施
You can use the Leaflet API as usual. There are two ways to grab a reference on the just initialized map and options.
...
<script type="text/javascript"> window.addEventListener("map:init", function (e) { var detail = e.detail; ... L.marker([50.5, 30.5]).addTo(detail.map); ... }, false); </script>