1

I'm trying to add an additional ol.layer.tile on my existing map view after a 'status' 'done' Ajax post call. I have tried to use "async" and "await" on initialize the ol.map. A sample code is below:

async function MapUI()
{

// Create Base Map and Layer using OpenLayers

  var view = new ol.View
  ({
  // change projection to geoserver's projection

    projection: "EPSG:4326",
    center: [-98.5284468774974,52.71391708013823],
          zoom: 7
  });

  // creating a openlayer base map

  var osm_map = await new ol.layer.Tile
  ({
    source: new ol.source.OSM()
  });

  // creating a map layer from geoserver

  var vector_source = await new ol.source.Vector
  ({
        url: MyGeoJsonURL, 
        format: new ol.format.GeoJSON()
  });

  var vector_map = await new ol.layer.Vector
  ({
    source: vector_source 

  });

  var raster_source = await new ol.source.TileWMS
  ({
    url:'http://localhost:8080/geoserver/Canola/ows?',
    params:{'LAYERS': 'Canola:Riskmap'},
    serverType: 'geoserver',
    crossOrigin:'anonymous'
  });

  var wms_map = await new ol.layer.Tile({

    source:raster_source
  });

  var map = await new ol.Map({

    layers: [osm_map, vector_map,wms_map],
    target: 'map',
    view: view
    });
}

Or the after "done" do something method inside of the call, like shown below

$.post("../lib/php/riskAnalysis.php", 
{    
     CropDensity:CropDensity,
     DiseaseHistory:DiseaseHistory,
     RegionRisk:RegionRisk,
})
.done(function(databack, status)
{
    $("#status").html("done");
    {
 var raster_source = new ol.source.TileWMS
  ({
    url:'http://localhost:8080/geoserver/Canola/ows?',
    params:{'LAYERS': 'Canola:Riskmap'},
    serverType: 'geoserver',
    crossOrigin:'anonymous'
  });

  var wms_map = new ol.layer.Tile({

    source:raster_source
  });

  map.addLayer(wms_map); // where map is the original map layer

}


})

None of the above code works... Not sure did I have any errors in my code or my approach was wrong.

  • Do you call MapUI before ajax call? – Chase Choi Sep 26 '17 at 0:53
  • @ChaseChoi Hi! Thanks for your respond! Yes I did... Everything else worked fine, I even tried it in standalone code, works fine as well.. Now I wonder it could be some syntax errors, or permission issues from the folder.. – MaxG Sep 26 '17 at 14:30

Your Answer

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

Browse other questions tagged or ask your own question.