Newbie
Newbie

Reputation: 1

How to assign a map layer to a button?

I am trying to assign a maplayer GEOJSON to a button , whereas i have tried making it as a function but its not working well

 <button type="button" class="btn btn-secondary btn-sm" id="geojson1" onclick="layer1()"> 
                                GeoJSON</button>

function layer1(){
 rainfall1 = new ol.layer.Tile({
   title: "layer1",
   source: new ol.source.TileWMS({
     url: './data/bsk.geojson',
    
     // Countries have transparency, so do not fade tiles:
     transition: 0,
   }),

Please help me how to assign this to a button.

Upvotes: 0

Views: 82

Answers (1)

Sebastian
Sebastian

Reputation: 327

Your are only creating a layer, but you don't add it to the map. You need to

function layer1(){
 let rainfall1 = new ol.layer.Tile({
   title: "layer1",
   source: new ol.source.TileWMS({
     url: './data/bsk.geojson',
    
     // Countries have transparency, so do not fade tiles:
     transition: 0,
   });

  yourMAP.addLayer(rainfall1);
}

Upvotes: 1

Related Questions