0

What am I missing, trying to get Geojson object to get passed with a style, but its not working and there is no console error as well. What do I do?

var olview = new ol.View({
  center: [0, 869099],
  zoom: 7,
  minZoom: 2,
  maxZoom: 20
})

myMap = new ol.layer.Tile({
  source: new ol.source.TileWMS({
        url: 'http://localhost:8080/geoserver/wms',
        params: {'LAYERS': 'basemap', 'TILED': true },
        serverType: 'geoserver'
      })
})
/*`var myMap = new ol.layer.Tile({
      source: new ol.source.OSM()
    });`
*/
// assuming you want to try it without having geoserver, use this

var geoJsonObject = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.6149902343749998,
          6.118707747190845
        ]
      }
    }
  ]
};

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

var iconStyle = new ol.style.Style({
        image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
          anchor: [0.5, 46],
          anchorXUnits: 'fraction',
          anchorYUnits: 'pixels',
          src: 'https://openlayers.org/en/v4.2.0/examples/data/icon.png'
        }))
      });

var geojsonVector = new ol.layer.Vector({
  source: geojsonVector,
  style: iconStyle
});

var map = new ol.Map({
  target: 'map',
  layers: [myMap, geojsonVector],
  view: olview
});
2

The section

var geojsonVector = new ol.layer.Vector({
  source: geojsonVector,
  style: iconStyle
});

Sets the source of the layer geojsonVector to geojsonVector (itself). This should be set to geojson instead.

  • so it should be the geojson object instead. Thanks – saviour123 Aug 19 '17 at 12:16
2

This worked for me, assuming there is a json file with the features:

var sampleLayerSource = new ol.source.Vector({
  url: (basePath + '/data/sample_layer.json'), // url to your json
  format: new ol.format.GeoJSON()
});

I've also tried readFeatures(), but I didn't get any features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.