I created a restfulAPI using Nodejs, from which I want to create a new Vectorlayer in Openlayers and display on a map.
The GeoJSON I get from the API looks like this (JSONlint and geojson.io both say it's valid):
{
"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"
}
}]
}
The function addKioskGeoJSON should add the layer to the map:
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)
}
}
What's weird is, that I can console.log() the geojson as you can see in the image and my code, but get an 404 error message for adding it as an vector layer.
addKioskGeojson? Edit: Containsgeojsonthe URL or the features? If last, thanurl: geojsonis wrong and should possibly befeatures: geojson. - pzaenger// Add GeoJSON endpoint from kiosks table router.get('/kiosks', async ctx => { const results = await database.getKiosks() if (results.length === 0) { ctx.throw(404) } // 404 for no results ctx.body = results })and call theaddKioskGeojsonfrom my main.js file: ` /** Load map data from the API */ async loadMapData () { // Download kiosk pois const kioskGeojson = await this.api.getKiosks() // Add data to map this.mapComponent.addKioskGeojson(kioskGeojson)` - didi_ofeatures: geojsonI get this error:TypeError: collection.getArray is not a function.geojsoncontains the features - didi_oreadFeatures- Mike