diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py index df5cd9b3f0..e48ba96865 100644 --- a/django/contrib/gis/forms/widgets.py +++ b/django/contrib/gis/forms/widgets.py @@ -103,10 +103,11 @@ class OSMWidget(OpenLayersWidget): template_name = 'gis/openlayers-osm.html' default_lon = 5 default_lat = 47 + default_zoom = 12 def __init__(self, attrs=None): super().__init__() - for key in ('default_lon', 'default_lat'): + for key in ('default_lon', 'default_lat', 'default_zoom'): self.attrs[key] = getattr(self, key) if attrs: self.attrs.update(attrs) diff --git a/django/contrib/gis/templates/gis/openlayers-osm.html b/django/contrib/gis/templates/gis/openlayers-osm.html index 24ede86ff0..88b1c8c2b6 100644 --- a/django/contrib/gis/templates/gis/openlayers-osm.html +++ b/django/contrib/gis/templates/gis/openlayers-osm.html @@ -4,6 +4,7 @@ {% block options %}{{ block.super }} options['default_lon'] = {{ default_lon|unlocalize }}; options['default_lat'] = {{ default_lat|unlocalize }}; +options['default_zoom'] = {{ default_zoom|unlocalize }}; {% endblock %} {% block base_layer %} diff --git a/docs/ref/contrib/gis/forms-api.txt b/docs/ref/contrib/gis/forms-api.txt index 32cdd51dad..4058ced4d9 100644 --- a/docs/ref/contrib/gis/forms-api.txt +++ b/docs/ref/contrib/gis/forms-api.txt @@ -185,6 +185,12 @@ Widget classes The default center latitude and longitude are ``47`` and ``5``, respectively, which is a location in eastern France. + .. attribute:: default_zoom + + .. versionadded:: 2.0 + + The default map zoom is ``12``. + The :class:`OpenLayersWidget` note about JavaScript file hosting above also applies here. See also this `FAQ answer`_ about ``https`` access to map tiles. diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 323b51fbf4..f029153708 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -75,6 +75,9 @@ Minor features * Any :class:`~django.contrib.gis.geos.GEOSGeometry` imported from GeoJSON now has its SRID set. +* Added the :attr:`.OSMWidget.default_zoom` attribute to customize the map's + default zoom level. + :mod:`django.contrib.messages` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/gis_tests/test_geoforms.py b/tests/gis_tests/test_geoforms.py index 10f9d8f89b..12066da013 100644 --- a/tests/gis_tests/test_geoforms.py +++ b/tests/gis_tests/test_geoforms.py @@ -328,11 +328,14 @@ class OSMWidgetTest(SimpleTestCase): def test_default_lat_lon(self): self.assertEqual(forms.OSMWidget.default_lon, 5) self.assertEqual(forms.OSMWidget.default_lat, 47) + self.assertEqual(forms.OSMWidget.default_zoom, 12) class PointForm(forms.Form): p = forms.PointField( widget=forms.OSMWidget(attrs={ - 'default_lon': 20, 'default_lat': 30 + 'default_lon': 20, + 'default_lat': 30, + 'default_zoom': 17, }), ) @@ -341,6 +344,7 @@ class OSMWidgetTest(SimpleTestCase): self.assertIn("options['default_lon'] = 20;", rendered) self.assertIn("options['default_lat'] = 30;", rendered) + self.assertIn("options['default_zoom'] = 17;", rendered) class GeometryWidgetTests(SimpleTestCase):