Tile Map Viewer for PureBasic 6.x

TileMapViewer.pbi is a PureBasic module that provides an interactive, multi-layer map viewer with support for online tile servers (see here https://www.strasis.com/documentation/limelight-xe/reference/tile-map-servers), precipitation overlays, caching, zooming, panning, and a customizable home marker. Precipitation tiles are optional; if enabled, the module can display real-time weather overlays from OpenWeather, which requires a free API key available after registration. The viewer features smooth navigation, threaded tile downloads, and is ideal for embedding dynamic maps in PureBasic applications.

Download


TileMapViewer() Procedure Parameters

Parameter NameTypeDescriptionExample Value
CanvasGadgetIntegerThe gadget ID of the canvas where the map will be drawn.gCanvas
homeLatDoubleLatitude (in degrees) for the “home” marker (centered on double-click or reset).49.5750583
homeLonDoubleLongitude (in degrees) for the “home” marker.17.4841983
defaultZoomIntegerInitial zoom level (typically 0–19, higher is more zoomed in).4
baseTileServer$StringURL template for the base map tile server. Use {z}{x}{y} as placeholders for zoom/x/y."https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}.jpg"
poolSizeIntegerNumber of concurrent download threads for tile fetching.50
cacheDir$StringDirectory path for cached map tiles."mycache"
markerDiameterIntegerDiameter (in pixels) of the home marker.8
markerBorderColorIntegerRGBA color value for the home marker border.RGBA(255,0,0,255)
markerFillColorIntegerRGBA color value for the home marker fill.RGBA(255,0,0,255)
showPrecipBoolean#True to enable precipitation overlay, #False to disable.#True
precipTileServer$StringURL template for the precipitation tile server. Use {z}{x}{y}{APIKEY} as placeholders."https://tile.openweathermap.org/map/precipitation_new/{z}/{x}/{y}.png?appid={APIKEY}"
precipApiKey$StringAPI key for the precipitation tile server."YOU API KEY"
precipRefreshIntervalIntegerPrecipitation overlay refresh interval (in seconds).60

Usage Example

TileMapViewer(gCanvas, 49.5750583, 17.4841983, 4, 
              "https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}.jpg", 
              50, "mycache", 
              8, RGBA(255,0,0,255), RGBA(255,0,0,255), 
              #True, 
              "https://tile.openweathermap.org/map/precipitation_new/{z}/{x}/{y}.png?appid={APIKEY}", 
              "xxxxxxxxxxxxxxxxxxxxxxx",
              60)

UpdateMap(CanvasGadget)

VariableTypeDescription
w, hIntegerWidth and height of the canvas gadget.
tIntegerTile size in pixels (from gTileSize).
tilesX, tilesYIntegerNumber of tiles horizontally/vertically needed to fill the canvas.
gridIntegerNumber of tiles in the grid (from gGrid).
halfGridIntegerHalf the grid size (for centering).
maxTileIntegerMaximum tile index at current zoom.
x, yIntegerLoop counters for tile grid.
tileX, tileYIntegerTile X/Y indices for each tile in the grid.
cacheDir$StringDirectory path for tile cache.
cachePath$StringFull file path for cached tile.
baseUrl$StringURL for the base map tile.
baseExt$StringFile extension for the base map tile.
precipKey$StringKey for precipitation overlay cache/maps.
expiredBooleanWhether the precipitation overlay is expired and needs refresh.
precipUrl$StringURL for the precipitation tile.

DrawMap(CanvasGadget)

VariableTypeDescription
gCanvasW, gCanvasHIntegerWidth and height of the canvas gadget.
grid, halfGridIntegerGrid size and half grid size.
tilePxIntegerTile size in pixels.
centerScreenX, centerScreenYIntegerCenter of the canvas in pixels.
x, yIntegerLoop counters for jobs.
dx, dyIntegerPixel offset for each tile from the center.
imgIntegerImage handle for base map tile.
precipImgIntegerImage handle for precipitation overlay.
homeTileX, homeTileYDoubleTile X/Y for the home marker.
pixelOffsetX, pixelOffsetYDoublePixel offset for the home marker.
markerScreenX, markerScreenYIntegerScreen position for the home marker.
markerRadiusIntegerRadius of the home marker.
cx, cyIntegerCenter of the canvas (for spinner).
radius, spokeLength, spokeWidth, spokesIntegerSpinner parameters.
angleDeg, angleRadFloatSpinner spoke angle in degrees/radians.
xStart, yStart, xEnd, yEndFloatSpinner spoke start/end coordinates.
perpX, perpYFloatPerpendicular vector for spinner spoke thickness.
lengthFloatLength for normalizing vectors.
timeStr$StringText for last precipitation update.
scaleBarLengthPxIntegerLength of the scale bar in pixels.
scaleBarLengthMIntegerLength of the scale bar in meters.
scaleLabel$StringLabel for the scale bar (e.g., “2 km”).
barX, barYIntegerX and Y position for the scale bar.
barHeightIntegerHeight (thickness) of the scale bar.
endMarkHeightIntegerHeight of the vertical end marks for the scale bar.
endMarkWidthIntegerWidth of the vertical end marks for the scale bar.
bottomMarginIntegerMargin from the bottom of the canvas for the scale bar.

StartTileDownloads(CanvasGadget)

VariableTypeDescription
activeThreadsIntegerNumber of currently active download threads.
idxIntegerIndex for thread array.
*jobPointerPointer to a TileJob structure.
allDoneBooleanWhether all jobs are finished.
totalSizeQuadTotal size of all jobs.
totalProgressQuadTotal progress of all jobs.

TileMapViewer_HandleEvent(CanvasGadget, EventType)

VariableTypeDescription
nowIntegerCurrent time in ms (for double-click detection).
dx, dyFloatPan offset in tiles after drag.

Scale Bar Variables (in DrawMap)

VariableTypeDescription
earthCircumferenceDoubleEarth’s circumference in meters.
latRadDoubleCenter latitude in radians.
metersPerPixelDoubleMeters per pixel at current zoom and latitude.
scaleLengths()ArrayArray of possible scale bar lengths (in meters).
maxBarPxIntegerMaximum allowed width of the scale bar in pixels.

Leave a Reply