mmomtchev / rlayers

React Component Library for OpenLayers
ISC License
162 stars 32 forks source link

Custom loader for RLayerTile #39

Closed Macklawe closed 2 years ago

Macklawe commented 2 years ago

Hi, I try to load tile from a server, and i recieve error, because my request is without correct token. How can i provide axios or token to request? tile error

mmomtchev commented 2 years ago

What form does your token have? URL parameter or HTTP header?

Macklawe commented 2 years ago

As http header

mmomtchev commented 2 years ago

Check https://mmomtchev.github.io/rlayers/#/addon for how to extend rlayers Create your own layer class extending from RLayerTile and overload only this function:

class AuthLayerTile extends RLayerTile {
  createSource(): void {
      this.source = new XYZ({
          url: this.props.url,
          projection: this.props.projection,
          tileGrid: this.props.tileGrid,
          tileLoadFunction: <your_tile_load_function>
      });
      this.eventSources = [this.ol, this.source];
}

Also check this stackoverflow answer which, even if it is a bit old, still works: https://stackoverflow.com/questions/49095151/how-to-add-a-http-header-to-openlayers4-requests

These days you can use the fetch API which is better.

Macklawe commented 2 years ago

Thanks a lot. It works