android osmdroid 加载常用离线地图格式(开源的在线地图)
2017-01-04 22:08
561 查看
osmdroid是(几乎)完整/免费更换为Android MapView(v1 API)类。它还包括一个模块化的瓷砖供应商系统支持众多的在线和离线瓷砖来源和覆盖支持内置覆盖绘制图标,跟踪位置和形状。如果想在你项目中使用osmdroid,绝对是灵活度最高,不用申请百度,高德等的key。还有一堆规则要遵循。osmdroid可以支持多图层叠加,绘制自已的图层。离线加载常用地图格式zip,sqlite,.mbtiles,gemf。也可以在线加载地图等。
github官网:https://github.com/osmdroid/osmdroid
osmdroid api解释:http://osmdroid.github.io/osmdroid/
维基解释:https://github.com/osmdroid/osmdroid/wiki
这篇博客代码的下载地址:http://download.csdn.net/detail/qq_16064871/9728127
网格形式:还没加载出来
加载sd卡下面一个离线地图
import java.io.File;
import java.util.Set;
import org.osmdroid.tileprovider.IRegisterReceiver;
import org.osmdroid.tileprovider.modules.ArchiveFileFactory;
import org.osmdroid.tileprovider.modules.IArchiveFile;
import org.osmdroid.tileprovider.modules.OfflineTileProvider;
import org.osmdroid.tileprovider.tilesource.FileBasedTileSource;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.util.SimpleRegisterReceiver;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView)findViewById(R.id.MapView);
initMapOverlays(mapView);
}
public void initMapOverlays(MapView mapView) {
if (mapView.getOverlays().size()<=0) {
mapViewOtherData(mapView);
// mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setDrawingCacheEnabled(true);
mapView.setMaxZoomLevel(20);
mapView.setMinZoomLevel(6);
mapView.getController().setZoom(12);
mapView.getController().setCenter(new GeoPoint(23.12655, 113.366575));
mapView.setUseDataConnection(true);
mapView.setMultiTouchControls(true);// 触控放大缩小
mapView.getOverlayManager().getTilesOverlay().setEnabled(true);
//自己图层
// mapView.getOverlays().add();
// ".zip"".sqlite"".mbtiles"".gemf"
}
}
public void mapViewOtherData(MapView mapView){
String strFilepath = Environment.getExternalStorageDirectory().getPath() + "/test.mbtiles";
File exitFile = new File(strFilepath);
String fileName = "test.mbtiles";
if (!exitFile.exists()) {
mapView.setTileSource(TileSourceFactory.MAPNIK);
}else {
fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
if (fileName.length() == 0)
return;
if (ArchiveFileFactory.isFileExtensionRegistered(fileName)) {
try {
OfflineTileProvider tileProvider = new OfflineTileProvider((IRegisterReceiver) new SimpleRegisterReceiver(this), new File[] { exitFile });
mapView.setTileProvider(tileProvider);
String source = "";
IArchiveFile[] archives = tileProvider.getArchives();
if (archives.length > 0) {
Set<String> tileSources = archives[0].getTileSources();
if (!tileSources.isEmpty()) {
source = tileSources.iterator().next();
mapView.setTileSource(FileBasedTileSource.getSource(source));
} else {
mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
}
} else
mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
Toast.makeText(this,"Using " + exitFile.getAbsolutePath() + " "+ source, Toast.LENGTH_LONG).show();
mapView.invalidate();
return;
} catch (Exception ex) {
ex.printStackTrace();
}
Toast.makeText(this, " did not have any files I can open! Try using MOBAC", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, " dir not found!", Toast.LENGTH_LONG).show();
}
}
}
}
离线地图的格式支持:".zip"".sqlite"".mbtiles"".gemf"
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4,在线地图数据源
可以加载不同的在线数据源
mapView.setTileSource(TileSourceFactory.MAPNIK);
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<org.osmdroid.views.MapView
android:id="@+id/MapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2.00" />
</RelativeLayout>
github官网:https://github.com/osmdroid/osmdroid
osmdroid api解释:http://osmdroid.github.io/osmdroid/
维基解释:https://github.com/osmdroid/osmdroid/wiki
这篇博客代码的下载地址:http://download.csdn.net/detail/qq_16064871/9728127
1,图片展示
在线地图:网格形式:还没加载出来
加载sd卡下面一个离线地图
2,部分离线地图参考代码
package com.example.osmdroiddemo;import java.io.File;
import java.util.Set;
import org.osmdroid.tileprovider.IRegisterReceiver;
import org.osmdroid.tileprovider.modules.ArchiveFileFactory;
import org.osmdroid.tileprovider.modules.IArchiveFile;
import org.osmdroid.tileprovider.modules.OfflineTileProvider;
import org.osmdroid.tileprovider.tilesource.FileBasedTileSource;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.util.SimpleRegisterReceiver;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView)findViewById(R.id.MapView);
initMapOverlays(mapView);
}
public void initMapOverlays(MapView mapView) {
if (mapView.getOverlays().size()<=0) {
mapViewOtherData(mapView);
// mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setDrawingCacheEnabled(true);
mapView.setMaxZoomLevel(20);
mapView.setMinZoomLevel(6);
mapView.getController().setZoom(12);
mapView.getController().setCenter(new GeoPoint(23.12655, 113.366575));
mapView.setUseDataConnection(true);
mapView.setMultiTouchControls(true);// 触控放大缩小
mapView.getOverlayManager().getTilesOverlay().setEnabled(true);
//自己图层
// mapView.getOverlays().add();
// ".zip"".sqlite"".mbtiles"".gemf"
}
}
public void mapViewOtherData(MapView mapView){
String strFilepath = Environment.getExternalStorageDirectory().getPath() + "/test.mbtiles";
File exitFile = new File(strFilepath);
String fileName = "test.mbtiles";
if (!exitFile.exists()) {
mapView.setTileSource(TileSourceFactory.MAPNIK);
}else {
fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
if (fileName.length() == 0)
return;
if (ArchiveFileFactory.isFileExtensionRegistered(fileName)) {
try {
OfflineTileProvider tileProvider = new OfflineTileProvider((IRegisterReceiver) new SimpleRegisterReceiver(this), new File[] { exitFile });
mapView.setTileProvider(tileProvider);
String source = "";
IArchiveFile[] archives = tileProvider.getArchives();
if (archives.length > 0) {
Set<String> tileSources = archives[0].getTileSources();
if (!tileSources.isEmpty()) {
source = tileSources.iterator().next();
mapView.setTileSource(FileBasedTileSource.getSource(source));
} else {
mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
}
} else
mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
Toast.makeText(this,"Using " + exitFile.getAbsolutePath() + " "+ source, Toast.LENGTH_LONG).show();
mapView.invalidate();
return;
} catch (Exception ex) {
ex.printStackTrace();
}
Toast.makeText(this, " did not have any files I can open! Try using MOBAC", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, " dir not found!", Toast.LENGTH_LONG).show();
}
}
}
}
离线地图的格式支持:".zip"".sqlite"".mbtiles"".gemf"
3,权限
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4,在线地图数据源
可以加载不同的在线数据源
mapView.setTileSource(TileSourceFactory.MAPNIK);
5,xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<org.osmdroid.views.MapView
android:id="@+id/MapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2.00" />
</RelativeLayout>
相关文章推荐
- androidstudio中外部数据库的导入
- 浅析总结 AS 中 Gradle 配置运行
- 基于栈和基于寄存器的虚拟机
- Android 起始页广告
- Android 下不同格式字体的实现方法
- Android源码分析
- Android中的JSON详细总结
- 知识记录一:ViewPager+Fragment实现tab
- android颜色表 以及颜色对应的xml配置值
- Android实现播放音频
- Android的Proxy-Stub设计模式
- android开发艺术探索 第一章笔记
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- Android的JNI技术
- Android 4.4以上保存图片并通知图库
- Android硬件编码
- android开发技巧收集
- 我的 Android 开发实战经验总结
- 用Kotlin实现Android定制视图(KAD 06)
- ToolBar讲解