我使用django-leaflet
在模板中显示地图,其中目标是在用户移动地图时仅显示地图可见区域的坐标。
为此,我使用了getBounds()
方法,但该函数只返回[Object Object]
。
template.html:
{% load leaflet_tags %}
{% block extra_head %}
{% leaflet_js %}
{% leaflet_css %}
{% endblock %}
{% block body %}
{% leaflet_map "extent" callback="map_init" %}
{% endblock %}
{% block extra_script %}
<script>
function map_init (map, options) {
map.on('moveend', function() {
alert(map.getBounds());
});
}
</script>
{% endblock %}
为什么不显示坐标?
由于getBounds()
返回一个LatLngBounds
,为了查看坐标,需要使用方法toBBoxString()
将其转换为字符串。
function map_init (map, options) {
map.on('moveend', function() {
alert(map.getBounds().toBBoxString());
});
}