Repository navigation
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? |
Replies: 8 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. |
|
@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? |
|
Hi @aLemonFox check this out |
|
it worked with me |
|
@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! |
|
@tyrauber, @aLemonFox Which is normal RasterSource but with tileURLTemplate of (mbtiles:///path_to_your_mbtiles_file.mbtiles) hope this was helpful |
|
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. |
|
seems like there is sadly a size limitation to this solution. I'd like to include the entirety of the Netherlands offline and the mbtiles file I downloaded using planetiler is about 670 MB. The limit of JS strings is allegedly 512 MB. Of course, it's not a good idea to include such a large file in the app's assets, but I did not succeed with expo document picker either - the file was copied into the cache dir just fine and returned a valid URI, but maybe this library's way of reading the file was silently failing? I only saw some contours of the individual provinces of the Netherlands, but that was it, nothing more loaded upon zooming. The exact same mbtiles file worked just fine when served via TileServer GL. I also tried a version converted to pmtiles and with an adjusted protocol in the URI, same issue. import React, { useState } from "react";
import { StyleSheet, View, Button, Text } from "react-native";
import * as DocumentPicker from "expo-document-picker";
import {
Map,
Camera,
VectorSource,
Layer,
} from "@maplibre/maplibre-react-native";
export default function App() {
const [mapUri, setMapUri] = useState(null);
const pickMapFile = async () => {
const result = await DocumentPicker.getDocumentAsync({
type: "*/*",
copyToCacheDirectory: true,
});
if (!result.canceled && result.assets && result.assets.length > 0) {
const rawUri = result.assets[0].uri;
console.log("Picked file URI:", rawUri);
const cleanPath = rawUri.replace("file://", "mbtiles://");
setMapUri(cleanPath);
}
};
return (
<View style={styles.container}>
{!mapUri ? (
<View>
<Text>
Select your local netherlands.pmtiles file
</Text>
<Button title="Pick tiles file" onPress={pickMapFile} />
</View>
) : (
<Map
style={{ flex: 1 }}
mapStyle={{
version: 8,
name: "Basic",
metadata: {
"mapbox:autocomposite": false,
"mapbox:type": "template",
"maputnik:renderer": "mbgljs",
"openmaptiles:version": "3.x",
"openmaptiles:mapbox:owner": "openmaptiles",
"openmaptiles:mapbox:source:url":
"mapbox://openmaptiles.4qljc88t",
},
sources: {
openmaptiles: {
type: "vector",
url: mapUri,
},
},
sprite:
"https://openmaptiles.github.io/maptiler-basic-gl-style/sprite",
glyphs:
"https://api.maptiler.com/fonts/{fontstack}/{range}.pbf?key={key}",
layers: [
{
id: "background",
type: "background",
paint: { "background-color": "hsl(47, 26%, 88%)" },
},
// more layer styles which are not important
}}
></Map>
)}
</View>
);
} |
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: