Code Monkey home page Code Monkey logo

Comments (2)

packetstracer avatar packetstracer commented on June 26, 2024

I'm doing a first try and think that can be done by modifying tileLoadFunction in source parameter passed to _register method in source.component.ts before setting the source to openlayers instance. Something like this seems to work with a xyz-source:

  protected _register(source: any) {
    const token = 'my-token';

    // function for AJAX request (this should be part of tileLoadFunction)
    const request = (url, token) => {
      return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onreadystatechange = () => {
          if (xhr.readyState === 4 && xhr.status === 200) {
            resolve(xhr);
          } else if (xhr.readyState === 4 && xhr.status !== 200) {
            reject(Error(xhr.statusText));
          }
        };
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.setRequestHeader('Authorization', 'Bearer ' + token);
        xhr.send();
      })
    };

    // openlayers custom load function, should be passed  as a parameter or already set as source.tileLoadFunction
    const tileLoadFunction = (imageTile: any, url) => {
      request(url, token)
        .then((xhr: any) => {
          const resp = xhr.response;
          const imagen = URL.createObjectURL(resp);
          imageTile.getImage().src = imagen;
          URL.revokeObjectURL(resp);
        })
        .catch(() => {
          imageTile.getImage().src = '#';
        })
    }

    if (this.host) {
      // set the function as source.tileLoadFunction
      source.tileLoadFunction = tileLoadFunction;
      this.host.instance.setSource(source);
    }

    if (this.raster) {
      this.raster.sources = [source];
    }
  }

My Idea is to add an @input property in xyz.component so the function can be passed when using the component as this:

  <aol-layer-tile [visible]="true">
    <aol-source-xyz
      url="https://my-server.com/{z}/{x}/{y}.png"
      [tileLoadFunction]="myCustomTileLoadFunction"
      opaque="false"
      crossOrigin=""
    >
    </aol-source-xyz>
  </aol-layer-tile>

and then set to the source object in one this 2 places:

  • xyz.component.ngAfterContentInit()
  • source.component._register()

I also guess, that this functionality could be added to other source components, but do not know which ones should support this kind of functionality.

Any thoughts on this approach?

Thanks

from ngx-openlayers.

packetstracer avatar packetstracer commented on June 26, 2024

Finally I found that the xyz component has a property for that

@Input() tileLoadFunction?: TileLoadFunctionType;

also de geoson component has a property for that

@Input() loader?: ol.FeatureLoader;

but in the second case I needed to acces geojson component openlayers instance property inside de loader function, so I needed to modify slightly de component ngOnInit() method. The resulting code is this for geojson.component.ts

    ngOnInit() {
        // @NOTE loader function is binded to this in order to have access to this.intance when executing the loader function
        this.loader = this.loader.bind(this);
        this.format = new format.GeoJSON();
        this.instance = new source.Vector(this);

        this.host.instance.setSource(this.instance);
    }

I also have to solve some typescript errors due to using the library source code in my Angular 8 aplication.

If you want a pull request or integrate the changes in the library just let me know.

from ngx-openlayers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.