放大/缩小一些不渲染的图块 - Osmdroid离线地图

时间:2016-02-14 15:10:38

标签: android osmdroid android-assets

我正在从asstets加载tile,这些是我的代码,它初始化map:

    mapView = (MapView)findViewById(R.id.offline_map_view); 
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(false);
    mapView.setMaxZoomLevel(macroZoomLevel);
    mapView.setMinZoomLevel(macroZoomLevel);
    mapView.getController().setZoom(macroZoomLevel); //set initial zoom-level, depends on your need
    mapView.setTileSource(new XYTileSource("MapQuest", 0, 18, 256, ".png",new String[] {"file:///android_asset/try/"} ));
    mapView.setUseDataConnection(false); //keeps the mapView from loading online tiles usi1ng network connection.
    mapView.getController().setCenter(new GeoPoint(54.370958, 18.589210));
    giveMarkersForActualLevel();

一切正常,直到我尝试放大,有部分地图无法正确渲染。然后我缩小,然后开始正确渲染的区域现在有一些灰色的瓷砖。

首先,我使用osmdroid 4.3,以这种方式添加:

 compile 'org.osmdroid:osmdroid-android:4.3'

然后我尝试使用最新版本的osmdroid,通过website compile 'org.osmdroid:osmdroid-android:5.1@aar'

中描述的方法导入它

然后我从源代码阅读here tu build,以便下载最新的源代码,通过gradle构建并添加aar文件osmdroid-android-release.aar。 这也不能解决我的问题。

放大和缩小后,我正在移除标记并添加其他标记,因此我尝试以这种方式刷新地图。

((View)mapView.getParent()).invalidate();
mapView.invalidate();
mapView.postInvalidate();

Marker startMarker = new Marker(mapView);
startMarker.setPosition(new GeoPoint(54.337385, 18.662132));
setPropertiesForTopMarker(startMarker);
mapView.getOverlays().add(startMarker);

Marker startMarker1 = new Marker(mapView);
startMarker1.setPosition(new GeoPoint(54.332781, 18.587932));
setPropertiesForTopMarker(startMarker1);
mapView.getOverlays().add(startMarker1);

mapView.invalidate();
((View)mapView.getParent()).invalidate();
mapView.invalidate();
mapView.postInvalidate();

但它也不起作用。

您有什么想法,如何解决这个问题?

编辑:

我尝试从源代码构建osmdroid来改变@spy提到的这个值。我的logcat调试看起来不错。 Here是日志。我不能把它贴在这里,因为行太多了。

我尝试以这种方式添加磁贴提供程序:

 final IRegisterReceiver registerReceiver = new SimpleRegisterReceiver(getApplicationContext());

        final ITileSource tileSource = new XYTileSource("MapQuest", 12, 14, 256, ".png",new String[] {"file:///android_asset/MapQuest/"} );

        final MapTileFilesystemProvider fileSystemProvider = new MapTileFilesystemProvider(
                registerReceiver, tileSource);

        final MapTileProviderArray tileProviderArray = new MapTileProviderArray(
                tileSource, registerReceiver, new MapTileModuleProviderBase[] {
                fileSystemProvider});

        mapView = new MapView(this, new DefaultResourceProxyImpl(this), tileProviderArray); 

或者这样

 final IRegisterReceiver registerReceiver = new SimpleRegisterReceiver(getApplicationContext());

            final ITileSource tileSource = new XYTileSource("MapQuest", 12, 14, 256, ".png",new String[] {"file:///android_asset/MapQuest/"} );

            final MapTileFilesystemProvider fileSystemProvider = new MapTileFilesystemProvider(
                    registerReceiver, tileSource);

            final MapTileProviderArray tileProviderArray = new MapTileProviderArray(
                    tileSource, registerReceiver, new MapTileModuleProviderBase[] {
                    fileSystemProvider});

 TilesOverlay tilesOverlay =
                new TilesOverlay(tileProviderArray, getApplicationContext());

        mapView.getOverlays().add(tilesOverlay);

但两种方法都没有向我显示任何地图。 Source此代码。

2 个答案:

答案 0 :(得分:1)

值得一试的东西......

如果您只想加载资源,请使用仅包含断言加载器的自定义磁贴提供程序数组。在上面的配置中,它仍然是在线机制(okhttp或设备上的任何东西)尝试从file:///下载文件,这可能与文件网址有点奇怪。

还可以尝试通过OpenStreetMapTileProviderConstants.DEBUGMODE和DEBUG_TILE_PROVIDERS打开磁贴调试。它可能揭示根本原因

答案 1 :(得分:0)

好的,最后我使用Providers解决了我的问题。

我对this回答的代码进行了一些修改,所以现在它看起来像这样。

        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(false);

        mapView.setMaxZoomLevel(macroZoomLevel);
        mapView.setMinZoomLevel(macroZoomLevel);
        mapView.getController().setZoom(macroZoomLevel); //set initial zoom-level, depends on your need

        mapView.setUseDataConnection(false); //keeps the mapView from loading online tiles usi1ng network connection.
        mapView.getController().setCenter(new GeoPoint(54.370958, 18.589210));

        // save zip to sd
        AssetManager assetManager = this.getAssets();
        InputStream is;
        String fileName = "map.zip";    // the zip file lies in assets root
        String path = this.getExternalFilesDir(null) + File.separator + fileName; // the path I save SD to

        File tileFile = new File(path);
        if(!tileFile.exists()) {
            try {
                is = assetManager.open(fileName);

                FileOutputStream fo = new FileOutputStream(path);

                byte[] b = new byte[1024];
                int length;
                while((length = is.read(b)) != -1) {
                    fo.write(b, 0, length);
                }

                fo.flush();
                fo.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        IArchiveFile[] archives = new IArchiveFile[1];
        archives[0] = ArchiveFileFactory.getArchiveFile(tileFile);

        // Simple implementation that extends BitmapTileSourceBase and nothing else
        CustomTileSource customTiles = new CustomTileSource("MapQuest", null, 10, 14, 256, ".png");  // Maverik is the name of the folder inside the zip (so zip is map.zip and inside it is a folder called Maverik)

        MapTileModuleProviderBase[] providers = new MapTileModuleProviderBase[1];
        providers[0] = new MapTileFileArchiveProvider(new SimpleRegisterReceiver(this.getApplicationContext()), customTiles, archives);    // this one is for local tiles (zip etc.)



        MapTileProviderArray tileProvider = new MapTileProviderArray(customTiles,
                new SimpleRegisterReceiver(this.getApplicationContext()), providers);
        tilesOverlay = new TilesOverlay(tileProvider, this.getApplicationContext());
        tilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);  // this makes sure that the invisble tiles of local tiles are transparent so we can see tiles from web, transparent have a minor performance decline.

        mapView.getOverlays().add(tilesOverlay);

CustomTileSource看起来像这样:

public class CustomTileSource extends BitmapTileSourceBase {

    public CustomTileSource(String aName, ResourceProxy.string aResourceId, int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels, String aImageFilenameEnding) {
        super(aName, aResourceId, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding);
    }
}

所以这只是BitmapTileSourceBase的子类,没有任何其他方法。

我有资源zip类型文件,其名称是map.zip,此目录的结构如下所示:

map.zip/MapQuest/[lvlOfZoom]/[x]/[y.png]

现在它工作正常,我只是在点击我的标记时进行缩放,用户通过手势无法做到这一点。

如果您在实施我描述的方法时遇到一些问题或者您不清楚某些问题,我会尽力帮助您做出评论。