【问题标题】:Adding custom icons for each feature in feature collection with mapbox-gl-js使用 mapbox-gl-js 为特征集合中的每个特征添加自定义图标
【发布时间】:2018-12-31 01:31:50
【问题描述】:

我需要使用 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
    }
  })

如果我在同一个纬度有多个标记,则仅显示一个标记,即使我将 icon-allow-overlap 选项设置为 true

【问题讨论】:

    标签: javascript mapbox mapbox-gl-js


    【解决方案1】:

    如果您在每个功能的属性中引用应该使用哪个图标,您可以使用 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”并将其用作icon-image 的值。

    【讨论】:

    • 新图层已添加到地图(map.getStyle().layers 检查图层),但地图上不显示标记
    • 要素中的icon 属性需要引用地图上可用的图标才能使用。这取决于您使用的地图样式。您可以使用map.listImages()map.hasImage() 来检查哪些图标可用。
    • 谢谢,成功了。但是,如果我想检查图像是否已经使用 map.hasImage 上传(参见我的编辑),它会在每次迭代时检查图像,在所有图像添加到地图之前,我真的不知道顺序
    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多