Rhonda
Rhonda

Reputation: 1721

Leaflet, JS, Sample Code doesn't render map

I am following Leaflet JS tutorial and got account at Mapbox.

I use the following code, save as HTML, but instead of showing map, it shows the HTML code.

Really, I cannot figure what's wrong, it's simple code I copied and pasted (I only included my id and accessToken from Mapbox) ~~~ Thanks!

<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>

Updated working code

<!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>

Upvotes: 0

Views: 363

Answers (1)

tmcw
tmcw

Reputation: 11882

You'll need to close the div element and put your code in a script tag:

<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>

Upvotes: 1

Related Questions