0

我正在尝试制作一个依赖离线地图的应用程序。Openstreet 地图成为我的选择,因为 OSMDroid 似乎被大量使用。但是,当我尝试使用离线地图(使用 Mobile Atlas 创建器生成的 MBTiles)时,“MapView”类不会在我的设备上显示任何地图。这是存在于片段中的片段。我也没有看到 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);
    }

任何帮助将不胜感激。提前致谢

4

1 回答 1

2

我有同样的错误,问题是指定的缩放级别不存在并且没有显示地图。尝试将缩放级别更改为最大/最小缩放级别等功能。我的实现也与你的不同。

我按照教程实现了离线 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;
    }
于 2014-03-25T12:19:52.413 回答