Django-leaflet: map getBounds returning [object Object]

221 Views Asked by At

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?

1

There are 1 best solutions below

0
Rafael Sá On BEST ANSWER

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());
    });
  }