I am using django-leaflet
to display a map in a template, where the goal is to only display the coordinates of the visible area of the map when the user moves the map.
For this I'm using the getBounds()
method, but the function only returns [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 %}
Why is not showing the coordinates?
As the getBounds()
returns a LatLngBounds
, for viewing the coordinates it is necessary to convert to a string, using the method toBBoxString()
.
function map_init (map, options) {
map.on('moveend', function() {
alert(map.getBounds().toBBoxString());
});
}