我正在使用Django和geemap模块,在其中我试图制作一个可以在地图上显示卫星数据的应用程序,并且地图也应该是交互式的,因为应该有从前端(Django模板)到后端(Python脚本)的双向数据流。
到目前为止,我只知道如何在Jupyter Notebook cell或Colab上显示geemap.Map()的实例(我们只需要为它编写变量的名称)。但是,我不知道如何在Django模板中显示geemap.Map()的实例。
当我使用以下方法时,它只是将实例对象打印为字典,而不是将其解释为映射并显示相同的映射。
My views.py的代码
from django.http import HttpResponse
from django.shortcuts import render
import geemap as gm
#import pandas as pd
def params(request):
g_map = gm.Map()
return render(request, "PlotMap/params.html", { "m" : g_map })
模板代码(params.html)
<!DOCTYPE html>
{% load static %}
<html>
<head>
<meta charset="utf-8">
<title>map</title>
</head>
<body>
{{ m }}
</body>
</html>
我得到的输出如下。output
如果有人能帮我,那就太好了,谢谢。
您可以使用geemap.foliumap.Map()或folium.Map()
html模板代码
<!DOCTYPE html>
{% load static %}
<html>
<head>
<meta charset="utf-8">
<title>map</title>
{{ map.header.render|safe }}
</head>
<body>
<div class="map">
{{ map.html.render|safe }}
</div>
</body>
<script> {{ map.script.render | safe }}</script>
</html>
后端代码(views.py)
import folium
import geemap.foliumap as geemap
class map(TemplateView):
template_name = 'map.html'
def get_context_data(request):
figure = folium.Figure()
Map = geemap.Map(plugin_Draw = True,
Draw_export = True)
Map.add_to(figure)
figure.render()
return {"map": figure}
urls.py的代码
urlpatterns = [
path('', views.map.as_view(), name = 'map'),
]