Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

header_add_map_providers()

Description

Adds map providers for Leaflet maps.

This function generates a JavaScript snippet that defines various tile layer providers for use in Leaflet maps.
It supports OpenStreetMap and ESRI basemaps, as well as custom providers defined in the global variable `$geo_leaflet_sources`.
The function also includes options for caching tile layers and handling different zoom levels and attribution.

Parameters

This function accepts no parameters.

Return

void Outputs the HTML and JavaScript for adding map providers to Leaflet.

Location

include/map_functions.php lines 608 to 764

Definition

 
function header_add_map_providers()
    {
    global 
$geo_leaflet_sources$baseurl$geo_tile_caching;
    
?>
    <script>
    // Copied from leaflet-providers.js
    (function (root, factory) {
        if (typeof define === 'function' && define.amd) {
            // AMD. Register as an anonymous module.
            define(['leaflet'], factory);
        } else if (typeof modules === 'object' && module.exports) {
            // define a Common JS module that relies on 'leaflet'
            module.exports = factory(require('leaflet'));
        } else {
            // Assume Leaflet is loaded into global object L already
            factory(L);
        }
    }(this, function (L) {
        'use strict';

        L.TileLayer.Provider = L.TileLayer.extend({
            initialize: function (arg, options) {
                var providers = L.TileLayer.Provider.providers;

                var parts = arg.split('.');

                var providerName = parts[0];
                var variantName = parts[1];

                if (!providers[providerName]) {
                    throw 'No such provider (' + providerName + ')';
                }

                var provider = {
                    url: providers[providerName].url,
                    options: providers[providerName].options
                };

                // overwrite values in provider from variant.
                if (variantName && 'variants' in providers[providerName]) {
                    if (!(variantName in providers[providerName].variants)) {
                        throw 'No such variant of ' + providerName + ' (' + variantName + ')';
                    }
                    var variant = providers[providerName].variants[variantName];
                    var variantOptions;
                    if (typeof variant === 'string') {
                        variantOptions = {
                            variant: variant
                        };
                    } else {
                        variantOptions = variant.options;
                    }
                    provider = {
                        url: variant.url || provider.url,
                        options: L.Util.extend({}, provider.options, variantOptions)
                    };
                }

                // replace attribution placeholders with their values from toplevel provider attribution,
                // recursively
                var attributionReplacer = function (attr) {
                    if (attr.indexOf('{attribution.') === -1) {
                        return attr;
                    }
                    return attr.replace(/\{attribution.(\w*)\}/g,
                        function (match, attributionName) {
                            return attributionReplacer(providers[attributionName].options.attribution);
                        }
                    );
                };
                provider.options.attribution = attributionReplacer(provider.options.attribution);

                // Compute final options combining provider options with any user overrides
                var layerOpts = L.Util.extend({}, provider.options, options);
                L.TileLayer.prototype.initialize.call(this, provider.url, layerOpts);
            }
        });

        /**
        * Definition of providers.
        * see http://leafletjs.com/reference.html#tilelayer for options in the options map.
        */

        L.TileLayer.Provider.providers = {

        
        
foreach($geo_leaflet_sources as $leaflet_source)
            {
            echo 
escape($leaflet_source["code"])  . ": {\n";
            if(
$geo_tile_caching)
                {
                
// Is this the search page? If so need to get collection ID to authenticate external shares
                
$searchparts explode(" "getval("search"""));
                
$collection str_replace("!collection"""$searchparts[0]);
                
$resource getval("ref",""); // For resource view page
                
$urlparams = array(
                    
"provider"  =>  $leaflet_source["code"],
                    
"resource"  => $resource,
                    
"collection"  => $collection,
                    
"k"  =>  getval("k",""),
                    );
                
$sourceurl generateURL($baseurl "/pages/ajax/tiles.php",$urlparams) . "&x={x}&y={y}&z={z}";
                }
            else
                {
                
$sourceurl =  $leaflet_source["url"];
                }
            echo 
"        url: '" $sourceurl "',\n";
            echo 
"        options: {\n";
            if(isset(
$leaflet_source["maxZoom"]) && is_int_loose($leaflet_source["maxZoom"]))
                {
                echo 
"        maxZoom: " . (int)$leaflet_source["maxZoom"] . ",\n";
                } 
            if(isset(
$leaflet_source["attribution"]))
                {
                echo 
"        attribution: '" $leaflet_source["attribution"] . "',\n";
                }
            echo 
"    },\n"// End of options
            
echo "        variants: {\n";
            foreach(
$leaflet_source["variants"] as $variant=>&$variantdata)
                {
                echo 
$variant  ": {\n        ";
                if(isset(
$variantdata["url"]))
                    {
                    if(
$geo_tile_caching)
                        {
                        
$urlparams["variant"] = $variant;
                        
$variantdata["url"] = generateURL($baseurl "/pages/ajax/tiles.php",$urlparams) . "&x={x}&y={y}&z={z}";
                        }
                    echo 
"    url: '" $variantdata["url"] . "'\n";
                    }
                echo 
"},\n";
                }    
            echo 
"         },\n"// End of variants
            
echo "},\n"// End of leaflet source
            
}
        
?>
        ResourceSpace: {
            url: ' echo $baseurl?>/pages/ajax/tiles.php?x={x}&y={y}&z={z}',
            options: {
                maxZoom: 3,
                attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
                },
            variants: { OSM: {}}
            }

        };

        L.tileLayer.provider = function (provider, options) {
            return new L.TileLayer.Provider(provider, options);
        };

        return L;
    }));
    </script>
    
    
}

This article was last updated 12th December 2024 15:35 Europe/London time based on the source file dated 22nd November 2024 15:35 Europe/London time.