Search code examples
androidoverlayandroid-mapviewopenstreetmaposmdroid

Use custom image as a maptile using osmdroid mapview


Does any body know if its possible to use an image as a custom maptile in a mapview using osmdroid? For example, I'd like the user to be able to select an image stored on the phone from my application and then have that image then replace a tile within my mapview.

Has anyone got any pointers or ideas of how this could be done?


Solution

  • I'm not sure to understand the real need. For just one specific zoom level?

    However, 2 directions:

    • OSMBonusPack GroundOverlay let you position a bitmap image somewhere on the map. You can adjust its size, rotation, and transparency. It's not replacing tiles, it's drawn above. And it works at all zoom levels.

    • You could try to replace a map tile file with your image file, in the osmdroid file system cache. These 2 utility functions will help you to get the exact file path for a given (lat,lon,zoom):

      public Point getMapTileFromCoordinates(final double aLat, final double aLon, final int zoom){
              final int y = (int) Math.floor((1 - Math.log(Math.tan(aLat * Math.PI / 180) + 1 / Math.cos(aLat * Math.PI / 180)) / Math.PI) / 2 * (1 << zoom));
              final int x = (int) Math.floor((aLon + 180) / 360 * (1 << zoom));
              return new Point(x, y);
      }
      
      public File getFileName(ITileSource tileSource, MapTile tile){
              final File file = new File(OpenStreetMapTileProviderConstants.TILE_PATH_BASE,
                              tileSource.getTileRelativeFilenameString(tile) + OpenStreetMapTileProviderConstants.TILE_PATH_EXTENSION);
              return file;
      }