【问题标题】:how to add markers with OpenLayers 3如何使用 OpenLayers 3 添加标记
【发布时间】:2014-06-19 20:35:16
【问题描述】:

我正在尝试在 OpenLayers 3 地图上添加制造商。

我发现的唯一示例是this 中的一个OpenLayers 示例list

但该示例使用ol.Style.Icon 而不是OpenLayers 2 中的OpenLayers.Marker

第一个问题

唯一的区别是您必须设置图像 URL,但这是添加标记的唯一方法吗?

另外,OpenLayers 3 似乎没有标记图像,所以如果除了ol.Style.Icon 之外别无他法,那就有意义了

第二个问题

如果有人能给我一个在地图加载后添加标记或图标的函数示例,那就太好了。

根据我在示例中的理解,他们为图标创建了一个图层

var iconFeature = new ol.Feature({
  geometry: new ol.geom.Point(ol.proj.transform([-72.0704, 46.678], 'EPSG:4326', 'EPSG:3857')),
  name: 'Null Island',
  population: 4000,
  rainfall: 500
});


var iconStyle = new ol.style.Style({
  image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
    anchor: [0.5, 46],
    anchorXUnits: 'fraction',
    anchorYUnits: 'pixels',
    opacity: 0.75,
    src: 'data/icon.png'
  }))
});

iconFeature.setStyle(iconStyle);

var vectorSource = new ol.source.Vector({
  features: [iconFeature]
});

var vectorLayer = new ol.layer.Vector({
  source: vectorSource
});

然后他们在初始化地图的时候设置图标层

var map = new ol.Map({
  layers: [new ol.layer.Tile({ source: new ol.source.OSM() }), vectorLayer],
  target: document.getElementById('map'),
  view: new ol.View2D({
    center: [0, 0],
    zoom: 3
  })
});

如果我想添加许多标记,是否必须为每个标记创建 1 个图层?

如何向一个图层添加许多标记?我不知道这部分会是什么样子 喜欢

var vectorSource = new ol.source.Vector({
  features: [iconFeature]
});

var vectorLayer = new ol.layer.Vector({
  source: vectorSource
});

谢谢

【问题讨论】:

  • 为了性能避免一一添加功能,使用批量添加vectorSource.addFeatures(features);
  • 您找到的示例的链接已失效。

标签: javascript openlayers-3


【解决方案1】:

第一季度。标记在 OpenLayers 2 中被认为已弃用,尽管这在文档中并不是很明显。相反,您应该使用 OpenLayers.Feature.Vector 并将 externalGraphic 设置为其样式中的某个图像源。这个概念已经被带到 OpenLayers 3,所以没有标记类,它是按照你引用的例子完成的。

第二季度。 ol.source.Vector 采用一组特征,注意这一行,特征:[iconFeature],因此您将创建一组图标特征并向其中添加特征,例如:

var iconFeatures=[];

var iconFeature = new ol.Feature({
  geometry: new ol.geom.Point(ol.proj.transform([-72.0704, 46.678], 'EPSG:4326',     
  'EPSG:3857')),
  name: 'Null Island',
  population: 4000,
  rainfall: 500
});

var iconFeature1 = new ol.Feature({
  geometry: new ol.geom.Point(ol.proj.transform([-73.1234, 45.678], 'EPSG:4326',     
  'EPSG:3857')),
  name: 'Null Island Two',
  population: 4001,
  rainfall: 501
});

iconFeatures.push(iconFeature);
iconFeatures.push(iconFeature1);

var vectorSource = new ol.source.Vector({
  features: iconFeatures //add an array of features
});

var iconStyle = new ol.style.Style({
  image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
    anchor: [0.5, 46],
    anchorXUnits: 'fraction',
    anchorYUnits: 'pixels',
    opacity: 0.75,
    src: 'data/icon.png'
  }))
});


var vectorLayer = new ol.layer.Vector({
  source: vectorSource,
  style: iconStyle
});

显然,这可以通过将所有 ol.Feature 创建放在基于某些数据源的循环中来更优雅地处理,但我希望这能证明这种方法。另请注意,您可以将样式应用于 ol.layer.Vector,以便将其应用于添加到图层的所有要素,而不必为单个要素设置样式,假设您希望它们成为显然是一样的。

编辑:这个答案似乎不起作用。这是一个更新的fiddle,它通过使用vectorSource.addFeature 将特征(图标)添加到循环中的空向量源中,然后将全部添加到层向量中。在更新我的原始答案之前,我会等待看看这是否适合你。

【讨论】:

  • 谢谢!这正是我所需要的。我无法通过文档弄清楚。
  • 不客气。文档仍然有点稀疏,但公平地说,他们仍在处理代码:D
  • jsfiddle 不产生结果
  • 我什么也没看到。我不知道我需要一个帐户才能看到结果。
  • @JohnBarça 的小提琴(提琴的 ty)不再起作用,因为 openlayer 库 en 图标的 url 不再存在。这是更新版本jsfiddle.net/6RS2z/352
【解决方案2】:

有一个很好的教程在:http://openlayersbook.github.io

未经测试,但可能有帮助

var features = [];

//iterate through array...
for( var i = 0 ; i < data.length ; i++){
    var item = data[i];                                     //get item
    var type = item.type;                                   //get type
    var longitude = item.longitude;                         //coordinates
    var latitude = item.latitude;
    /*....
    * now get your specific icon...('..../ic_customMarker.png')
    * by e.g. switch case...
    */
    var iconPath = getIconPath(type);

    //create Feature... with coordinates
    var iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.transform([longitude, latitude], 'EPSG:4326',     
        'EPSG:3857'))
    });

    //create style for your feature...
    var iconStyle = new ol.style.Style({
        image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        opacity: 0.75,
        src: iconPath
        }))
    });

    iconFeature.setStyle(iconStyle);
    features.push(iconFeature);
    //next item...
}

/*
* create vector source
* you could set the style for all features in your vectoreSource as well
*/
var vectorSource = new ol.source.Vector({
    features: features      //add an array of features
    //,style: iconStyle     //to set the style for all your features...
});

var vectorLayer = new ol.layer.Vector({
    source: vectorSource
});
map.addLayer(vectorLayer);

【讨论】:

  • 谢谢!!! map.addLayer() 是我所缺少的!我试图将图层推送到 map.layers 中,但不起作用。
【解决方案3】:
var exampleLoc = ol.proj.transform(
    [131.044922, -25.363882], 'EPSG:4326', 'EPSG:3857');

var map = new ol.Map({
  target: 'map',
  renderer: 'canvas',
  view: new ol.View2D({
    projection: 'EPSG:3857',
    zoom: 3,
    center: exampleLoc
  }),
  layers: [
    new ol.layer.Tile({source: new ol.source.MapQuest({layer: 'osm'})})
  ]
});

map.addOverlay(new ol.Overlay({
  position: exampleLoc,
  element: $('<img src="resources/img/marker-blue.png">')
      .css({marginTop: '-200%', marginLeft: '-50%', cursor: 'pointer'})
      .tooltip({title: 'Hello, world!', trigger: 'click'})
}));

map.on('postcompose', function(evt) {
  evt.vectorContext.setFillStrokeStyle(
      new ol.style.Fill({color: 'rgba(255, 0, 0, .1)'}),
      new ol.style.Stroke({color: 'rgba(255, 0, 0, .8)', width: 3}));
  evt.vectorContext.drawCircleGeometry(
      new ol.geom.Circle(exampleLoc, 400000));
});

var exampleKml = new ol.layer.Vector({
  source: new ol.source.KML({
    projection: 'EPSG:3857',
    url: 'data/example.kml'
  })
});
map.addLayer(exampleKml);

【讨论】:

    【解决方案4】:

    我们刚刚完成将我们的网站NUFOSMATIC 从 ol2 更新到 ol6。 ol2 和 ol3 代码都在网站上。这包括 Matt Walker 的 ol-layerswitcher https://github.com/walkermatt 替换缺少的 ol2 layerswitcher。我们实际上更新了三个地图应用程序; HEATMAP 将 Patrick Wied (http://www.patrick-wied.at) ol2 热图替换为原生 ol6 热图。

    只用了 6 天。想知道为什么我们等了这么久......哦,是的,我们有日常工作......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      相关资源
      最近更新 更多