0

I have just started working with LeafLetJs and to get start with it I tried to implement Quick Start Guide From LeafLetJs : https://leafletjs.com/examples/quick-start/

I followed the instruction and included CSS, JS links and added a div.

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>



 <!-- Make sure you put this AFTER Leaflet's CSS -->
 <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>



<div id="mapid"></div>

Then on CSS side Included this CSS:

#mapid { height: 180px; }

Then in the JavaScript section:

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

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

This returns a grey screen. and in the console it gives multiple 401 UnAuthorized error. One of them is mentioned as below:

 https://api.tiles.mapbox.com/v4/mapbox.streets/11/1022/681.png?access_token={accesstoken} 401 (Unauthorized)

I have studied the other question on StackOverflow but that question was asked 5 years ago and I have tried the solutions but nothing worked. Link to Previous Question: leaflet map shows up grey

Link To Jsfiddle: https://jsfiddle.net/saadzr/3aro8eu2/2/

Any help will be appreciated.

kboul
  • 13,836
  • 5
  • 42
  • 53
Saad Khan
  • 193
  • 1
  • 2
  • 9

1 Answers1

1

Just remove the curly braces {} from the L.tileLayer url

Use this access_token=your_access_token

instead of this:

access_token={your_access_token}

where your_access_token equals to the mapbox access token you have.

and you should be fine.

Updated Demo

kboul
  • 13,836
  • 5
  • 42
  • 53