前面一篇摸索完简单的GIS后,这二天又摸索了一下本地离线缓存底图和加载离线底图的效果;直接看最终的效果:
1、把瓦片缓存到磁盘和加载:
// 首先尝试从缓存中获取瓦片
byte[] tileData = await GetTileFromCacheAsync(tileInfo.Index);
if (tileData == null)
{
// 如果缓存中没有,从服务器获取瓦片
tileData = await GetTileAsync(tileInfo);
// 将获取的瓦片保存到磁盘
await SaveTileToCacheAsync(tileInfo.Index, tileData);
}
return tileData;
2、把瓦片缓存到SQLite数据库:
public async Task InsertTileAsync(int x, int y, int zoom, byte[] tileData)
{
int tmsY = (1 << zoom) - 1 - y; // TMS Y坐标转换
var existingTile = await db.Queryable < Tile > ().Where(t => t.zoom_level == zoom && t.tile_column == x && t.tile_row == tmsY).SingleAsync();
if(existingTile != null)
{
// 瓦片已存在,更新它
existingTile.tile_data = tileData;
await db.Updateable(existingTile).ExecuteCommandAsync();
}
else
{
// 瓦片不存在,插入新的瓦片
var tile = new Tile
{
zoom_level = zoom,
tile_column = x,
tile_row = tmsY,
tile_data = tileData
};
await db.Insertable(tile).ExecuteCommandAsync();
}
}
3、从SQLite数据库读取离线瓦片数据:
public async Task<byte[]> GetTileAsync(int x, int y, int zoom)
{
int tmsY = (1 << zoom) - 1 - y; // TMS Y坐标转换
var tile = await db.Queryable<Tile>()
.Where(t => t.zoom_level == zoom && t.tile_column == x && t.tile_row == tmsY)
.SingleAsync();
return tile?.tile_data;
}
4、本地磁盘缓存的瓦片效果:
5、SQLite缓存的瓦片效果:最终简单的效果先这样吧;以后有时间的话,可以再去摸索一下更复杂的效果;编程不息、Bug不止、无Bug、无生活;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下!