Module:LocationMap: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 18: Line 18:
     local results = smw.ask(query)
     local results = smw.ask(query)
      
      
     -- Build coordinates list
     -- Build coordinates list with titles and colors
     local coordinates = {}
     local coordinates = {}
   
    -- Add current location (red marker)
     if hasCoordinates ~= '' then
     if hasCoordinates ~= '' then
         table.insert(coordinates, hasCoordinates)
         table.insert(coordinates, hasCoordinates .. '~' .. pageName .. '~' .. pageName .. '~red')
     end
     end
      
      
    -- Add child locations (blue markers)
     if results then
     if results then
         for _, row in ipairs(results) do
         for _, row in ipairs(results) do
             if row['Has Coordinates'] then
             if row['Has Coordinates'] then
                 table.insert(coordinates, row['Has Coordinates'])
                local childPage = row[1] or row['page'] or ''
                 table.insert(coordinates, row['Has Coordinates'] .. '~' .. childPage .. '~' .. childPage .. '~blue')
             end
             end
         end
         end

Revision as of 12:38, 16 February 2026

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 ''
    
    -- Get all child locations
    local query = {
        string.format('[[Is Located In::%s]]', pageName),
        '?Has Coordinates',
        'limit=500'
    }
    
    local results = smw.ask(query)
    
    -- Build coordinates list with titles and colors
    local coordinates = {}
    
    -- Add current location (red marker)
    if hasCoordinates ~= '' then
        table.insert(coordinates, hasCoordinates .. '~' .. pageName .. '~' .. pageName .. '~red')
    end
    
    -- Add child locations (blue markers)
    if results then
        for _, row in ipairs(results) do
            if row['Has Coordinates'] then
                local childPage = row[1] or row['page'] or ''
                table.insert(coordinates, row['Has Coordinates'] .. '~' .. childPage .. '~' .. childPage .. '~blue')
            end
        end
    end
    
    -- Build the display_map call
    local coordString = table.concat(coordinates, ';')
    local mapParams = {
        'zoom=6',
        'service=leaflet',
        'height=250px'
    }
    
    if hasCoordinates ~= '' then
        table.insert(mapParams, 'center=' .. hasCoordinates)
    end
    
    if hasGeoJson ~= '' then
        table.insert(mapParams, 'geojson=' .. hasGeoJson)
    end
    
    if coordString ~= '' then
        return frame:callParserFunction('#display_map', coordString, table.concat(mapParams, '|'))
    else
        return ''
    end
end

return p