Search code examples
osmdroid

Draw tiles locally with OSMDroid Tilesource


How do you custom draw (at runtime) OSMDroid tiles. I am trying to generate a (simple) weather overlay on the device itself from data. Whilst a overlay would suffice and I understand MapsForge may be one such possibility for generating vector tiles, the data I am trying to draw is very simple and I figured it may be overkill?

I have attempted to implement a generic BitmapTileSourceBase and overwrite the getDrawable() method to return a bitmap but this does not seem to get triggered and end up with blank tiles.

public class DrawnTiles extends BitmapTileSourceBase {
public DrawnTiles(String aName) {
    super(aName, 1, 6, 256, ".png");
}

@Override
public synchronized  Drawable getDrawable(final String aFilePath) {
    //Make the bad tile easy to spot
    Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.RGB_565);
    bitmap.eraseColor(Color.YELLOW);

    return new BitmapDrawable(bitmap);
}
}

Appreciate any advice or a preferred solution. The final tiles will be drawn according to their bounds so a way to access this the method will be ideal. No fussed about cacheing so much as the data will change fairly frequently.


Solution

  • Whilst not a direct answer to the original question the following does solve my problem and may solve others.

    Generating tiles may be required for a global solution, however the OSMDroid Custom Overlay does seem to satisfy my requirements. Supporter functions for converting between pixels & lat/longs.

    public class CustomOverlay extends org.osmdroid.views.overlay.Overlay {
        @Override
        public void draw(Canvas canvas, MapView map, boolean shadow) {
            if (!isEnabled()) return;
            if (shadow) {
                //draw a shadow if needed, otherwise return
                return;
            }
    
            /*
            This will go from pixel x,y to lat/lon
            GeoPoint iGeoPoint = (GeoPoint) projection.fromPixels(x,y);
    
            This will go from lat/lon to pixel x,y
            projection.toPixels(geoPoint, pt);
    
            To project pixels should give you the canvas coordinates
            projection.toProjectedPixels(...)
             */
    
            final Projection pj = map.getProjection();
            canvas.save();
            Paint paint = new Paint();
            canvas.drawCircle(pj.getScreenCenterX(),pj.getScreenCenterY(),40,paint);
            canvas.restore();
    }