Module:LocationMap: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 9: Line 9:
     local hasGeoJson = frame.args.hasGeoJson or ''
     local hasGeoJson = frame.args.hasGeoJson or ''
      
      
     -- Get all child locations with page names
     -- Start building the display_map query
     local query = {
     local mapQuery = ''
        string.format('[[Is Located In::%s]]', pageName),
        '?Has Coordinates',
        'mainlabel=-',
        'limit=500'
    }
      
      
    local results = smw.getQueryResult(query)
     -- Add current location coordinates
   
    -- Build coordinates list with page titles for clickable markers
    local coordinates = {}
   
     -- Add current location (red marker)
     if hasCoordinates ~= '' then
     if hasCoordinates ~= '' then
         table.insert(coordinates, hasCoordinates .. '~' .. pageName .. '~~#ff0000')
         mapQuery = hasCoordinates
     end
     end
      
      
     -- Add child locations (blue markers)
     -- Add child locations using inline query
     if results and results.results then
     local childQuery = string.format('[[Is Located In::%s]]', pageName)
        for childPageName, data in pairs(results.results) do
    mapQuery = mapQuery .. '{{#ask:' .. childQuery .. '|?Has Coordinates|format=list|sep=;|propsep=;}}'
            if data.printouts and data.printouts['Has Coordinates'] and data.printouts['Has Coordinates'][1] then
                local coords = data.printouts['Has Coordinates'][1]
                -- Handle if coords is a table or string
                if type(coords) == 'table' then
                    coords = coords.lat .. ',' .. coords.lon
                end
                -- Format: coordinates~title~text~icon/color
                table.insert(coordinates, coords .. '~' .. childPageName .. '~~#0000ff')
            end
        end
    end
      
      
     -- Build the display_map call
     -- Build parameters
     local coordString = ''
     local params = 'zoom=6|service=leaflet|height=250px'
    if #coordinates > 0 then
        coordString = table.concat(coordinates, ';')
    end
   
    local mapParams = 'zoom=6|service=leaflet|height=250px'
      
      
     if hasCoordinates ~= '' then
     if hasCoordinates ~= '' then
         mapParams = mapParams .. '|center=' .. hasCoordinates
         params = params .. '|center=' .. hasCoordinates
     end
     end
      
      
     if hasGeoJson ~= '' then
     if hasGeoJson ~= '' then
         mapParams = mapParams .. '|geojson=' .. hasGeoJson
         params = params .. '|geojson=' .. hasGeoJson
     end
     end
      
      
     if coordString ~= '' then
     -- Return the complete map call
        return frame:callParserFunction('#display_map', coordString, mapParams)
     local result = '{{#display_map:' .. mapQuery .. '|' .. params .. '}}'
     elseif hasGeoJson ~= '' then
      
        return frame:callParserFunction('#display_map', 'geojson=' .. hasGeoJson, 'zoom=6|service=leaflet|height=250px')
    return frame:preprocess(result)
     else
        return ''
    end
end
end


return p
return p

Revision as of 12:46, 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 ''
    
    -- Start building the display_map query
    local mapQuery = ''
    
    -- Add current location coordinates
    if hasCoordinates ~= '' then
        mapQuery = hasCoordinates
    end
    
    -- Add child locations using inline query
    local childQuery = string.format('[[Is Located In::%s]]', pageName)
    mapQuery = mapQuery .. '{{#ask:' .. childQuery .. '|?Has Coordinates|format=list|sep=;|propsep=;}}'
    
    -- Build parameters
    local params = 'zoom=6|service=leaflet|height=250px'
    
    if hasCoordinates ~= '' then
        params = params .. '|center=' .. hasCoordinates
    end
    
    if hasGeoJson ~= '' then
        params = params .. '|geojson=' .. hasGeoJson
    end
    
    -- Return the complete map call
    local result = '{{#display_map:' .. mapQuery .. '|' .. params .. '}}'
    
    return frame:preprocess(result)
end

return p