React-Leaflet render local tiles in development environment

Viewed 606

I need help to solve a problem that I have by using Webpack with React-leaflet, when I want to load the tiles of my map I get this error message for each tiles

GET http://localhost:9080/home/drigtime/Documents/electron-webpack-quick-start/static/tiles/amakna/3/11/6.jpg 404 (Not Found)

Here is my component code :

class MapComponent extends Component {
  state = {
    crs: L.CRS.Simple,
    hightLight: [],
    lat: -250,
    lng: 425,
    markers: [],
    maxZoom: 4,
    minZoom: 0,
    zoom: 3
  };
  render() {
    const { lat, lng, zoom, crs, maxZoom, minZoom } = this.state;
    const position = [lat, lng];

    return (
      <Map
        center={position}
        zoom={zoom}
        maxZoom={maxZoom}
        minZoom={minZoom}
        crs={crs}
      >
        <MapCoord />
        <MapArea />
        <HightLight />
        <TileLayer url={join(__static, "./tiles/amakna/{z}/{x}/{y}.jpg")} />
      </Map>
    );
  }
}

Am I doing something wrong ? How can I load my map tiles in the development environment ?

Here is the repo of my project https://github.com/Drigtime/owltouch/tree/beta

1 Answers

Ok I fixed my probleme by adding :

include : path.join(__dirname, "static")

to the webpack configuration file for the "url-loader" module.

{ test: /\.(png|jpg)$/, loader: "url-loader", include: [path.join(__dirname, 'static')] }

After that I got a new error message saying :

Not allowed to load local resource: file:///home/drigtime/Document/electron-webpack-quick-start/static/tiles/amakna/3/12/6.jpg"

So I disabled the websecurity

const window = new BrowserWindow({
  webPreferences: {
    webSecurity: false
  }
})

And it worked ! My map is finaly rendering ! :)

Related