Bootstrap下拉菜单按钮未与Leaflet映射一起显示

时间:2018-12-04 22:12:03

标签: javascript html css bootstrap-4 leaflet

我正在尝试使用Bootstrap按钮创建一个leafletJS Map。我将地图全屏显示,并将按钮放在其顶部。这是代码:

<!DOCTYPE html>
<html>
<head>

    <title>Map</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="static/bootstrap/js/bootstrap.min.js"></script>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="shortcut icon" type="image/x-icon" href="static/favicon.ico" />

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
    <script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>
    <link rel="stylesheet" media="screen" href ="static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="static/bootstrap/css/bootstrap-theme.min.css">

    <style>
        body {
            padding: 0;
            margin: 0;
        }
        html, body, #map {
            height: 100%;
            width: 100%;
        }

        #back_button {
          position: absolute;
          top: 20px;
          left: 20px;
          padding: 10px;
          z-index: 1001;
        }

        #drop_button {
          position: absolute;
          top: 80px;
          left: 20px;
          padding: 10px;
          z-index: 1001;
        }
    </style>


</head>
<body>
    <div id='map'>
    </div>

    <form  action="/" method="post" role="form">
        <button type="submit" class="btn btn-default" id="back_button"><- Back</button>
    </form>

    <div class="dropdown">
      <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" id="drop_button">Dropdown Example
      <span class="caret"></span></button>
      <ul class="dropdown-menu">
        <li><a href="#">HTML</a></li>
        <li><a href="#">CSS</a></li>
        <li><a href="#">JavaScript</a></li>
      </ul>
    </div>
<script>
    var map = L.map('map', {zoomControl:false}).setView([47.57768554635565,-122.16660887002944], 13);

    var coordinate = {{list_latlong | tojson | safe}};
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    var logoIcon = L.icon({
        iconUrl: './static/picture.png',
        iconSize:     [22, 22], // size of the icon
        iconAnchor:   [11, 22], // point of the icon which will correspond to marker's location
    });

    var i;
    for (i = 0; i < coordinate.length; i++) {
        var point = coordinate[i]
        var text = "";
        text = "<dl><dt>Number: " + point[2] + "</dt>"
        + "<dt>" + point[5] + "</dt>"
        + "<dt>" + point[6] + ", " + point[7] + ", " + point[8] + "</dt>"
        +"</dl>"
        L.marker([point[3],point[4]], {icon: logoIcon}).bindPopup(text).addTo(map);
    }


</script>
</body>
</html>

运行此代码将仅显示“返回”按钮,而没有下拉按钮。如果我删除

<link rel="stylesheet" media="screen" href ="static/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="static/bootstrap/css/bootstrap-theme.min.css">

显示了下拉按钮和后退按钮,但没有显示下拉菜单中的元素。

2 个答案:

答案 0 :(得分:1)

我已为您修复了一个以上的原因,首先,引导链接错误,其次,您在错误的id #dropdown按钮上添加了错误的样式,因此该按钮显示在页脚中,但您却没有没看到,这是完整的代码,其中编辑了一些文件的位置和url,并从#dropdown到父div .dropdown更新了新样式

<!DOCTYPE html>
<html>
<head>

    <title>Map</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" type="image/x-icon" href="static/favicon.ico" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"/>

    <style>
        body {
            padding: 0;
            margin: 0;
        }
        html, body, #map {
            height: 100%;
            width: 100%;
        }

        #back_button {
          position: absolute;
          top: 20px;
          left: 20px;
          padding: 10px;
          z-index: 1001;
        }
        .dropdown {
          position: absolute;
          top: 80px;
          left: 20px;
          z-index: 1001;
        }
    </style>
</head>
<body>
    <div id='map'>
    </div>

    <form  action="/" method="post" role="form">
        <button type="submit" class="btn btn-default" id="back_button"><- Back</button>
    </form>
    <div class="dropdown">
      <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" id="drop_button">Dropdown Example
      <span class="caret"></span></button>
      <ul class="dropdown-menu">
        <li><a href="#">HTML</a></li>
        <li><a href="#">CSS</a></li>
        <li><a href="#">JavaScript</a></li>
      </ul>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
    var map = L.map('map', {zoomControl:false}).setView([47.57768554635565,-122.16660887002944], 13);

    var coordinate = {{list_latlong | tojson | safe}};
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    var logoIcon = L.icon({
        iconUrl: './static/picture.png',
        iconSize:     [22, 22], // size of the icon
        iconAnchor:   [11, 22], // point of the icon which will correspond to marker's location
    });

    var i;
    for (i = 0; i < coordinate.length; i++) {
        var point = coordinate[i]
        var text = "";
        text = "<dl><dt>Number: " + point[2] + "</dt>"
        + "<dt>" + point[5] + "</dt>"
        + "<dt>" + point[6] + ", " + point[7] + ", " + point[8] + "</dt>"
        +"</dl>"
        L.marker([point[3],point[4]], {icon: logoIcon}).bindPopup(text).addTo(map);
    }


</script>
</body>
</html>

答案 1 :(得分:0)

<script src="static/bootstrap/js/bootstrap.min.js"></script>

这是引导脚本文件,用于使菜单和其他依赖于js的高级功能正常工作。但是,为了使其正常工作,需要具有一个DOM(文档对象模型)来工作。您当前拥有的是脚本在jQuery之后的顶部触发,没有任何DOM元素可使用(菜单元素尚未解析!)。

在关闭</body>标签之前,请尝试将其放置在现场。那应该使菜单元素起作用。