Search code examples
javascriptjsonopenlayers

How to create a VectorSource in OpenLayer 6.5 from a JavasSript object?


I want to create a web app which is using OpenLayer 6.5. I want to mark some locations dynamically so I can't keep some ".geojson" files on my server. What should I do?

I'm trying to create a VectorSource from a JavaScript GeoJson object but it always shows a false location! What's wrong with this code?

var GeoJSONObject = {
    type: "FeatureCollection",
    features: [
      {
        type: "Feature",
        properties: {},
        geometry: {
          type: "Point",
          coordinates: [
            70.33447265624999,
            55.541064956111036
          ]
        }
      }
    ]
  };

var vectorSource = new ol.source.Vector({
        features: new ol.format.GeoJSON().readFeatures(GeoJSONObject)
});

var vectorLayer = new ol.layer.Vector({
    renderMode: 'image',
    source: vectorSource,
    visible: true,
    title: "Plant",
    style: new ol.style.Style({
        image: circleStyle
    })
});

map.addLayer(vectorLayer);

Solution

  • I solved this problem by using ol.style.Icon instead of Vector! If you have this problem, look at this: https://openlayers.org/en/latest/examples/icon.html