Leaflet,JS,示例代码不呈现地图

时间:2015-07-22 19:55:22

标签: javascript leaflet openstreetmap mapbox

我正在关注Leaflet JS tutorial并在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>

1 个答案:

答案 0 :(得分:1)

您需要关闭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>