django 如何动态生成叶片/瓣叶的最佳缩放?

but5z9lq  于 2023-04-22  发布在  Go
关注(0)|答案(1)|浏览(91)

我使用leafletfolium来绘制位置。
这些位置可以被过滤掉,因此需要一些动态的东西。
我想实现两件事:
1.将用户集中在不同位置之间的Map上(有效);
1.现在我还想调节缩放级别以捕获屏幕上的所有位置-有时这种缩放可能是在街道级别,有时可能是在国家级别。
我觉得我的问题可以通过使用fitBounds来解决,根据文档 * 自动计算缩放以适应Map上的矩形区域 *。
这听起来很理想,这篇文章似乎给出了一个类似问题的答案:pre-determine optimal level of zoom in folium
小问题,我只是不明白。
我想我应该能够使用最小和最大经度和纬度来生成传单文档所指的矩形区域。
但是,如何在传单提供的缩放级别转换?

def function(request):
    markers = Model.objects.filter(location_active=True)

    #Max latitude & longitude
    min_latitude = Model.objects.filter(location_active=True).aggregate(Min('latitude'))['latitude__min']
    min_longitude = Model.objects.filter(location_active=True).aggregate(Min('longitude'))['longitude__min']

    #Min latitude & longitude
    max_latitude = Model.objects.filter(location_active=True).aggregate(Max('latitude'))['latitude__max']
    max_longitude = Model.objects.filter(location_active=True).aggregate(Max('longitude'))['longitude__max']

    sum_latitude = 0
    # sum latitude and longitude
    sum_latitude = max_latitude + min_latitude
    sum_longitude = max_longitude + min_longitude

    
    #average position for latitude and longitude
    average_latitude = sum_latitude/2
    print(f'average_latitude - {average_latitude} ')
    average_longitude = sum_longitude/2
    print(f'average_longitude - {average_longitude} ')

    center_location = [average_latitude,average_longitude]
    center_zoom_start= 12
    tiles_style = 'Stamen Terrain'
   
    
    #create Folium map
    m = folium.Map(location=center_location,zoom_start=center_zoom_start,tiles=tiles_style)
    m_access = folium.Map(location=center_location,zoom_start=center_zoom_start,tiles=tiles_style)

    context = {'markers':markers,'map_access':m_access._repr_html_,'map':m._repr_html_}
    
    return render(request,'template.html',context)
xvw2m8pv

xvw2m8pv1#

答案其实很简单,回头看。

markers = Model.objects.filter(location_active=True)

#Max latitude & longitude
min_latitude = Model.objects.filter(location_active=True).aggregate(Min('latitude'))['latitude__min']
min_longitude = Model.objects.filter(location_active=True).aggregate(Min('longitude'))['longitude__min']

#Min latitude & longitude
max_latitude = Model.objects.filter(location_active=True).aggregate(Max('latitude'))['latitude__max']
max_longitude = Model.objects.filter(location_active=True).aggregate(Max('longitude'))['longitude__max']

#average position for latitude and longitude
average_latitude = sum_latitude/2
print(f'average_latitude - {average_latitude} ')
average_longitude = sum_longitude/2
print(f'average_longitude - {average_longitude} ')

center_location = [average_latitude,average_longitude]
#center_zoom_start= 12
tiles_style = 'Stamen Terrain'

m = folium.Map(location=center_location,tiles=tiles_style) #<-- removed reference to zoom_start=center_zoom_start
m.fit_bounds([[min_latitude, min_longitude], [max_latitude, max_longitude]]) #<-- added this bit. I did not knowwe could call m like that.
m_access = folium.Map(location=center_location,tiles=tiles_style) #<-- removed reference to zoom_start=center_zoom_start

相关问题