【问题标题】:How to set bounds to django leaflet map?如何设置 Django 传单地图的边界?
【发布时间】:2021-09-04 00:12:34
【问题描述】:

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

该点表示在该区域之外,我必须缩小 我尝试过:

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,
}

【问题讨论】:

    标签: django leaflet geodjango django-leaflet


    【解决方案1】:

    尝试使用fitBounds 而不是setBounds

    map.fitBounds(datasets.getBounds());
    

    Related question

    【讨论】:

    • 对不起,它不起作用,控制台显示“未捕获的错误:边界无效。”。似乎函数 getBounds 给出了一个 fitBounds 无法识别为有效的值
    • 尝试在您的 our_layers javascript 函数中插入 console.log(datasets.getBounds()); 并检查控制台以查看它返回的内容。
    • 我不得不使用 django-geojson function get_geom(map,options){ var geom = {{ worker|geojsonfeature|safe }}; var feature = L.geoJson(geom).addTo(map); map.fitBounds(feature.getBounds()); map.setZoom(12) }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2014-09-19
    相关资源
    最近更新 更多