我使用Nodejs创建了一个restfulAPI,我想用它在Openlayers中创建一个新的Vectorlayer并显示在地图上。
我从API得到的GeoJSON如下所示(JSONlint和geojson.io都说它是有效的):
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.9627244, 53.5565378]
},
"properties": {
"f1": 1,
"f2": "Tabakbörse",
"f3": "Beim Grünen Jäger 2, 20359 Hamburg"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.9874951, 53.5162912]
},
"properties": {
"f1": 2,
"f2": "Fähr Getränke Kiosk",
"f3": "Veringstraße 27, 21107 Hamburg"
}
}]
}函数addKioskGeoJSON应该将图层添加到地图中:
import './map.scss'
import {Map as olMap} from 'ol'
import View from 'ol/View'
import TileLayer from 'ol/layer/Tile'
import OSM from 'ol/source/OSM'
import { fromLonLat } from 'ol/proj'
import {Style, Icon} from 'ol/style'
import { Vector as layerVector } from 'ol/layer'
import { Vector as sourceVector } from 'ol/source/'
import GeoJSON from 'ol/format/GeoJSON'
import { Component } from '../component'
const template = '<div ref="mapContainer" class="map-container"></div>'
export class Map extends Component {
constructor (placeholderId, props) {
super(placeholderId, props, template)
const target = this.refs.mapContainer
// Initialize Openlayers Map
this.map = new olMap({
....
});
this.layers = {}; // Map layer dict (key/value = title/layer)
}
addKioskGeojson (geojson) {
console.log(geojson)
this.layers.kiosks = new layerVector({
title: 'Kiosks',
visible: true,
source: new sourceVector({
format: new GeoJSON(),
projection : 'EPSG:4326',
url: geojson
}),
style: new Style({
image: new Icon(({
anchor: [0.5, 40],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: './map-icons/kiosk.png'
}))
})
});
this.map.addLayer(this.layers.kiosks)
}
}
奇怪的是,我可以像您在image和我的代码中看到的那样对geojson执行console.log(),但在将其添加为向量层时却得到了404错误消息。
发布于 2019-07-05 22:10:27
你已经有了一个geojson对象(它不是一个url)。您所需要做的就是解析对象中的特征:
addKioskGeojson (geojson) {
console.log(geojson)
this.layers.kiosks = new layerVector({
title: 'Kiosks',
visible: true,
source: new sourceVector({
features: new GeoJSON().readFeatures(geojson, { featureProjection: this.map.getView().getProjection() })
}),
style: new Style({
image: new Icon(({
anchor: [0.5, 40],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: './map-icons/kiosk.png'
}))
})
});
this.map.addLayer(this.layers.kiosks)
}https://stackoverflow.com/questions/56904317
复制相似问题