我希望在我的React-Native应用中实现自定义瓦片系统,以便离线使用。我已经在Android模拟器设备中实现了下载和保存自定义瓦片的系统。在地图渲染时,这些文件确实存在。我的瓷砖保存在/data/user/0/com.myapp/files/maps/COUNTRY_CODE/{z}/{x}/{y}.png
,例如对于捷克,我的设备文件夹结构看起来像:
/data/user/0/com.myapp/files/maps/CZ/:
- 4/
- 8/
- 5.png
- 5/
- 17/
- 10.png
- 11.png
- 6/
- 34/
- 21.png
- 22.png
- 35/
- 21.png
- 22.png
- 7/
...
- 8/
...
- 9/
...
- 10/
...
我正在使用 rn-fetch-blob
进行瓦片下载和文件系统管理,而解压缩/保存则由 react-native-zip-archive
处理。
我的地图组件如下:
// this returns /data/user/0/com.myapp/files/maps
const tilesPath = RNFetchBlob.fs.dirs.DocumentDir + "/maps";
// prints to the console that one of the tiles choosed randomly really exists
// maybe I should make more detailed check if every of the tiles exist for testing?
const czMapFound = RNFetchBlob.fs
.exists(tilesPath + "/CZ/4/8/5.png")
.then(res => {
console.log(res);
return res;
});
if (czMapFound) {
// log just to make sure that this branch of statement is executed
console.log(tilesPath + "/CZ/{z}/{x}/{y}.png");
return (
<MapView
style={styles.map}
onRegionChange={mapRegion => this.setState({ mapRegion })}
region={{
// In this example, map is pointing at Czech Republic correctly
latitude: selectedMap ? selectedMap.centerX : 52.519325,
longitude: selectedMap ? selectedMap.centerY : 13.392709,
latitudeDelta: selectedMap ? selectedMap.sizeX : 50,
longitudeDelta: selectedMap ? selectedMap.sizeY : 50
}}
>
<LocalTile
pathTemplate={tilesPath + "/CZ/{z}/{x}/{y}.png"}
size={256}
/>
</MapView>
);
}
我的地图没有显示任何自定义瓦片,只能看到捷克共和国上的默认谷歌地图瓦片。我无法使其工作。
但是,当我使用
UrlTile
并将urlTemplate
设置为http://c.tile.openstreetmap.org/{z}/{x}/{y}.png
时,自定义瓦片可以正常工作。也许我只是将瓦片保存到了错误的设备文件夹中,我的设备无法从那里读取文件?注意:这些瓦片是从OpenStreetMap服务下载并按国家存储在本地的。
rn-fetch-blob
和react-native-zip-archive
处理的想法? - Naveen Kumar H S