MapBox載入GeoServer釋出的WMS地圖服務

gisuuser發表於2019-03-08

使用MapBox載入GeoServer釋出的WMS地圖服務,測試一下成功,但是發現好像不支援4326座標系,只支援3857座標系,最終沒有找到怎麼載入4326座標系的WMS服務。載入程式碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MapBox載入WMS地圖服務</title>
    <script src='https://api.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
        /*隱藏logo*/
        .mapboxgl-ctrl.mapboxgl-ctrl-attrib{
            display: none !important;
        }
        .mapboxgl-ctrl-logo{
            display: none !important;
        }
    </style>
</head>
<body>
<div id='map'></div>
<script >
    mapboxgl.accessToken = 'pk.eyJ1IjoiaGFtYnVnZXJkZXZlbG9wIiwiYSI6ImNqNXJtZjF4ZzB3em4yd21pZmVqbHlleDAifQ.I9eqVjtotz7jaU7XcJm9pQ';
    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v10',
        center: [108.438,34.431],
        zoom: 7
    });
    map.on("load",function () {
        map.addLayer({
            'id': 'wms-test-layer',
            'type': 'raster',
            'source': {
                'type': 'raster',
                'tiles': [
                    "http://localhost:8080/geoserver/word/wms?service=WMS&version=1.1.0&request=GetMap&layers=word:town3857&styles=&bbox={bbox-epsg-3857}&width=256&height=256&srs=EPSG:3857&format=image/png&TRANSPARENT=TRUE"
                ],
                'tileSize': 256
            },
            'paint': {}
        });
    })
</script>
</body>
</html>

效果圖如下:

需要注意的是URL,連線WMS中的bbox引數用{bbox-epsg-3857}替換了,載入的時候,MapBox會自動替換成對應的引數,這樣地圖就可以載入了

相關文章