使用mapbox-gl-js为要素集合中的每个要素添加自定义图标

问题描述 投票:2回答:1

我需要使用mapbox-gl-js为地图中的每个点设置不同的自定义图像,但是我找不到为要素集合中的每个要素提供自定义图标的方法

incidentMarkers = {
"type": "FeatureCollection"
"features": [{
  "type": "geojson",
  "data": {
    "type": "Feature",
    "properties": {
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        longitude
        latitude
      ]
    }
  }
},
  {
    "type": "geojson",
    "data": {
      "type": "Feature",
      "properties": {
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          longitude
          latitude
        ]
      }
    }
  }]
}

 map.addSource('incidentMarkers', {
    "type": "geojson"
    "data": incidentMarker
  })


window.map.addLayer({
    "id": 'incidentMarkers',
    "type": "symbol",
    "source": 'incidentMarkers' 
    "layout": {
      "icon-image": "image-1",
      "icon-size": 0.25,
      "icon-allow-overlap": true,
      "text-allow-overlap": true
    }
  })

现在我将每个点添加为单独的图层,以便为每个图标提供自定义图像,但是对于具有标记的聚类,我需要将所有标记作为相同的图层,是否有任何方法可以为每个图层添加自定义图像

pointsData.forEach (data) ->
    window.map.loadImage("#{data.category_image_path}", (e, image) ->
      window.map.addImage("image-#{data.id}", image)
  incidentMarker = {
    "type": "Feature",
    "properties": {
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        data.longitude
        data.latitude
      ]
    }
  }
  map.addSource('incidentMarkers' + data.id, {
    "type": "geojson",
    "data": incidentMarker
  })

  window.map.addLayer({
    "id": 'incidentMarkers' + data.id,
    "type": "symbol",
    "source": 'incidentMarkers' + data.id
    "layout": {
      "icon-image": "image-#{data.id}",
      "icon-size": 0.25,
      "icon-allow-overlap": true,
      "text-allow-overlap": true
    }
  })

如果我在同一个latlng中有多个标记只显示一个标记,甚至我将icon-allow-overlap选项设置为true

javascript mapbox mapbox-gl-js
1个回答
5
投票

如果您引用每个要素属性中应使用的图标,则可以使用mapbox的数据驱动样式功能为每个要素使用不同的图标:

const geojson = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      properties: {icon: 'image-1'},
      geometry: {/* */}
    },
    {
      type: 'Feature',
      properties: {icon: 'image-2'},
      geometry: {/* */}
    }
  ]
}

// add source

map.addLayer({
  type: 'symbol',
  source: 'source-id',
  layout: {
    'icon-image': ['get', 'icon']
  }
})

['get', 'icon']是一个表达式,它从每个特征“获取”属性“图标”并将其用作icon-image的值。

© www.soinside.com 2019 - 2024. All rights reserved.