在Leaflet弹出窗口中显示图像不起作用

时间:2015-11-01 23:36:57

标签: javascript image popup leaflet

我正试图在this example here.之后的传单地图上的弹出窗口中显示图像。我想我的格式设置相同,但我不确定它为什么没有显示。

我做错了什么?

示例我正在关注:

marker1.bindPopup( "<img src=" + icon_url + "/> Current temperature in " + location + " is: " + temp_f)

HTML:

<body>
    <div id="wrapper">
        <div id="content">
            <h1>DC Area Parks</h1>
            <p>Parks in the DC area that I have visited since creating this page.</p>
            <br>
            <div id="map"></div>
        </div>
    </div>
</body>

CSS:

#wrapper {
    padding-right: 10px;
    padding-left: 10px;
    margin: 0 auto;
    width: 80%;
    border-right: 1px solid black;
    border-left: 1px solid black;
    height: 100vh;
    box-shadow: 5px 5px 10px 5px gray;
    margin-top: none;
    padding-top: none;
    text-align: center;
}

#map {
    height: 300px;
    width: 80%;
    border: 1px solid black;
    box-shadow: 2px 2px 4px 1px black;
    margin: 0 auto;
}

JavaScript的:

$(document).ready(function(){
    console.log("The JavaScript has loaded");
    var map = L.map('map',{ center: [38.907192, -77.036871], zoom: 9});
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap' }).addTo(map);
    //Potomac Overlook Park
    var potomacOverlookPark = L.marker([38.990175, -77.165271]).addTo(map);
    potomacOverlookPark.bindPopup("Potomac Overlook Park <br> The second line <br> <img src=" + images/PotOvePar.jpg + "/>");
});

1 个答案:

答案 0 :(得分:1)

在示例中,icon_url不是用于粘贴图像路径的注释,而是变量的示例,其值是表示该路径的字符串。

因此,在您的情况下,您将执行以下操作之一:

// Directly write your image src in the popup html string.
potomacOverlookPark.bindPopup("Potomac Overlook Park <br> The second line <br> <img src='images/PotOvePar.jpg'/>");

或者:

// Assign the path to a variable. Then it looks closer to your example:
var icon_url = "images/PotOvePar.jpg";
potomacOverlookPark.bindPopup("Potomac Overlook Park <br> The second line <br> <img src=" + icon_url + "/>");