我正在尝试创建一个依赖于离线地图的应用程序。 Openstreet地图成了我的选择,因为OSMDroid似乎被大量使用。但是,当我使用离线地图(使用Mobile Atlas创建者生成的MBTiles)尝试使用" MapView" class不会在我的设备上显示任何地图。这是片段中的片段。我没有看到logcat抛出任何错误
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
String [] url = {"http://example.org/"};
XYTileSource MBTILESRENDER = new XYTileSource("mbtiles",
ResourceProxy.string.offline_mode,
1, 20, 256,
".png",
url);
DefaultResourceProxyImpl mResourceProxy = new DefaultResourceProxyImpl(getActivity());
SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(getActivity());
File f = new File(Environment.getExternalStorageDirectory(), "osmdroid/srMbTiles.mbtiles");
Log.e("File ", f.toString() + " PATH:" + f.getAbsolutePath());
if ( f.exists() ) {
Log.d("DEBUG", " #### FIle Exists");
}
IArchiveFile[] files = { MBTilesFileArchive.getDatabaseFileArchive(f) };
MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(simpleReceiver, MBTILESRENDER, files);
MapTileProviderArray mProvider = new MapTileProviderArray(MBTILESRENDER, null, new MapTileModuleProviderBase[] { moduleProvider });
myOpenMapView = new MapView(getActivity(),
256,mResourceProxy, mProvider);
myOpenMapView.setBuiltInZoomControls(true);
myOpenMapView.setTileSource(MBTILESRENDER);
IMapController controller = (IMapController) myOpenMapView.getController();
controller.setZoom(12);
getActivity().setContentView(myOpenMapView);
}
非常感谢任何帮助。提前致谢
答案 0 :(得分:2)
我遇到了同样的错误,问题是指定的缩放级别不存在且没有显示地图。尝试将缩放级别更改为最大/最小缩放级别等功能。我的实施也与你的不同。
我按照this教程实施了离线MBTiles地图,在主要活动中我用这种方式修改了它:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the mapView with an MBTileProvider
DefaultResourceProxyImpl resProxy;
resProxy = new DefaultResourceProxyImpl(this.getApplicationContext());
String path = Environment.getExternalStorageDirectory() + "/LocalPath/";
File file = new File(path, "file.mbtiles");
MBTileProvider provider = new MBTileProvider(this, file);
mapView = new MapView(this,
resProxy,
provider);
mapView.setScrollableAreaLimit(provider.getBoundingBox());
mapView.zoomToBoundingBox(provider.getBoundingBox());
// Set the MapView as the root View for this Activity; done!
setContentView(mapView);
mapView.setBuiltInZoomControls(true);
MapController controller = (MapController) mapView.getController();
//desired zoom level (here we use maximum)
controller.setZoom(provider.getTileSource().getMaximumZoomLevel());
}
此外,您正在使用具有已定义边界的地图,因此需要告诉地图附加到它们。这是通过以下指令完成的:
mapView.setScrollableAreaLimit(provider.getBoundingBox());
我通过这种方式修改MBTileSource.java来做到这一点:
protected MBTileSource(int minZoom,
int maxZoom,
BoundingBoxE6 box,
int tileSizePixels,
File file,
SQLiteDatabase db) {
super("MBTiles", resourceId, minZoom, maxZoom, tileSizePixels, ".png");
this.bbox = box;
archive = file;
database = db;
}
public static MBTileSource createFromFile(File file) {
//original stuff
//added
// Get the MBTiles limits
Cursor box = db.rawQuery("SELECT value FROM metadata WHERE name='bounds' LIMIT 0,1",
new String[] {});
BoundingBoxE6 bbox = null;
if (box.moveToFirst())
{
String rawBox = box.getString(box.getColumnIndex("value"));
StringTokenizer tokens=new StringTokenizer(rawBox, ",");
double fourth = Double.parseDouble((String) tokens.nextElement());
double third = Double.parseDouble((String) tokens.nextElement());
double second = Double.parseDouble((String) tokens.nextElement());
double first = Double.parseDouble((String) tokens.nextElement());
bbox = new BoundingBoxE6(first,second,third,fourth);
}
box.close();
return new MBTileSource(minZoomLevel, maxZoomLevel, bbox, tileSize, file, db);
}
public BoundingBoxE6 getBoundingBox()
{
return this.bbox;
}