Detect if the OSM Mapview is still loading or not in android

≯℡__Kan透↙ 提交于 2019-12-05 08:15:58

Take a look at TilesOverlay and the TileLooper implementation. This is what we use to load and then draw each tile on the screen. In handleTile(...) we attempt to get the tile from the tile provider mTileProvider.getMapTile(pTile). If that returns a Drawable then the tile is loaded, if not it will return null.

A simple way to do this is to extend TilesOverlay, override drawTiles(...) and call your own TileLooper before calling super.drawTiles(...) that will check to see if all the tiles that get passed to handleTile(...) are not null. To use your TilesOverlay call mMapView.getOverlayManager().setTilesOverlay(myTilesOverlay).

I created a class named "MyTileOverlay" by extending TilesOverlay and it contins this class:

https://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-android/src/main/java/org/osmdroid/views/overlay/TilesOverlay.java?r=1086

Then when setting up the mapview, I do this:

this.mTilesOverlay = new MyTileOverlay(mProvider, this.getBaseContext());

As instructed by kurtzmarc, I used handleTile() to check whether all tiles are being loaded or not:

@Override
        public void handleTile(final Canvas pCanvas, final int pTileSizePx,
                final MapTile pTile, final int pX, final int pY) {
            Drawable currentMapTile = mTileProvider.getMapTile(pTile);
            if (currentMapTile == null) {
                currentMapTile = getLoadingTile();
                Log.d("Tile Null", "Null");
            } else {

                Log.d("Tile Not Null", "Not Null");
            }

            if (currentMapTile != null) {
                mTileRect.set(pX * pTileSizePx, pY * pTileSizePx, pX
                        * pTileSizePx + pTileSizePx, pY * pTileSizePx
                        + pTileSizePx);
                onTileReadyToDraw(pCanvas, currentMapTile, mTileRect);
            }

            if (DEBUGMODE) {
                mTileRect.set(pX * pTileSizePx, pY * pTileSizePx, pX
                        * pTileSizePx + pTileSizePx, pY * pTileSizePx
                        + pTileSizePx);
                mTileRect.offset(-mWorldSize_2, -mWorldSize_2);
                pCanvas.drawText(pTile.toString(), mTileRect.left + 1,
                        mTileRect.top + mDebugPaint.getTextSize(),
                        mDebugPaint);
                pCanvas.drawLine(mTileRect.left, mTileRect.top,
                        mTileRect.right, mTileRect.top, mDebugPaint);
                pCanvas.drawLine(mTileRect.left, mTileRect.top,
                        mTileRect.left, mTileRect.bottom, mDebugPaint);
            }
        }

This method ensures whether the loading procedure is finalized or not:

@Override
            public void finaliseLoop() {
                Log.d("Loop Finalized", "Finalized");
            }

I can also use this method to identify whether all tiles have been loaded or not:

public int getLoadingBackgroundColor() {
            return mLoadingBackgroundColor;
        }

Hope this help someone!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!