我在views.py
中有这个:
import json
import requests
def geojsonFun(request):
r = requests.get('http://localhost:8000/villeLis/')
data = r.json()
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [d["coordonate_x"], d["coordonate_y"]],
},
"properties": d,
} for d in data]
}
# print(geojson)
return HttpResponse(geojson)
我使用序列化程序从我的mysql数据库中获取json文件,并且该链接正在工作,问题是我无法在dashboard.html
上的地图上看到我的观点,这是其中的一部分:
<script>
var geojson = geojson;
mapboxgl.accessToken = 'pk.eyJ1IjoieW9zcmFqYWJyaSIsImEiOiJjazh4M3duc3AwMnJhM2VzMmxjOThxa2F6In0.OAugyooi_oKgzRTznR4eyw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [9.5, 36.5],
zoom: 7
});
map.addSource(currentKey, {
"type": "geojson",
"data": geojson,
cluster: true
});
map.addLayer({
id: 'points',
source: 'pointsSource',
type: 'circle',});
</script>
我想在python中检索该函数的结果,并将其用作js中的geojson。
在addLayer中,您提到源是pointsSource
,但是源名称是'currentKey',必须将名称输入为字符串