javascript - 无法加载资源 : the server responded with a status of 401 for mapbox api

标签 javascript html css leaflet

我已遵循传单“get started”教程

但我在尝试加载图 block 时遇到 401 错误

https://api.tiles.mapbox.com/v4/your.mapbox.project.id/13/4093/2724.png?access_token=your.mapbox.public.access.token Failed to load resource: the server responded with a status of 401 (Unauthorized)

我错过了什么?

我的html

<html>

<head>
  <title></title>
  <link rel="stylesheet" href="elad_map.css" />
  <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
</head>

<body>
  <div id="map"></div>

  <script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
  <script type="text/javascript" src="elad_map.js"></script>
</body>

</html>

document.onload = loadMap();

function loadMap() {
  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: 'your.mapbox.project.id',
    accessToken: 'your.mapbox.public.access.token'
  }).addTo(map);


  var circle = L.circle([51.508, -0.11], 500, {
    color: 'red',
    fillColor: '#f03',
    fillOpacity: 0.5
  }).addTo(map);

  var polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
  ]).addTo(map);

}

最佳答案

什么忘记换掉 idaccessToken:

id: 'your.mapbox.project.id',
accessToken: 'your.mapbox.public.access.token'

您需要将 accessToken 更改为在注册 Mapbox 时获得的那个,并将 id 更改为 mapbox 元素 ID,例如 mapbox.streetsmapbox-outdoorsmapbox-satellite 或通过 Mapbox Studio Classic 创建自定义 map 时获得的自定义 ID。

关于javascript - 无法加载资源 : the server responded with a status of 401 for mapbox api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35060895/

相关文章:

html - 图像不在较大的卡片 div 中居中

html - 为什么按钮(还有图像)倾向于隐藏在文本行下方?

IE8 中的 CSS 圆 Angular

jquery - 等到一个 Action 结束再开始另一个 Action

javascript - 如何使幻灯片仅在第一次单击时向左移动并在第一次单击时拒绝向左移动?

javascript - IE 中的鼠标位置

html - 在另一个 HTML 页面中加载 HTML 页面

html - 带阴影的内部透明箭头

javascript - AngularJS 在数组中搜索过滤器到对象中

javascript - 动画 <tr> 的大小