如何设置django传单地图的边界?

时间:2021-06-19 16:58:34

标签: django leaflet geodjango django-leaflet

在我的 django 应用程序中,我有一个带有位置 (Point) 字段的 CenterModel 和一个带有 geom (Linestring) 字段的 RouteModel。我正在使用 django-leaflet 来表示视图 (UI) 中的那些几何字段。我可以表示这些字段,但在一般设置中,我为每个传单地图设置了默认中心和缩放,因此当我表示中心或路线时,我必须缩小并移动地图以查看标记和线,有没有什么办法可以设置地图的边界框,不用缩小和移动地图就可以看到点或线?

该点在该区域外表示,我必须缩小 The point is represented outside this area and I have to zoom out 我尝试过:

map.setBounds(datasets.getBounds());

但是显示setBounds方法不存在的错误

这是我的代码:

这就是我表示路线的方式: 在模板中:

<div class="row">
 {% leaflet_map "gis" callback="window.our_layers" %}                        
</div>

<script type="text/javascript">
    function our_layers(map,options){
        var datasets= new L.GeoJSON.AJAX("{% url 'riesgo:route_locate' route.id %}" ,{});
        
        datasets.addTo(map);        
        // this is showing an error       
        map.setBounds(datasets.getBounds());
    }                              
</script>

#ajax 视图

@login_required(login_url='/login/')
def route_get_location(request,pk):
    route=serialize('geojson',RouteModel.objects.filter(id=pk))
    return HttpResponse(route,content_type='json')

在其他模板中表示中心

<div class="row">
   {% leaflet_map "gis" callback="window.our_layers" %}
</div>

<script type="text/javascript">
    function our_layers(map,options){
        var datasets= new L.GeoJSON.AJAX("{% url 'riesgo:center_locate' center.id %}" ,{});
        datasets.addTo(map);  
        map.setBounds(datasets.getBounds());  
    }                                  
</script>

获取中心的ajax视图

@login_required(login_url='/login/')
def center_get_location(request,pk):
    center=serialize('geojson',CenterModel.objects.filter(id=pk))
    return HttpResponse(center,content_type='json')

设置:

LEAFLET_CONFIG = {
  'DEFAULT_CENTER': (18.47186, -69.89232),
  'DEFAULT_ZOOM': 12,
  'MIN_ZOOM': 7,
  'MAX_ZOOM': 19,
  'RESET_VIEW' : False,
}

1 个答案:

答案 0 :(得分:1)

尝试使用 fitBounds 而不是 setBounds

map.fitBounds(datasets.getBounds());

Related question