【问题标题】:How to get image source from geojson openlayers3如何从geojson openlayers3获取图像源
【发布时间】:2016-08-10 10:41:09
【问题描述】:

需要为json点传递不同的不同图像。如何从 json 中给出图像 src url

矢量图层和样式

vector = new ol.layer.Vector({
  source: new ol.source.Vector({
   projection : 'EPSG:4326',
   format: new ol.format.GeoJSON(),
   url: 'resources/multipoint.geojson'
 }),
  style: styleFunction1
});

var styleFunction1 = function(feature) {
              return styles[feature.getGeometry().getType()];
            };

var styles = {
              'Point': [
              new ol.style.Style({
               image: new ol.style.Icon({
                 src: 'resources/icon.png',
                 anchor: [0.5, 1]
               })
             })],
              'LineString': [new ol.style.Style({
                stroke: new ol.style.Stroke({
                  color: 'gray',
                  width: 5
                })
              })]
            };

json

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
          "name": "Missing Person",
          "ref":" Ref 5684"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-0.12755, 51.507222]
      }
    },
    {
      "type": "Feature",
      "properties": {
          "name": "Wanted",
          "category": "cat1",
           "ref":" Ref 56124"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-0.12755, 52.507222]
      }
    },
    {
      "type": "Feature",
      "properties": {
          "name": "Missing 1",
           "ref":" Ref 1684"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-1.12755, 52.507222]
      }
    }    
  ]
}

如何从json传递src: 'resources/Blue_pointer.png',

【问题讨论】:

    标签: javascript json openlayers-3


    【解决方案1】:

    将您的样式函数更改为:

    var styleFunction1 = function(feature) {
      var styles = {
        'Point': [
          new ol.style.Style({
            image: new ol.style.Icon({
              src: feature.get('src'),
              anchor: [0.5, 1]
            })
          })],
        'LineString': [
          new ol.style.Style({
            stroke: new ol.style.Stroke({
              color: 'gray',
              width: 5
            })
        })]
      };
    
      return styles[feature.getGeometry().getType()];
    };
    

    并将图像源添加到您的 JSON:

    {
      "type": "Feature",
      "properties": {
          "name": "Missing Person",
          "ref":" Ref 5684",
          "src": "resources/some-img.png"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-0.12755, 51.507222]
      }
    },
    

    【讨论】:

    • 出现错误 - 无法读取 null ol.js 的属性 '0' // ol 版本:v3.16.0
    • 在 styleFunction1 如果我得到名字.. console.log(feature.get('name'));但没有得到 src console.log(feature.get('src'));
    • 现在工作..它只需要 2 个属性.. "properties": { "name": "Wanted", "src": "resources/icon.png" },
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 2018-02-15
    相关资源
    最近更新 更多