gpt4 book ai didi

python - 如何在 MapBox 上显示来自 GeoDjango 的 GeoJson 点图层

转载 作者:行者123 更新时间:2023-12-01 07:09:39 24 4
gpt4 key购买 nike

我正在尝试将 GeoDjango 模型中的一些点放在基于 MapBox 的 map 上。这是我第一次使用MapBox和here我已经了解了如何在 MapBox 的 map 上添加 GeoJson。

models.py

class AddPoint(models.Model):
geom = models.PointField()

def __int__(self):
return self.pk

def coordinates_geodjango(self):
return str(self.geom.x) + ', ' + str(self.geom.y)

ma​​p.html

mapboxgl.accessToken = 'my.access.token';
var map = new mapboxgl.Map({
container: 'map',
accessToken: mapboxgl.accessToken,
style: 'mapbox://styles/maxdragonheart/cjxkimp5j5s0o1ct4b68n4x1p',
minZoom: 2,
maxZoom: 22,
logoPosition: 'bottom-right',
center: [15,41],
zoom: 4,
})

map.on('load', function () {

map.addSource('some id', {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": [{% for point in geo_objects %}
{
"type": "Feature",
"properties": {
"pk": "{{ point.pk }}"
},
"geometry": {
"type": "Point",
"coordinates": [{{ point.coordinates_geodjango }}]
}
{% if forloop.last %}} {% else %}}, {% endif %}{% endfor %}
]
}
});

});

当我看到页面源代码时,我可以看到点列表:

map.addSource('some id', {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"pk": "3"
},
"geometry": {
"type": "Point",
"coordinates": [15.996093749999993, 41.24477234308207]
}
},
{
"type": "Feature",
"properties": {
"pk": "2"
},
"geometry": {
"type": "Point",
"coordinates": [12.392578124999993, 43.13306116240612]
}
},
{
"type": "Feature",
"properties": {
"pk": "1"
},
"geometry": {
"type": "Point",
"coordinates": [14.348144531249998, 40.96330795307351]
}
}
]
}
});

问题是这些点没有显示在 map 上,并且 Chrome 控制台中没有错误。我有什么错吗?

最佳答案

你就快到了。

您已成功将 GeoJSON 源添加到 map 中,但尚未从该源创建图层。

关注this example并修改为我们当前的需求:

map.on('load', function () {

map.addSource('some-id', {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": [{% for point in geo_objects %}
{
"type": "Feature",
"properties": {
"pk": "{{ point.pk }}"
},
"geometry": {
"type": "Point",
"coordinates": [{{ point.coordinates_geodjango }}]
}
{% if forloop.last %}} {% else %}}, {% endif %}{% endfor %}
]
}
});

map.addLayer({
"id": "some-other-id",
"type": "fill",
"source": "some-id", // Here is the part where you add the source to a layer.
"paint": {
"fill-color": "#888888",
"fill-opacity": 0.4
}
});
});

有一种直接的方法可以做到这一点,如 this other example 所示。 (当然根据我们的情况进行了修改!):

map.on('load', function() {
map.addLayer({
"id": "some-other-id",
"type": "fill",
"source": {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": [{% for point in geo_objects %}
{
"type": "Feature",
"properties": {
"pk": "{{ point.pk }}"
},
"geometry": {
"type": "Point",
"coordinates": [{{ point.coordinates_geodjango }}]
}
{% if forloop.last %}} {% else %}}, {% endif %}{% endfor %}
]
}
},
"paint": {
"fill-color": "#888888",
"fill-opacity": 0.4
}
});
});

关于python - 如何在 MapBox 上显示来自 GeoDjango 的 GeoJson 点图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283210/

24 4 0
文章推荐: docker - 如何删除 kubernetes pod 的标签
文章推荐: java - 查找字符串中连续重复字符的最大数量
文章推荐: jquery - 使用 jQuery 根据值而不是文本设置选定的