3

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
asked Sep 18, 2017 at 15:56
1

1 Answer 1

3

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
 })
})
.......
answered Sep 18, 2017 at 19:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.