2016-05-17 97 views
0

下面是我在我的瀏覽器中加載地圖時使用的HTML和JavaScript。 我在Mabbox.org創建了一張地圖。所以地圖的傳單網址是:MapBox URL無法正常工作

https://api.mapbox.com/styles/v1/johnmichel/ciobach7h0084b3nf482gfvvr/tiles/{z}/{x}/{y}?access_token=pk.eyJ1Ijoiam9obm1pY2hlbCIsImEiOiJjaW9iOW1vbHUwMGEzdnJseWNranhiMHpxIn0.leVOjMBazNl6v4h9MT7Glw 

現在我把這個URL放在一個名爲init()的Javascript函數中;我跟着這個http://leafletjs.com/examples/quick-start.html。但是當我加載HTML時,不會顯示地圖。你能幫我嗎?

的Javascript

function init(){ 

var map = L.map('map'); 

L.tileLayer('https://api.mapbox.com/styles/v1/johnmichel/ciobach7h0084b3nf482gfvvr/tiles/{z}/{x}/{y}?access_token=pk.eyJ1Ijoiam9obm1pY2hlbCIsImEiOiJjaW9iOW1vbHUwMGEzdnJseWNranhiMHpxIn0.leVOjMBazNl6v4h9MT7Glw', { 
    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 
}).addTo(map); 

    map = L.map('map').setView([46.2, 2], 5); 
} 

HTML

<!DOCTYPE html> 
<html> 
<head> 

    <title></title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 
    <link rel="stylesheet" type="text/css" href="monstyle.css"> 
    <link rel="stylesheet" type="text/css" href="leaflet/leaflet.css"> 
    <script src="leaflet/leaflet.js"></script> 
    <script src="MonFichierJS.js"></script> 
</head> 
<body onload="init()"> 

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

</body> 
</html> 

CSS

body { 
    padding: 0; 
    margin: 0; 
} 
html, body, #map { 
    height: 500px; 
width: 500px; 
} 
+0

出於測試目的,試着給你的'#map'像素代替% – chrki

+0

@chrki高度正如預期的那樣已經調整股利。我選擇了500px的高度和寬度。地圖仍然沒有顯示(我只看到縮放按鈕+和 - )。我編輯了我的OP來更改CSS –

回答

2

的問題是在使用Javascript。下面是一個正確的JS代碼

function init(){ 

var map = L.map('map').setView([46.2, 2], 5); 

L.tileLayer('https://api.mapbox.com/styles/v1/johnmichel/ciobach7h0084b3nf482gfvvr/tiles/{z}/{x}/{y}?access_token=pk.eyJ1Ijoiam9obm1pY2hlbCIsImEiOiJjaW9iOW1vbHUwMGEzdnJseWNranhiMHpxIn0.leVOjMBazNl6v4h9MT7Glw', { 
    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 
}).addTo(map); 

}