Skip to content

How to use local .mbtiles file? #266

Closed

Description

@maarten-22

Is it possible to add a local .mbtiles file onto the map using a <VectorSource>?
I was thinking of doing something like this but I couldn't figure out a way to make it work:

const Overlay: FC<OverlayProps> = ({}) => {
   return (
      <VectorSource id={'overlay-layer'} url={'mbtiles://my-file.mbtiles'}>
         <FillLayer props-... />
      </VectorSource>
   );
}

If this is not supported, what are the options to do such a thing without using a remote server?

Activity

  1. gorbypark commented on Feb 24, 2024

    @gorbypark

    I believe it should be possible, I have done it with the pre-forked version of Mapbox-gl-native. You will have to include the mbtiles file in the bundle somehow. I ended up doing it manually (dragging it into the project in Xcode and android studio) and then used something like react-native-filesystem to get the proper local uri for the file, then fed that into the url prop.

    It's been a while so I don't remember some of the exact details, but the hardest part was getting the mbtiles file included in the bundle. I spent a number of hours trying to coax metro into including it with a require, but couldn't make that work, so had to resort to the manual method I mentioned above.

  2. maarten-22 commented on Feb 26, 2024

    @maarten-22
    Author

    @gorbypark Hm, I don't seem to get it to work using filesystem either. Did you use the .mbtiles file or extract a pbf from it?

  3. mohanedmoh commented on May 20, 2024

    @mohanedmoh
  4. mohanedmoh commented on May 20, 2024

    @mohanedmoh

    it worked with me

  5. tyrauber commented on May 20, 2024

    @tyrauber
    Collaborator

    @mohanedmoh, please post the context of the slack conversation in question. Unfortunately, the slack history isn't publicly accessible, nor is it long lived. Some mock code with explanations of how you made it work, I am sure that would be appreciated by all. Thanks!

  6. mohanedmoh commented on May 20, 2024

    @mohanedmoh

    @tyrauber, @aLemonFox
    Sure, so you will save the MBtiles locally and load it using
    <MapLibreGL.RasterSource id="testSource" tileUrlTemplates={["mbtiles:///" + path]} tileSize={512} minZoomLevel={10} maxZoomLevel={14}> <MapLibreGL.RasterLayer id="testLayer" sourceID="testSource" /> </MapLibreGL.RasterSource>

    Which is normal RasterSource but with tileURLTemplate of (mbtiles:///path_to_your_mbtiles_file.mbtiles)
    make sure to change the tileSize based on your MBtile configuration, mine was 512
    if it's the wrong tileSize the map will start showing some tiles in dark mode and others in light mode

    hope this was helpful

  7. KiwiKilian commented on Jan 4, 2025

    @KiwiKilian
    Collaborator

    Here is an example a put together the other day with expo-asset:

    import { MapView } from "@maplibre/maplibre-react-native";
    import { useAssets } from "expo-asset";
    
    export function App() {
      const [assets] = useAssets([require("../assets/tiles/maplibre.mbtiles")]);
    
      if (!assets?.[0]?.localUri) {
        return null;
      }
    
      return (
        <MapView
          style={{ flex: 1 }}
          mapStyle={{
            name: "MapLibre",
            version: 8,
            sources: {
              mbtiles: {
                url: assets[0].localUri.replace("file://", "mbtiles://"),
                type: "vector",
              },
            },
            layers: [
              {
                id: "background",
                type: "background",
                paint: {
                  "background-color": "#285daa",
                },
              },
              {
                id: "countries-fill",
                type: "fill",
                source: "mbtiles",
                "source-layer": "countries",
                paint: {
                  "fill-color": "#ffffff",
                },
              },
            ],
          }}
        />
      );
    }

    Will look, if we can add this to the examples. It's more difficult, because assets are different on Expo and React Native.

  8. changed the title [-]Rendering from local .mbtiles file?[/-] [+]How to use local `.mbtiles` file?[/+] on Jan 4, 2025
  9. locked and limited conversation to collaborators on Jan 4, 2025
  10. converted this issue into a discussion #591 on Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions