使用django-leaflet我创建了一个表单如下
class AnnotationForm(forms.ModelForm):
<crispy forms stuff redacted from here for brevity>
class Meta:
model = Annotation # Your user model
fields= ['name','annotationType','description','location_point','document','e']
widgets = {'location_point': LeafletWidget(),'e':forms.HiddenInput()}
Run Code Online (Sandbox Code Playgroud)
它以正常方式从视图中实例化.
在模板中; -
{% crispy annotation_form %}
Run Code Online (Sandbox Code Playgroud)
现在.在主体加载的开始,它隐藏包含地图的div,而在其他地方有一些代码基本上监视annotationType下拉列表,如果选择"location",它将取消隐藏div.
问题是,看来leafletjs不喜欢隐藏实例化,似乎对它的界限有点困惑.我假设答案就是在地图上调用_onresize(),但我似乎无法找到如何获取对地图实例的引用.
L.map('id_annotation_location_point_map')._onResize()
Run Code Online (Sandbox Code Playgroud)
..只是抱怨地图已经初始化.检查表单生成的代码似乎是在(function(){etc etc})()类型闭包内创建了映射,这使得只是从那里劫持变量是不可行的.
所以我的问题是,在这种情况下,如何获得对leaflet.js地图的引用?
有如何只使用DOM对象获取leaflet.js实例?但它似乎没有真正回答这个问题,只是提出了一个在我的案例中没有的替代方案.