Osmdroid只查看网格

时间:2018-04-06 10:10:54

标签: android openstreetmap osmdroid

我正在使用Osmdroid来加载离线地图。我是从article

引用的

当我运行我的项目时,它只显示一个网格。我不知道为什么,我尝试了一些方法,但没有任何改变。 enter image description here

我的mainActivity剪辑代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mapView = (MapView) findViewById(R.id.mapView);

    resProxy = new DefaultResourceProxyImpl(getApplicationContext());
    String packageDir = "/Osmdroid";
    String path = Environment.getExternalStorageDirectory() + packageDir;
    File dbFile = new File(path, "map.mbtiles");
    if (dbFile.exists()) {
        provider = new MBTileProvider(this, dbFile);
        mapView.setUseDataConnection(true);
        mapView = new MapView(getApplicationContext(), 
        provider.getTileSource().getTileSizePixels(), resProxy, provider);
    }
    mapView.setMultiTouchControls(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(provider.getMinimumZoomLevel());
    GeoPoint gPt = new GeoPoint(10.813472, 106.666105);
    mapController.animateTo(gPt);
}

请,如何解决这个问题?

分辨

我在互联网上进行了研究,找到了解决问题的解决方案。来自@osedok的answer。我不知道为什么它只是按来源使用mbtiles文件,否则就失败了。

1 个答案:

答案 0 :(得分:0)

您从布局文件中重新实例化mapView然后更改它..尝试将创建的实例设置为您的内容视图

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    resProxy = new DefaultResourceProxyImpl(getApplicationContext());
    String packageDir = "/Osmdroid";
    String path = Environment.getExternalStorageDirectory() + packageDir;
    File dbFile = new File(path, "map.mbtiles");
    if (dbFile.exists()) {
        provider = new MBTileProvider(this, dbFile);
        mapView.setUseDataConnection(true);
        mapView = new MapView(getApplicationContext(), 
                              provider.getTileSource().getTileSizePixels(),
                              resProxy, 
                              provider);
    }
    mapView.setMultiTouchControls(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(provider.getMinimumZoomLevel());
    GeoPoint gPt = new GeoPoint(10.813472, 106.666105);
    mapController.animateTo(gPt);

    // here is the important part
    // Set the MapView as the root View for this Activity; done!
    setContentView(mapView);
}

P.S:确保dbFile存在