I've been looking into GeoDjango recently, I'm struggling to customize the default zoom level of the openstreet map displaying in my admin section. Below is what I have tried but with no effect, please assist.
from django.contrib.gis import admin
class LocationAdmin(admin.OSMGeoAdmin):
default_zoom = 5
admin_site.register(ReferenceSpaceLocation, LocationAdmin)
My model
class ReferenceSpaceLocation(models.Model):
geometry = models.GeometryField()
The default value for point_zoom
with OSMGeoAdmin
is 14 which is what your's seems to be at the moment. Try overwriting point_zoom
in LocationAdmin
. As is explained below, this should work if you are using PointField
or MultiPointField
.
The default zoom level will depend upon a few different things:
default_zoom
will be used. This can be set as follows:class LocationAdmin(admin.OSMGeoAdmin):
default_zoom = 5
PointField
or MultiPointField
then then it looks like the point_zoom
value will be used instead of the default_zoom
. This can be set as follows:class LocationAdmin(admin.OSMGeoAdmin):
point_zoom = 10
If you look in the source code, you'll see the following in the js file that run's that admin view:
// django/contrib/gis/templates/gis/admin/openlayers.js
var wkt = document.getElementById('{{ id }}').value;
if (wkt){
...
// Zooming to the bounds.
{{ module }}.map.zoomToExtent(admin_geom.geometry.getBounds());
if ({{ module }}.is_point){
{{ module }}.map.zoomTo({{ point_zoom }});
}
} else {
{% localize off %}
{{ module }}.map.setCenter(new OpenLayers.LonLat({{ default_lon }}, {{ default_lat }}), {{ default_zoom }});
{% endlocalize %}
}
This means that:
default_zoom
is used.PointField
or MultiPointField
then {{module}}.is_point
is true, and as can be seen point_zoom
is used. zoomToExtent(admin_geom.geometry.getBounds())
. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With