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

How to show GeoJson points layer from GeoDjango on MapBox

我正在尝试将 GeoDjango 模型中的一些点放在基于 MapBox 的地图上。这是我第一次使用 MapBox,here 我看到了如何在 MapBox 的地图上添加 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)

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 %}
         ]
     }
  });

});

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

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]
             }
           } 
         ]
     }
  });

问题是地图上没有显示这些点,Chrome 控制台中也没有错误。我有什么问题吗?

你快到了。

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

我们跟随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
    }
  });
});