Osmdroid API功能介绍3----离线地图加载
一,文件加载
Osmdroid支持.zip、.sqlite、.gemf、.mbtiles文件
使用方式
//ok found a file we support and have a driver for the format, for this demo, we'll just use the first one
//create the offline tile provider, it will only do offline file archives
OfflineTileProvider tileProvider = new OfflineTileProvider(new SimpleRegisterReceiver(getActivity()),
new File[]{filePath});
//tell osmdroid to use that provider instead of the default rig which is (asserts, cache, files/archives, online
mMapView.setTileProvider(tileProvider);
//this bit enables us to find out what tiles sources are available. note, that this action may take some time to run
//and should be ran asynchronously. we've put it inline for simplicity
String source = "";
IArchiveFile[] archives = tileProvider.getArchives();
if (archives.length > 0) {
//cheating a bit here, get the first archive file and ask for the tile sources names it contains
Set<String> tileSources = archives[0].getTileSources();
//presumably, this would be a great place to tell your users which tiles sources are available
if (!tileSources.isEmpty()) {
//ok good, we found at least one tile source, create a basic file based tile source using that name
//and set it. If we don't set it, osmdroid will attempt to use the default source, which is "MAPNIK",
//which probably won't match your offline tile source, unless it's MAPNIK
source = tileSources.iterator().next();
this.mMapView.setTileSource(FileBasedTileSource.getSource(source));
} else {
this.mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
}
} else {
this.mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
}
Snackbar.make(getView(), "Using " + list[i].getAbsolutePath() + " " + source, Snackbar.LENGTH_SHORT).show();
this.mMapView.invalidate();
效果图:
二,assets加载
使用方式,在assets文件中放入离线数据
代码:
public static final OnlineTileSourceBase MAPNIK = new XYTileSource("Mapnik",
0, 19, 256, ".png", new String[] {
"https://a.tile.openstreetmap.org/",
"https://b.tile.openstreetmap.org/",
"https://c.tile.openstreetmap.org/" },"© OpenStreetMap contributors");
MapTileAssetsProvider prov = new MapTileAssetsProvider(new SimpleRegisterReceiver(getContext() ),
getActivity().getAssets());
this.mMapView.setTileProvider(new MapTileProviderArray(TileSourceFactory.MAPNIK,
new SimpleRegisterReceiver(getContext()),
new MapTileModuleProviderBase[]{ prov }));
三,Mapsforge文件加载
DEMO文件的下载地址:http://ftp-stud.hs-esslingen.de/pub/Mirrors/download.mapsforge.org/maps/
使用这种方式需要在Assets文件夹下配置风格文件
代码:
//this creates the forge provider and tile sources
//protip: when changing themes, you should also change the tile source name to prevent cached tiles
//null is ok here, uses the default rendering theme if it's not set
XmlRenderTheme theme = null;
try {
theme = new AssetsRenderTheme(getContext().getApplicationContext(), "renderthemes/", "rendertheme-v4.xml");
} catch (Exception ex) {
ex.printStackTrace();
}
fromFiles = MapsForgeTileSource.createFromFiles(new File[]{filePath}, theme, "rendertheme-v4");
forge = new MapsForgeTileProvider(
new SimpleRegisterReceiver(getContext()),
fromFiles, null);
mMapView.setTileProvider(forge);
//now for a magic trick
//since we have no idea what will be on the
//user's device and what geographic area it is, this will attempt to center the map
//on whatever the map data provides
mMapView.getController().setZoom(fromFiles.getMinimumZoomLevel());
mMapView.zoomToBoundingBox(fromFiles.getBoundsOsmdroid(), true);
四,文件夹加载
文件结构
代码使用
mMapView.setUseDataConnection(false);
final ITileSource tileSource = new XYTileSource(
"Abstract", 0, 29, 256, ".png", new String[]{"http://localhost/"}, "abstract data");
mMapView.setUseDataConnection(false);
final MapTileAssetsProvider assetsProvider = new MapTileAssetsProvider(new SimpleRegisterReceiver(getContext()), getActivity().getAssets(), tileSource);
final MapTileApproximater approximationProvider = new MapTileApproximater();
approximationProvider.addProvider(assetsProvider);
final MapTileProviderArray array = new MapTileProviderArray(
tileSource, new SimpleRegisterReceiver(getContext()),
new MapTileModuleProviderBase[]{assetsProvider, approximationProvider});
mMapView.setTileProvider(array);
mMapView.getController().setZoom(29.);
// cf. https://fr.wikipedia.org/wiki/Point_z%C3%A9ro_des_routes_de_France
// In English: starting point of all French roads
mMapView.setExpectedCenter(new GeoPoint(48.85340215825712, 2.348784611094743));
mMapView.invalidate();
效果