Reputation: 1300
I'm trying to put some points from a GeoDjango model on a map based on MapBox. Is the first time that I use MapBox and here I've seen how is possible to add a GeoJson on MapBox's map.
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)
map.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 %}
]
}
});
});
When I see the page source I can see the list of points:
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]
}
}
]
}
});
The problem is that the points aren't shown on the map and in Chrome console there aren't errors. What I've wrong?
Upvotes: 1
Views: 891
Reputation: 23144
You are almost there.
You have managed to add a GeoJSON source to your map but you haven't created a layer from that source.
Let's follow this example and modify it to our current needs:
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
}
});
});
There is a direct way to do it as shown in this other example (modified for our case of course!):
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
}
});
});
Upvotes: 1