我想在我的react原生应用程序中实现离线模式使用的自定义瓷砖系统。我已经实现了下载自定义磁贴并将其保存到我的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>
);
}我的地图没有显示任何自定义的瓦片,我所能看到的是捷克各地默认的google地图瓦片。我不能让它工作。
但是,当我使用UrlTile并将urlTemplate设置为http://c.tile.openstreetmap.org/{z}/{x}/{y}.png时,自定义磁贴可以正常工作。也许我只是将磁贴保存到错误的设备文件夹中,而我的设备无法从那里读取文件?
注意:这些磁贴从openStreetMap服务下载,并按国家/地区存储在本地。
发布于 2018-12-06 21:21:56
实际上,这是我自己想出来的。因此,如果将来其他任何人会遇到此类问题,我建议您使用UrlTile而不是LocalTile,即使您正在尝试加载自定义磁贴。您的代码将如下所示:
<MapView
style={styles.map}
// keep the reference for region inside your local state..
onRegionChangeComplete={mapRegion => this.setState({ mapRegion })}
region={this.state.mapRegion}
>
<UrlTile urlTemplate={urlTemplate} zIndex={1} />
</MapView>要使其正常工作,请确保切片路径以file://前缀开头,例如const urlTemplate = "file://"+"RNFetchBlob.fs.dirs.DocumentDir"+"/maps/CZ/{z}/{x}/{y}.png"
发布于 2019-04-19 08:40:15
您正在正确地构建路径,但是在LocalTile的第二个属性上有一个错误,您正在编写size,而实际上它应该是tileSize。
<LocalTile
pathTemplate="/path/to/locally/saved/tiles/{z}/{x}/{y}.png"
tileSize={256}
zIndex={-1}
/>在react-native-maps的official repo上可以看到
无论如何,我都同意UrlTile更加直截了当。
https://stackoverflow.com/questions/53650517
复制相似问题