检测OSM Mapview是否仍然在android中加载

时间:2013-06-13 04:38:59

标签: android map osmdroid openstreetmap

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

以下是加载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();
     }

3 个答案:

答案 0 :(得分:4)

查看TilesOverlayTileLooper实施。这就是我们用来加载然后在屏幕上绘制每个图块的方法。在handleTile(...)中,我们尝试从图块提供程序mTileProvider.getMapTile(pTile)中获取图块。如果返回Drawable,则会加载图块,否则会返回null

执行此操作的一种简单方法是在调用TilesOverlay之前扩展drawTiles(...),覆盖TileLooper并调用您自己的super.drawTiles(...),以检查是否所有切片都是传递给handleTile(...)的不是null。要使用TilesOverlay调用mMapView.getOverlayManager().setTilesOverlay(myTilesOverlay)

答案 1 :(得分: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

然后在设置mapview时,我这样做:

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

希望这能有所帮助!

答案 2 :(得分:0)

自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
*/