I'm using OpenLayers 4.2. Currently I add a source vector to a layer vector like this :
var vectorSource = new ol.source.Vector({
url: 'http://example/carto.geojson',
format: new ol.format.GeoJSON()
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
I'd like to load my source from a string.
(url = "'{"type":"FeatureCollection",
"features":[{"type":"Feature",
"id":0,
"geometry":{"type":"Point",
"coordinates":[-1.68117,
48.3717]},
"properties":{"titre":"Nutus",
"datedebut":"1 avril 2018",
"datefin":"12 avril 2018",
"lieu":"ETANG AUX MOINES"}},
{"type":"Feature",
"id":1,
"geometry":{"type":"Point",
"coordinates":[-4.50069,
48.4083]},
"properties":{"titre":"Nutus",
"datedebut":"1 avril 2018",
"datefin":"12 avril 2018",
"lieu":"BREST "}}]}"';)
Is it possible and how?
JGH
44.4k3 gold badges49 silver badges95 bronze badges
-
Not exactly a duplicate, but you may find some inspiration here gis.stackexchange.com/questions/244285/…deg– deg2017年09月18日 17:50:25 +00:00Commented Sep 18, 2017 at 17:50
1 Answer 1
What about this ...
const format = new ol.format.GeoJSON();
const features = format.readFeatures(JSON.parse(`{"type":"FeatureCollection",
"features":[{"type":"Feature",
"id":0,
"geometry":{"type":"Point",
"coordinates":[-1.68117,
48.3717]},
"properties":{"titre":"Nutus",
"datedebut":"1 avril 2018",
"datefin":"12 avril 2018",
"lieu":"ETANG AUX MOINES"}},
{"type":"Feature",
"id":1,
"geometry":{"type":"Point",
"coordinates":[-4.50069,
48.4083]},
"properties":{"titre":"Nutus",
"datedebut":"1 avril 2018",
"datefin":"12 avril 2018",
"lieu":"BREST "}}]}`))
const vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: features
})
})
.......
lang-js