首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Openlayers 4.6上使用不同图标的多个标记?

在Openlayers 4.6上使用不同图标的多个标记?
EN

Stack Overflow用户
提问于 2018-06-07 22:18:02
回答 1查看 5.4K关注 0票数 0

我试着在Openlayers 4.6上使用多个标记和不同的图标。问题是,当加载地图时,我只看到一个标记图像。我遗漏了什么?如何修复此错误?

代码语言:javascript
复制
var lamarin = ol.proj.fromLonLat([29.1728363, 40.8950354]);
var view = new ol.View({
  center: lamarin,
  zoom: 4
});

var vectorSource = new ol.source.Vector({});
var places = [
  [29.1728363, 40.8950354, 'http://www.binaprekast.com/wp-content/uploads/2018/04/MG_2371.jpg'],
  [29.1741285, 40.8960586, 'http://www.binaprekast.com/wp-content/uploads/2018/04/3-4.jpg'],
  [29.1728363, 40.8950354, 'http://www.binaprekast.com/wp-content/uploads/2018/04/DSC1554.png'],
];

for (var i = 0; i < places.length; i++) {
  var iconFeature = new ol.Feature({
    geometry: new ol.geom.Point(ol.proj.transform([places[i][0], places[i][1]], 'EPSG:4326', 'EPSG:3857')),
  });

  var iconStyle = new ol.style.Style({
    image: new ol.style.Icon({
      anchor: [0.5, 0.5],
      anchorXUnits: 'fraction',
      anchorYUnits: 'fraction',
      src: places[i][2],
    })
  });
}
vectorSource.addFeature(iconFeature);
iconFeature.getStyle(iconStyle);

var vectorLayer = new ol.layer.Vector({
  source: vectorSource,
  updateWhileAnimating: true,
  updateWhileInteracting: true,
  style: function(feature, resolution) {
    iconStyle.getImage().setScale(map.getView().getResolutionForZoom(18) / resolution);
    return iconStyle;
  }
});

var map = new ol.Map({
  target: 'map',
  view: view,
  layers: [
    new ol.layer.Tile({
      preload: 3,
      source: new ol.source.OSM(),
    }),
    vectorLayer,
  ],
  loadTilesWhileAnimating: true,
});

map.once('postrender', function(event) {
  view.animate({
    center: lamarin,
    zoom: 17,
    duration: 10000,
    mapTypeId: 'roadmap',
  });
});
代码语言:javascript
复制
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<div id="map" class="map"></div>

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50743577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档