Beatdown
Beatdown

Reputation: 229

Can't add a geoJson layer to my Open Layers map

Using Open Layers and leaflet-sidebar-v2, I've added the sidebar to my map, this works. However, I also need to add another layer to my map, this layer will outline each country. I have the coordinates stored in a 'borders.json' file. I'm attempting to use D3.json to to import the border coordinates and then L.geoJson to add the new layer to my map.

I'm currently getting the following error message:

Uncaught TypeError: t.getLayerStatesArray is not a function

Here is the relevant part of my code..

var map = new ol.Map({
  target: "map",
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM(),
    }),
  ],
  view: new ol.View({
    center: ol.proj.transform([7, 51.2], "EPSG:4326", "EPSG:3857"),
    zoom: 3,
  }),
});

var sidebar = new ol.control.Sidebar({
  element: "sidebar",
  position: "left",
});
map.addControl(sidebar);

d3.json(("borders.json"), function (json){
    function style(feature) {
        return {
            fillColor: "transparent",
            weight: 1,
            opacity: 0.4,
            color: 'grey',
            fillOpacity: 0.3
        }
    }

    geojson = L.geoJson(json, {
      style: style,
    }).addTo(map);
})

I think I might be adding the geojson layer to my map incorrectly, but I can't figure out what is wrong. I've spent quite a bit of time playing with it, but no luck.

Any helps is appreciated.

Cheers, Beat

Upvotes: 0

Views: 569

Answers (1)

ad2969
ad2969

Reputation: 500

It might be hard to tell what the problem is without knowing other possible relevant parts of your code. I'd start by checking that the contents of borders.json follows valid GeoJSON format.


This is likely unrelated to your question, but is there a reason that you've declared style as a function like function style(feature) { ... }?

It looks like the style attribute of L.geoJson accepts an object rather than a function.

Upvotes: 1

Related Questions