7

我在我的 android 应用程序中包含了 Open Street Maps。在地图视图中,用户应该能够在地图完全加载后捕获屏幕。但是目前即使地图视图仍在加载,用户也可以捕获图像。有人能告诉我如何检测地图视图何时完全加载吗?

下面是我加载地图视图的代码:

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();
     }
4

3 回答 3

4

看看TilesOverlayTileLooper实施。这就是我们用来加载然后在屏幕上绘制每个图块的方法。在handleTile(...)我们尝试从 tile provider 获取 tile mTileProvider.getMapTile(pTile)。如果返回 aDrawable则图块已加载,否则将返回null

一个简单的方法是在调用之前扩展TilesOverlay、覆盖drawTiles(...)和调用你自己的方法,这将检查是否所有传递给的图块都不为空。使用您的 TilesOverlay 调用。TileLoopersuper.drawTiles(...)handleTile(...)mMapView.getOverlayManager().setTilesOverlay(myTilesOverlay)

于 2013-06-26T13:09:09.810 回答
2

从 osmdroid API 6.1.0 开始,这很容易:

// 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
*/
于 2019-11-29T13:24:59.027 回答
1

我通过扩展 TilesOverlay 创建了一个名为“MyTileOverlay”的类,它延续了这个类:

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

然后在设置地图视图时,我这样做:

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

按照 kurtzmarc 的指示,我使用 handleTile() 检查是否正在加载所有图块:

@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);
            }
        }

此方法确保加载过程是否已完成:

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

我还可以使用此方法来识别是否所有图块都已加载:

public int getLoadingBackgroundColor() {
            return mLoadingBackgroundColor;
        }

希望这对某人有帮助!

于 2013-06-27T07:57:33.957 回答