Skip to content

[Feature] Handle geojson file as provider in <Map /> #210

@OhMaley

Description

@OhMaley

Hello,

For an application I need to handle both online and offline mode. When in offline mode I wish to render the map using data stored in a json file that follows the Geojson format:

offlineMap.json:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            -61.686668,
                            17.024441000000138
                        ],
                        [
                            -61.88722200000001,
                            17.033054000000106
                        ],
                        [
                            -61.794448999999986,
                            17.163330000000087
                        ],
                        [
                            -61.686668,
                            17.024441000000138
                        ]
                    ]
                ]
            },
        },
        {...}
    ]
}

I was looking at a way to render only the necessary tiles, may be using the tileComponent prop of <Map /> combined with the geojson-vt library.

I manage to extract the coordinates of the tile I want to render from the tile argument of the tileComponent function and then get my Tile object using the geojson library. But now I am unsure how use this object to return a dom element from the tileComponent function.

Here what I have done so far (only the necessary code in included):

render() {
    const geojsonTiles = geojsonvt(offlineGeojsonData); // offlineGeojsonData is imported from offlineMap.json
    const customTileComponent = ({ tile, tileLoaded }) => {
            const coords = tile.key.split('-');
            const x = parseInt(coords[0]);
            const y = parseInt(coords[1]);
            const z = parseInt(coords[2]);
            const tileIndex = geojsonTiles.getTile(z, x, y);
            return (
                <GeoJson data={tileIndex} /> // <-- This is where I struggle
            );
        }
    render (
        <Map defaultCenter={[50.879, 4.6997]} defaultZoom={11} height={600} tileComponent={customTileComponent} />
    );
}

Is there a way to achieve this using this library without the need to code low rendering layer like <svg> or <g> elements?

Or does anyone already faced this issue?

Any insight is welcome 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions