openlayers3 application (1): call Baidu online map

    The most direct way to use Baidu map in the project is to use Baidu api , but to use Baidu api , you need to apply for a key , and some function calls are limited in number of times.

This article describes the method of using Baidu Maps in openlayers3 . Calling Baidu Maps has also gone through several twists and turns

Paste the display code to prevent others from calling Baidu Maps to take a detour. The effect is as follows:

 

 code show as below:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link rel="stylesheet" type="text/css" href="ol/ol3/css/ol.css" />
    <style type="text/css">
        body, #mainMap {
            border: 0px;
            margin: 0px;
            padding: 0px;
            width: 100%;
            height: 100%;
            font-size: 13px;
        }
    </style>
    <script type="text/javascript" src="ol/ol3/build/ol-debug.js"></script>

</head>
<body>

    <div id="mainMap">

    </div>

</body>
</html>
<script type="text/javascript">

    var projection = ol.proj.get("EPSG:3857");
    var resolutions = [];
    for (var i = 0; i < 19; i++) {
        resolutions[i] = Math.pow(2, 18 - i);
    }
    var tilegrid = new ol.tilegrid.TileGrid (ol
        origin: [0, 0],
        resolutions: resolutions
    });

    var baidu_source = new ol.source.TileImage({
        projection: projection,
        tileGrid: tilegrid,
        tileUrlFunction: function (tileCoord, pixelRatio, proj) {
            if (!tileCoord) {
                return "";
            }
            var z = tileCoord [0];
            var x = tileCoord[1];
            var y = tileCoord[2];

            if (x < 0) {
                x = "M" + (-x);
            }
            if (y < 0) {
                y = "M" + (-y);
            }

            return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1";
        }
    });

    var baidu_layer = new ol.layer.Tile({
        source: baidu_source
    });

    var map = new ol.Map ({
        target: 'mainMap',
        controls: ol.control.defaults().extend([
               new ol.control.MousePosition({ projection: 'EPSG:4326' })
        ]),
        layers: [baidu_layer],
        view: new ol.View({
            center: ol.proj.transform([104.06, 30.67], 'EPSG:4326', 'EPSG:3857'),
            zoom: 4
        })
    });
</script>

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326617373&siteId=291194637