为什么 Bootstrap Slider 不能与 Carto 地图一起使用?

Why doesn't Bootstrap Slider work with Carto map?

我已将 Bootstrap 滑块的属性编辑如下:

<!DOCTYPE html>
<html>
  <head>
  <title>Slider</title>
  <script LANGUAGE="JavaScript" src="jquery.min.js" type="text/javascript"></script>
  <script LANGUAGE="JavaScript" src="bootstrap-slider.js" type="text/javascript"></script>
  <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <style>
    html,
    body,
    #Myslider {
      position: absolute;
      left: 500px;
    }

    #Myslider.slider-selection,
    #Myslider.slider-track-low {
        background-color: black;
    }
  </style>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

  <link rel="stylesheet" type="text/css" href="bootstrap-slider.css">
  <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
  </head>
  <body>
    <p align="center" id="demo">Start Sliding !</p>
    <input id="Myslider" data-provide="slider" data-slider-min="1" data-slider-max="12" data-slider-step="1" data-slider-value="1" data-slider-selection="after" />
    <script>
    function main() {
      $('#Myslider').slider();
      $("#Myslider").on("slide", function(slideEvt) {
        $("#SliderVal").text(slideEvt.value);
        if ($('#Myslider').val() == 1) {
          document.getElementById("demo").innerHTML = "Genel endeks";
        }
        if ($('#Myslider').val() == 2) {
          document.getElementById("demo").innerHTML = "Konut";
        }
        if ($('#Myslider').val() == 3) {
          document.getElementById("demo").innerHTML = "Çalışma hayatı";
        }
        if ($('#Myslider').val() == 4) {
          document.getElementById("demo").innerHTML = "Gelir ve servet";
        }
        if ($('#Myslider').val() == 5) {
          document.getElementById("demo").innerHTML = "Sağlık";
        }
        if ($('#Myslider').val() == 6) {
          document.getElementById("demo").innerHTML = "Eğitim";
        }
        if ($('#Myslider').val() == 7) {
          document.getElementById("demo").innerHTML = "Çevre";
        }
        if ($('#Myslider').val() == 8) {
          document.getElementById("demo").innerHTML = "Güvenlik";
        }
        if ($('#Myslider').val() == 9) {
          document.getElementById("demo").innerHTML = "Sivil katılım";
        }
        if ($('#Myslider').val() == 10) {
          document.getElementById("demo").innerHTML = "Altyapı hizmetlerine erişim";
        }
        if ($('#Myslider').val() == 11) {
          document.getElementById("demo").innerHTML = "Sosyal yaşam";
        }
        if ($('#Myslider').val() == 12) {
          document.getElementById("demo").innerHTML = "Yaşam memnuniyeti";
        }
      });
   }
   window.onload = main;
   </script>
  </body>
</html>

您也可以在 JSFiddle 上查看此代码。

然后我在 carto 上创建了一个地图,并尝试在这个 HTML 页面上添加这个滑块;

<!DOCTYPE html>
<html>
  <head>
  <title>Living Index</title>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <meta name="author" content="capanh">
  <link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
  <script LANGUAGE="JavaScript" src="jquery.min.js" type="text/javascript"></script>
  <script LANGUAGE="JavaScript" src="bootstrap-slider.js" type="text/javascript"></script>
  <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <style>
    html,
    body,
    #map {
        height: 600px;
        width: 1900px;
        left: 0px;
        padding: 0;
        margin: 0;
    }

    #Myslider {
        position: absolute;
        left: 850px;
    }

    #Myslider.slider-selection,
    #Myslider.slider-track-low {
        background-color: black;
    }
  </style>
  <link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" />
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <!--[if lte IE 8]>
  <link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v2/themes/css/cartodb.ie.css" />
  <![endif]-->
  <link rel="stylesheet" type="text/css" href="bootstrap-slider.css">
  <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
  </head>
  <body>
    <div id="map"></div>
    <div id="legend"></div>
    <p align="center" id="demo">Kaydirmaya Basla !</p>
    <input id="Myslider" data-provide="slider" data-slider-min="1" data-slider-max="12" data-slider-step="1" data-slider-value="1" data-slider-selection="after" />
    <!-- include cartodb.js library -->
    <script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
    function main() {
      var map = new L.Map('map', {
        zoomControl: false,
        center: [38.9765, 35.3320],
        zoom: 5
      });
      L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.jpg', {
        attribution: 'CartoDB · OSM data'}).addTo(map);
      cartodb.createLayer(map, 'https://capanh.carto.com/api/v2/viz/94dd9018-e918-11e6-a6cf-0ef7f98ade21/viz.json', {
        legends: true
      }).addTo(map);
      $('#Myslider').slider();
      $("#Myslider").on("slide", function(slideEvt) {
        $("#SliderVal").text(slideEvt.value);
        if ($('#Myslider').val() == 1) {
          document.getElementById("demo").innerHTML = "Genel endeks";
        }
        if ($('#Myslider').val() == 2) {
          document.getElementById("demo").innerHTML = "Konut";
        }
        if ($('#Myslider').val() == 3) {
          document.getElementById("demo").innerHTML = "Çalışma hayatı";
        }
        if ($('#Myslider').val() == 4) {
          document.getElementById("demo").innerHTML = "Gelir ve servet";
        }
        if ($('#Myslider').val() == 5) {
          document.getElementById("demo").innerHTML = "Sağlık";
        }
        if ($('#Myslider').val() == 6) {
          document.getElementById("demo").innerHTML = "Eğitim";
        }
        if ($('#Myslider').val() == 7) {
          document.getElementById("demo").innerHTML = "Çevre";
        }
        if ($('#Myslider').val() == 8) {
          document.getElementById("demo").innerHTML = "Güvenlik";
        }
        if ($('#Myslider').val() == 9) {
          document.getElementById("demo").innerHTML = "Sivil katılım";
        }
        if ($('#Myslider').val() == 10) {
          document.getElementById("demo").innerHTML = "Altyapı hizmetlerine erişim";
        }
        if ($('#Myslider').val() == 11) {
          document.getElementById("demo").innerHTML = "Sosyal yaşam";
        }
        if ($('#Myslider').val() == 12) {
          document.getElementById("demo").innerHTML = "Yaşam memnuniyeti";
        }
      });
    }
    window.onload = main;
    </script>
  </body>
</html>

最终结果是文本框而不是滑块。一开始我以为是引用错误。但是,正如您在第一个代码块中看到的那样,滑块单独使用时使用相同的引用。为什么在我向文件添加地图时它不起作用?有人见过这样的东西吗?

我已经给 Carto 发了邮件,他们很友好地回答了我的问题。我的代码中的问题是库冲突。如您所见,我包含了一个名为 jquery-ui ;

的 js 文件
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

但这不是必须的!当我删除这行代码时,滑块工作了!我还创建了一个新的 div 来包含文本和滑块对象。将库添加到同一文件夹,您可以将 Bootstrap Slider 与 Carto 地图一起使用。最后,工作代码如下所示;

<!DOCTYPE html>
<html>
   <head>
<title>Living Index</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="author" content="capanh">
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />

<script LANGUAGE="JavaScript" src="jquery.min.js" type="text/javascript"></script>
<script LANGUAGE="JavaScript" src="bootstrap-slider.js" type="text/javascript"></script>
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<style>
    html,
    body,
    #map {
        height: 600px;
        width: 1900px;
        left: 0px;
    top : 40px;
        padding: 0;
        margin: 0;
    }

    #Myslider {
        position: relative;
    }

    #Myslider.slider-selection,
    #Myslider.slider-track-low {
        background-color: black;
    }
</style>

<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v2/themes/css/cartodb.ie.css" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="bootstrap-slider.css">
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">

</head>

<body>
   <!-- include cartodb.js library -->
<div id='slider_container' align="center">
  <p id="demo">Start Sliding !</p>
  <input id="Myslider" data-provide="slider" data-slider-min="1" data-slider-max="12" data-slider-step="1" data-slider-value="1" data-slider-selection="after" />
</div>
<div id='map'></div> 



<script>  

       function main() {

        var map = new L.Map('map', {
            zoomControl: false,
            center: [38.9765, 35.3320],
            zoom: 5
        });

        L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.jpg', {
            attribution: 'CartoDB · OSM data'
        }).addTo(map);

        cartodb.createLayer(map, 'https://capanh.carto.com/api/v2/viz/94dd9018-e918-11e6-a6cf-0ef7f98ade21/viz.json', {
                legends: true
            })
            .addTo(map);

        $('#Myslider').slider();
        $("#Myslider").on("slide", function(slideEvt) {
            $("#SliderVal").text(slideEvt.value);
            if ($('#Myslider').val() == 1) {
                document.getElementById("demo").innerHTML = "Genel endeks";
            }
            if ($('#Myslider').val() == 2) {
                document.getElementById("demo").innerHTML = "Konut";
            }
            if ($('#Myslider').val() == 3) {
                document.getElementById("demo").innerHTML = "Çalışma hayatı";
            }
            if ($('#Myslider').val() == 4) {
                document.getElementById("demo").innerHTML = "Gelir ve servet";
            }
            if ($('#Myslider').val() == 5) {
                document.getElementById("demo").innerHTML = "Sağlık";
            }
            if ($('#Myslider').val() == 6) {
                document.getElementById("demo").innerHTML = "Eğitim";
            }
            if ($('#Myslider').val() == 7) {
                document.getElementById("demo").innerHTML = "Çevre";
            }
            if ($('#Myslider').val() == 8) {
                document.getElementById("demo").innerHTML = "Güvenlik";
            }
            if ($('#Myslider').val() == 9) {
                document.getElementById("demo").innerHTML = "Sivil katılım";
            }
            if ($('#Myslider').val() == 10) {
                document.getElementById("demo").innerHTML = "Altyapı hizmetlerine erişim";
            }
            if ($('#Myslider').val() == 11) {
                document.getElementById("demo").innerHTML = "Sosyal yaşam";
            }
            if ($('#Myslider').val() == 12) {
                document.getElementById("demo").innerHTML = "Yaşam memnuniyeti";
            }
        });

    }
    window.onload = main;
</script>