如何使用带有Stamen贴图的Leaflet.js插件?

时间:2018-02-19 21:20:01

标签: javascript d3.js leaflet cartodb stamen-maps

我正在尝试将以下Leaflet.js滑块添加到我的地图中:https://github.com/Eclipse1979/leaflet-slider

我最初在安装Carto时安装了传单

<!-- cartodb.js comes with Leaflet @0.7 and jQuery -->
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />

但是,当我尝试将Sllet的更高版本安装到用户Slider时,会引发以下类型错误:

TypeError: L.StamenTileLayer is not a constructor

我尝试使用快速入门指南安装Leaflet:

&#13;
&#13;
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"
   integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
   crossorigin=""/>
   
<!-- Make sure you put this AFTER Leaflet's CSS -->
 <script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"
   integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
   crossorigin=""></script>
&#13;
&#13;
&#13;

并直接从插件母版

安装文件
<link rel="stylesheet" href="leaflet.css">
<link rel="stylesheet" href="example.css">
<link rel="stylesheet" href="leaflet-slider.css">

<script src="leaflet.js"></script>
<script src="leaflet-slider.js"></script>

两种情况都会导致相同的错误。我使用以下内容加载我的Stamen图层:

var map = L.map('map').setView([51.47, 0.25], 10);
    map.on('click', onMapClick);
    //create a tile layer for our toner basemap
    var tonerLayer = new L.StamenTileLayer("toner");
    map.addLayer(tonerLayer);

我使用以下代码添加微调器:

slider = L.control.slider(function(value) {
            console.log(value);
        }, {
        min: 1000,
        max: 5000,
        value: 1000,
        step:100,
        size: '250px',
        orientation:'horizontal',
        id: 'slider'
    }).addTo(map);

2 个答案:

答案 0 :(得分:5)

您是否尝试将底图定义为TileLayer? 将图层添加到本机传单图时,语法不同,因为它们不是内置于Leaflet的各种底图。

var Stamen_Toner = L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.{ext}', {
    attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
   subdomains: 'abcd',
   minZoom: 0,
   maxZoom: 20,
   ext: 'png'
});
map.addLayer(Stamen_Toner);

结帐Leaflet Providers Demo了解其他样本和底图

答案 1 :(得分:2)

您尝试使用雄蕊功能L.StamenTileLayer,而无需先加载Stamen library。通过在head标记(以及Leaflet.js库)中添加它来安装它:

<script src="http://maps.stamen.com/js/tile.stamen.js"></script>

或安全版本:

<script src="https://stamen-maps.a.ssl.fastly.net/js/tile.stamen.js"></script>

此代码现在应该可以使用:

var tonerLayer = new L.StamenTileLayer("toner");
map.addLayer(tonerLayer);