How to use local .mbtiles
file?
#591
-
Is it possible to add a local .mbtiles file onto the map using a 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? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
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. |
Beta Was this translation helpful? Give feedback.
-
@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? |
Beta Was this translation helpful? Give feedback.
-
Hi @aLemonFox check this out |
Beta Was this translation helpful? Give feedback.
-
it worked with me |
Beta Was this translation helpful? Give feedback.
-
@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! |
Beta Was this translation helpful? Give feedback.
-
@tyrauber, @aLemonFox Which is normal RasterSource but with tileURLTemplate of (mbtiles:///path_to_your_mbtiles_file.mbtiles) hope this was helpful |
Beta Was this translation helpful? Give feedback.
-
Here is an example a put together the other day with You also need to add this to your Metro Config: config.resolver.assetExts.push("mbtiles"); 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. |
Beta Was this translation helpful? Give feedback.
Here is an example a put together the other day with
expo-asset
, make sure to install it first.You also need to add this to your Metro Config: