Leaflet加載OSM、MapBox地圖

先展示地圖加載的效果:


MapBox地圖
OSM地圖

(1)首先獲取Leaflet的資源包
Leaflet資源文件Url:http://leafletjs.com/download.html
Leaflet資源文件非常小,雖然輕但是麻雀雖小,五臟俱全。完全可以滿足基本的地圖操作和基礎(chǔ)的GIS功能。(Github開源社區(qū)有對(duì)Leaflet功能的更新,可以關(guān)注一下)
推薦大家使用Webstorm,直接引入Leaflet或openlayers資源文件,Webstrom識(shí)別資源文件其中的方法,編寫代碼有代碼提示提高編寫代碼的效率

(2)在WebStorm中新建工程,引入Leaflet資源文件。新建HTML頁面。


WebStorm引入資源文件

這里提供加載OSM和MapBox地圖的代碼:
加載OSM地圖:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Leaflet加載OSM地圖</title>
    <link rel="stylesheet" href="leaflet/leaflet.css">
    <script src="leaflet/leaflet.js"></script>
    <style type="text/css">
        #map,html,body{
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>
</body>
<script type="text/javascript">
    var map = new L.Map('map');
    var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
    var osm = new L.TileLayer(osmUrl, {minZoom: 5, maxZoom: 18});

    map.addLayer(osm);
    map.setView(new L.LatLng(31.864942016,117.2882028929),11);
</script>
</html>

加載MapBox地圖:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Leaflet加載MapBox地圖</title>
    <link rel="stylesheet" href="leaflet/leaflet.css">
    <script src="leaflet/leaflet.js"></script>
    <style type="text/css">
        #map,html,body{
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>
</body>
<script type="text/javascript">
    var map = L.map('map').setView([31.864942016,117.2882028929], 11);
    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
        maxZoom: 18,
        id: 'mapbox.streets'
    }).addTo(map);
</script>
</html>

代碼的寫法和Openlayers寫法相似(Url地址、縮放級(jí)別、中心點(diǎn)坐標(biāo))。

(3)Leaflet加載其他地圖效果:


ArcGIS底圖
Google地圖
高德地圖
天地圖
天地圖影像

添加底圖切換控件:

var baseLayers = {
    "地圖": normal,
    "影像": image,
}

L.control.layers(baseLayers).addTo(map);
L.control.zoom({
    zoomInTitle: '放大',
    zoomOutTitle: '縮小'
}).addTo(map);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容