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

别说谁变了你拦得住时间么 提交于 2019-12-07 02:50:51

问题


I have included Open Street Maps in my android application. In the mapview, user should be able to capture the screen after the map is fully loaded. But currently user can capture the image even when the mapview is still loading. Can someone tell me how to detect when the mapview is fully loaded?

Below is my code to load the mapview:

public class MainActivity extends Activity  {
    MapView mapView;
    MyLocationOverlay myLocationOverlay = null;
    ArrayList<OverlayItem> anotherOverlayItemArray;
    protected ItemizedOverlayWithBubble<ExtendedOverlayItem> itineraryMarkers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);

        final ArrayList<ExtendedOverlayItem> waypointsItems = new ArrayList<ExtendedOverlayItem>();
        itineraryMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this, waypointsItems, mapView, new ViaPointInfoWindow(R.layout.itinerary_bubble, mapView));
        mapView.getOverlays().add(itineraryMarkers);

        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.setBuiltInZoomControls(true);
        MapController mapController = mapView.getController();
        mapController.setZoom(1);
        GeoPoint point2 = new GeoPoint(51496994, -134733);
        mapController.setCenter(point2);

        Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
        GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
        ExtendedOverlayItem overlayItem = new ExtendedOverlayItem("Title Test Loc", "Desc", myPoint1, this);
        overlayItem.setMarkerHotspot(OverlayItem.HotspotPlace.BOTTOM_CENTER);
        overlayItem.setMarker(marker);
        overlayItem.setRelatedObject(0);
        itineraryMarkers.addItem(overlayItem);
        mapView.invalidate();



        myLocationOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocationOverlay);
        myLocationOverlay.enableMyLocation();

        myLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
             mapView.getController().animateTo(myLocationOverlay.getMyLocation());
               } 
           });


    }

    @Override
     protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      myLocationOverlay.enableMyLocation();
      myLocationOverlay.enableCompass();
      myLocationOverlay.enableFollowLocation();
     } 

     @Override
     protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      myLocationOverlay.disableMyLocation();
      myLocationOverlay.disableCompass();
      myLocationOverlay.disableFollowLocation();
     }

回答1:


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).




回答2:


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!




回答3:


Since osmdroid API 6.1.0, it's quite easy:

// check completeness of map tiles
TileStates tileStates = mapView.getOverlayManager().getTilesOverlay().getTileStates();

// evaluate the tile states
if (tileStates.getTotal() == tileStates.getUpToDate())
{
    // map is loaded completely

}
else
{
    // loading is still in progress

}

/* meaning of TileStates
    .getUpToDate()   not expired yet
    .getExpired()    expired
    .getScaled()     computed during zoom
    .getNotFound()   default grey tile
    ---------------------------------------
    .getTotal()      sum of all above
*/


来源:https://stackoverflow.com/questions/17079359/detect-if-the-osm-mapview-is-still-loading-or-not-in-android

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