Back to project page aBusTripMK.
The source code is released under:
GNU General Public License
If you think the Android project aBusTripMK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.andnav.osm.views.overlay; //from w w w . ja v a 2s. c o m import org.andnav.osm.services.util.OpenStreetMapTile; import org.andnav.osm.util.GeoPoint; import org.andnav.osm.util.MyMath; import org.andnav.osm.views.OpenStreetMapView; import org.andnav.osm.views.OpenStreetMapView.OpenStreetMapViewProjection; import org.andnav.osm.views.util.OpenStreetMapRendererInfo; import org.andnav.osm.views.util.OpenStreetMapTileProvider; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.os.Handler; import android.os.Message; import android.util.Log; public class OpenStreetMapTilesOverlay extends OpenStreetMapViewOverlay { protected OpenStreetMapView mOsmv; protected OpenStreetMapRendererInfo mRendererInfo; /** Current renderer */ protected final OpenStreetMapTileProvider mTileProvider; protected final Paint mPaint = new Paint(); public OpenStreetMapTilesOverlay(final OpenStreetMapView aOsmv, final OpenStreetMapRendererInfo aRendererInfo, final OpenStreetMapTileProvider aTileProvider) { this.mOsmv = aOsmv; this.mRendererInfo = aRendererInfo; if(aTileProvider == null) mTileProvider = new OpenStreetMapTileProvider(mOsmv.getContext(), new SimpleInvalidationHandler()); else this.mTileProvider = aTileProvider; } public OpenStreetMapRendererInfo getRendererInfo() { return mRendererInfo; } public void setRendererInfo(final OpenStreetMapRendererInfo aRendererInfo) { this.mRendererInfo = aRendererInfo; } public void setAlpha(int a) { this.mPaint.setAlpha(a); } @Override protected void onDraw(Canvas c, OpenStreetMapView osmv) { //if(DEBUGMODE) // Log.v(DEBUGTAG, "onDraw"); /* * Do some calculations and drag attributes to local variables to save * some performance. */ final OpenStreetMapViewProjection pj = osmv.getProjection(); final int zoomLevel = osmv.getZoomLevel(); final Rect viewPort = c.getClipBounds(); final int tileSizePx = this.mRendererInfo.MAPTILE_SIZEPX; final int tileZoom = this.mRendererInfo.MAPTILE_ZOOM; final int worldSize_2 = 1 << (zoomLevel + this.mRendererInfo.MAPTILE_ZOOM - 1); final OpenStreetMapTile tile = new OpenStreetMapTile(0, 0, 0, 0); tile.rendererID = this.mRendererInfo.ordinal(); // TODO get from service /* * Calculate the amount of tiles needed for each side around the center * one. */ viewPort.offset(worldSize_2, worldSize_2); final int tileNeededToLeftOfCenter = (viewPort.left >> tileZoom) - 1; final int tileNeededToRightOfCenter = viewPort.right >> tileZoom; final int tileNeededToTopOfCenter = (viewPort.top >> tileZoom) - 1; final int tileNeededToBottomOfCenter = viewPort.bottom >> tileZoom; final int mapTileUpperBound = 1 << zoomLevel; // final Point mapTileCoords = new Point(); Point tilePos = new Point(); /* Draw all the MapTiles (from the upper left to the lower right). */ for (int y = tileNeededToTopOfCenter; y <= tileNeededToBottomOfCenter; y++) { for (int x = tileNeededToLeftOfCenter; x <= tileNeededToRightOfCenter; x++) { /* Construct a URLString, which represents the MapTile. */ tile.zoomLevel = zoomLevel; tile.y = MyMath.mod(y, mapTileUpperBound); tile.x = MyMath.mod(x, mapTileUpperBound); pj.toPixels(x, y, tilePos); final Bitmap currentMapTile = mTileProvider.getMapTile(tile); if (currentMapTile != null) { c.drawBitmap(currentMapTile, tilePos.x, tilePos.y, mPaint); } //if (DEBUGMODE) { // c.drawText(tile.toString(), tilePos.x + 1, tilePos.y + mPaint.getTextSize(), mPaint); // c.drawLine(tilePos.x, tilePos.y, tilePos.x + tileSizePx, tilePos.y, mPaint); // c.drawLine(tilePos.x, tilePos.y, tilePos.x, tilePos.y + tileSizePx, mPaint); //} } } // draw a cross at center in debug mode if (DEBUGMODE) { final GeoPoint center = osmv.getMapCenter(); final Point centerPoint = pj.toMapPixels(center, null); c.drawLine(centerPoint.x, centerPoint.y - 9, centerPoint.x, centerPoint.y + 9, mPaint); c.drawLine(centerPoint.x - 9, centerPoint.y, centerPoint.x + 9, centerPoint.y, mPaint); } } @Override protected void onDrawFinished(Canvas c, OpenStreetMapView osmv) { } private class SimpleInvalidationHandler extends Handler { @Override public void handleMessage(final Message msg) { switch (msg.what) { case OpenStreetMapTile.MAPTILE_SUCCESS_ID: mOsmv.invalidate(); break; } } } }