Detect if OSM Mapview is still loading or not in android

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

Below is my code for loading 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(); } 
+6
source share
2 answers

I created a class called "MyTileOverlay" by extending TilesOverlay and continuing 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 mapview, I do this:

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

As stated in kurtzmarc, I used handleTile () to check if all tiles are 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 that the download procedure is completed or not:

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

I can also use this method to determine if all tiles have been loaded or not:

 public int getLoadingBackgroundColor() { return mLoadingBackgroundColor; } 

Hope this helps someone!

+1
source

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

An easy way to do this is to expand the TilesOverlay , override drawTiles(...) and call your own TileLooper before calling super.drawTiles(...) , which will check if all the tiles that are passed to handleTile(...) , not zero. To use the TilesOverlay call mMapView.getOverlayManager mMapView.getOverlayManager().setTilesOverlay(myTilesOverlay) .

+4
source

Source: https://habr.com/ru/post/947180/


All Articles