Osmdroid API功能介绍3----离线地图加载

Osmdroid API功能介绍3----离线地图加载

http://osmdroid.github.io/osmdroid/

一,文件加载

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();

效果
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值