Leaflet、JS、示例代码不渲染地图

Leaflet, JS, Sample Code doesn't render map

我正在关注 Leaflet JS tutorial and got account at Mapbox

我使用以下代码,另存为 HTML,但显示的不是地图,而是 HTML 代码。

真的,我不知道哪里出了问题,这是我复制和粘贴的简单代码(我只包含了我的 id 和来自 Mapbox 的 accessToken)~~~ 谢谢!

<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<div id="map">

#map { height: 180px; }
var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
    maxZoom: 18,
    id: sonihal.9b051ec5,
    accessToken: pk.eyJ1Ijoic29uaWhhbCIsImEiOiIxNGViNGQ1YjdkZTgyNDM2OGY2ZTFmYzRiYzVmODgwYyJ9.hvFFPqS5Mltym7RhKYwLNg
}).addTo(map);

</div>

更新了工作代码

<!DOCTYPE html>
<html>
<head>
    <title>Leaflet Quick Start Guide Example</title>
    <meta charset="utf-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="http://leafletjs.com/dist/leaflet.css" />

</head>
<body>
    <div id="map" style="width: 600px; height: 400px"></div>

    
    <script src="http://leafletjs.com/dist/leaflet.js"></script>
    <script>

        var map = L.map('map').setView([51.505, -0.09], 13);

        L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
    maxZoom: 18,
    id: 'sonihal.9b051ec5',
    accessToken: 'pk.eyJ1Ijoic29uaWhhbCIsImEiOiIxNGViNGQ1YjdkZTgyNDM2OGY2ZTFmYzRiYzVmODgwYyJ9.hvFFPqS5Mltym7RhKYwLNg'
}).addTo(map);

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

您需要关闭 div 元素并将您的代码放入脚本标记中:

<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<div id="map"></div>

<script>
#map { height: 180px; }
var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
    maxZoom: 18,
    id: sonihal.9b051ec5,
    accessToken: pk.eyJ1Ijoic29uaWhhbCIsImEiOiIxNGViNGQ1YjdkZTgyNDM2OGY2ZTFmYzRiYzVmODgwYyJ9.hvFFPqS5Mltym7RhKYwLNg
}).addTo(map);

</script>