使用OpenLayers 5在地图上添加一个图标

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

我正在尝试使用OpenLayers 5在地图上添加一个图标。

我试图在网站上关注Openlayers示例,但没有成功(https://openlayers.org/en/latest/examples/icon.html

我相信问题可能是Style,因为当我从功能中删除它时,地图上会显示一个点,但是当我尝试向该点添加一个样式(它是图标)时,没有显示任何内容地图。

我在下面发送我使用的代码:

import Point from 'ol/geom/Point'
import { Icon, Style } from 'ol/style.js'
// or
// import Icon from 'ol/style/Icon'
// import Style from 'ol/style/Style'

...

const vectorMarkerSource = new VectorSource()

const vectorMarkerGroup = new VectorLayer({
  source: vectorMarkerSource
})

...

this.olmap = new Map({
    target: 'map',
    layers: [
      baseLayerGroup, vectorMarkerGroup
    ],
    view: this.view
})

...

var iconFeature = new Feature({
    geometry: new Point([0, 0]),
    projection: 'EPSG:4326'
})

// I've already tried the two options of 'src'
var iconStyle = new Style({
    image: new Icon(/** @type {module:ol/style/Icon~Options} */ ({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        src: '@/assets/img/marker/marker-blue.png'
        // src: '../../assets/img/marker/marker-blue.png'
    }))
})

iconFeature.setStyle(iconStyle)

vectorMarkerSource.addFeature(iconFeature)

先感谢您。

icons openlayers point openlayers-5
1个回答
0
投票

我偶然发现了一个解决方案。我正在阅读另一个代码来解决另一个问题,在这段代码中,创建者使用以下方法插入一个图标。

import markerIconBlue from '@/assets/img/marker/marker-icon-2x-blue.png'

var iconStyle = new Style({
    image: new Icon(/** @type {module:ol/style/Icon~Options} */ ({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        src: markerIconBlue
    }))
})

幸运的是,这种方法对我有用,我希望这有助于其他人。

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