0

How can i get the bounds of several kml file ? i can get easely for one kml file but i am a little be lost ... to get the bounds of all kml file who are in the map ...

here is the code (write in php file context)

at first i define the kml file needed and the js var for sources and layers (ie : src_expe_pro0 expe_pro0 ...)

$pro_kml .= '
   var src_expe_pro'.$j.' =new ol.source.Vector({
      url: "http://www.grottes-et-karsts-de-chine.org/gkc_kml_file/pro_kml_file/'.strtolower($pro_id).'.kml",
      format: new ol.format.KML()
   });
   var expe_pro'.$j.' = new ol.layer.Vector({
      source:src_expe_pro'.$j.'
    });';   
   $layers_pro_kml .= "expe_pro".$j.",
   ";

later i put this in the map (so $layers_pro_kml can be one or several layer(s))

...
         layers: [
            fond_carte,
            '.$layers_pro_kml.'
            expe_markers
         ],
...

and here come the fog ... for now i simply use this who work taking the bound of the first kml layer

   expe_pro0.once("change", function(e){
      var extension = src_expe_pro0.getExtent();
      map.getView().fit(extension);
   });

so in fact the question is how to work with the arrays (extension) to reduce at one array we will give me the bounds of all the kml layers ? is there a fonction in the api or must i play with the arrays ?? or perhaps a simple way ?

Any advice

Thanks

With the advice of mike (thanks) i use this and its work perfectly ..

      var extension = ol.extent.createEmpty();
      map.getLayers().forEach(function(layer){
         if(!layer.values_.id) {
            layer.once("change", function(e){
               ol.extent.extend(extension, layer.getSource().getExtent());
               map.getView().fit(extension);
            });
         }
      });
jipexu
  • 3
  • 5

1 Answers1

1

Try something like this. The sources might load in any order so it would be simplest to re-fit as each loads.

     // before opening map set listeners for vectors loading
     // and use extents to fit the map
     var extension = ol.extent.createEmpty();
     [
        fond_carte,
        '.$layers_pro_kml.'
        expe_markers
     ].forEach(function(layer) {
       if (layer.getSource().getExtent) {
         layer.getSource().on("addfeature", function(e){
           ol.extent.extend(extension, layer.getSource().getExtent());
           map.getView().fit(extension);
         });
       }
     });
     // then open the map to load the vectors
     map.setView(
       new ol.View({
         center: [0,0],
         zoom: 0
       })
     );
Mike
  • 16,042
  • 2
  • 14
  • 30
  • thanks but i get TypeError: layer.getSource().getExtent is not a function. (In 'layer.getSource().getExtent()', 'layer.getSource().getExtent' is undefined) – jipexu Oct 18 '19 at 16:08
  • Are you only including vector layers? – Mike Oct 18 '19 at 16:42
  • 1
    You are only interested in vectors so you could limit the list or add a test to see if the source supports getExtent() – Mike Oct 18 '19 at 17:42
  • yes i triying a condition with the id of the values_ of the OSM layer – jipexu Oct 18 '19 at 17:59
  • `if (layer.getSource().getExtent) {` will restrict it to sources which support `getExtent()` – Mike Oct 18 '19 at 18:01
  • yes i see try and i have nothing on the map but no error for now – jipexu Oct 18 '19 at 18:39
  • now i got this AssertionError: Assertion failed. See https://openlayers.org/en/v6.0.0/doc/errors/#25 for details. K — Feature.js:1 fit — View.js:1018 gkc_ol_load — gkc_rcexd.php:398 Page actualisée à 20:45:13 K — Feature.js:1 – jipexu Oct 18 '19 at 18:47
  • A "change" event may fire before any features have been added. Try using `layer.getSource().on("addfeature", function(e){` instead. – Mike Oct 18 '19 at 18:56
  • white map ... without error .... i think i will try to write something similar outside of new ol.Map ??? – jipexu Oct 18 '19 at 19:11
  • You must open the map with a center and zoom ([0,0] and 0 will suffice) to get the vectors to load. Then you read their extends to fir the map. – Mike Oct 18 '19 at 19:20