Module:LocationMap: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
     local hasGeoJson = frame.args.hasGeoJson or ''
     local hasGeoJson = frame.args.hasGeoJson or ''
      
      
     -- Get all child locations with page names
     -- Build coordinates string
     local query = {
     local coordParts = {}
        string.format('[[Is Located In::%s]]', pageName),
        '?Has Coordinates',
        'mainlabel=-',
        'limit=500'
    }
      
      
    local results = smw.getQueryResult(query)
     -- Add current location
   
    -- 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')
         table.insert(coordParts, hasCoordinates)
     end
     end
      
      
     -- Add child locations (blue markers)
     -- Query child locations
     if results and results.results then
    local queryString = '[[Is Located In::' .. pageName .. ']]|?Has Coordinates|limit=500'
         for childPageName, data in pairs(results.results) do
    local results = smw.ask(queryString)
             if data.printouts and data.printouts['Has Coordinates'] and data.printouts['Has Coordinates'][1] then
   
                local coords = data.printouts['Has Coordinates'][1]
     if results then
                -- Handle if coords is a table or string
         for i, row in ipairs(results) do
                if type(coords) == 'table' then
             local coords = row['Has Coordinates']
                    coords = coords.lat .. ',' .. coords.lon
            if coords and coords ~= '' then
                end
                 table.insert(coordParts, coords)
                -- Format: coordinates~title~text~icon/color
                 table.insert(coordinates, coords .. '~' .. childPageName .. '~~#0000ff')
             end
             end
         end
         end
     end
     end
      
      
     -- Build the display_map call
     -- Build final map call
     local coordString = ''
     local coordString = table.concat(coordParts, ';')
    if #coordinates > 0 then
        coordString = table.concat(coordinates, ';')
    end
   
    local mapParams = 'zoom=6|service=leaflet|height=250px'
      
      
    -- Build all parameters as one string
    local allParams = coordString
     if hasCoordinates ~= '' then
     if hasCoordinates ~= '' then
         mapParams = mapParams .. '|center=' .. hasCoordinates
         allParams = allParams .. '|center=' .. hasCoordinates
     end
     end
   
     if hasGeoJson ~= '' then
     if hasGeoJson ~= '' then
         mapParams = mapParams .. '|geojson=' .. hasGeoJson
         allParams = allParams .. '|geojson=' .. hasGeoJson
     end
     end
    allParams = allParams .. '|zoom=6|service=leaflet|height=250px'
      
      
     if coordString ~= '' then
     return frame:callParserFunction('#display_map', allParams)
        return frame:callParserFunction('#display_map', coordString, mapParams)
    elseif hasGeoJson ~= '' then
        return frame:callParserFunction('#display_map', 'geojson=' .. hasGeoJson, 'zoom=6|service=leaflet|height=250px')
    else
        return ''
    end
end
end


return p
return p

Latest revision as of 12:47, 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 ''
    
    -- 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