Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Infobox mapframe

From Alternative Lifestyle Wiki
More languages
More actions

Documentation for this module may be created at Module:Infobox mapframe/doc

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local infoboxImage = require('Module:InfoboxImage').InfoboxImage

-- Default configurations
local DEFAULT_FRAME_WIDTH = "270"
local DEFAULT_FRAME_HEIGHT = "200"
local DEFAULT_ZOOM = 10

-- Trim whitespace from args, and remove empty args
local function trimArgs(argsTable)
    local cleanArgs = {}
    for key, val in pairs(argsTable) do
        if type(val) == 'string' then
            val = val:match('^%s*(.-)%s*$')
            if val ~= '' then
                cleanArgs[key] = val
            end
        else
            cleanArgs[key] = val
        end
    end
    return cleanArgs
end

-- Convert latitude and longitude to tile coordinates
local function latLonToTile(lat, lon, zoom)
    local n = 2 ^ zoom
    local xtile = math.floor((lon + 180.0) / 360.0 * n)
    local ytile = math.floor((1.0 - math.log(math.tan(lat * math.pi / 180.0) + 1 / math.cos(lat * math.pi / 180.0)) / math.pi) / 2.0 * n)
    return xtile, ytile
end

local p = {}

p.main = function(frame)
    local args = getArgs(frame)
    args = trimArgs(args)

    local lat = tonumber(args.lat or args.latitude)
    local lon = tonumber(args.lon or args.longitude)
    local zoom = tonumber(args.zoom) or DEFAULT_ZOOM
    local width = args.width or DEFAULT_FRAME_WIDTH
    local height = args.height or DEFAULT_FRAME_HEIGHT

    if not lat or not lon then
        return "Coordinates (lat, lon) must be specified."
    end

    -- Convert lat/lon to tile coordinates for the given zoom level
    local xtile, ytile = latLonToTile(lat, lon, zoom)
    
    -- Construct the OpenStreetMap tile URL
    local tileUrl = string.format("https://tile.openstreetmap.org/%d/%d/%d.png", zoom, xtile, ytile)
    
    -- Generate the HTML for the map frame
    local mapHtml = string.format(
        '<div class="mapframe" style="width: %dpx; height: %dpx;">' ..
        '<img src="%s" alt="Map view" style="width: 100%%; height: 100%%;">' ..
        '</div>',
        width, height, tileUrl
    )
    
    return frame:preprocess(mapHtml)
end

return p