Module:LocationMap

From The Seven Sages of Rome
Revision as of 12:47, 16 February 2026 by Noeth (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

function p.displayMap(frame)
    local mw = require('mw')
    local smw = mw.smw or mw.ext.smw
    
    local pageName = frame.args.pageName or mw.title.getCurrentTitle().text
    local hasCoordinates = frame.args.hasCoordinates or ''
    local hasGeoJson = frame.args.hasGeoJson or ''
    
    -- Build coordinates string
    local coordParts = {}
    
    -- Add current location
    if hasCoordinates ~= '' then
        table.insert(coordParts, hasCoordinates)
    end
    
    -- Query child locations
    local queryString = '[[Is Located In::' .. pageName .. ']]|?Has Coordinates|limit=500'
    local results = smw.ask(queryString)
    
    if results then
        for i, row in ipairs(results) do
            local coords = row['Has Coordinates']
            if coords and coords ~= '' then
                table.insert(coordParts, coords)
            end
        end
    end
    
    -- Build final map call
    local coordString = table.concat(coordParts, ';')
    
    -- Build all parameters as one string
    local allParams = coordString
    if hasCoordinates ~= '' then
        allParams = allParams .. '|center=' .. hasCoordinates
    end
    if hasGeoJson ~= '' then
        allParams = allParams .. '|geojson=' .. hasGeoJson
    end
    allParams = allParams .. '|zoom=6|service=leaflet|height=250px'
    
    return frame:callParserFunction('#display_map', allParams)
end

return p