OpenLayers笔记–加载百度地图和添加比例尺
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”/>
- <title></title>
- <link rel=“stylesheet” type=“text/css” href=“css/ol.css” />
- <style type=“text/css”>
- body, #Map {
- border: 0px;
- margin: 0px;
- padding: 0px;
- width: 100%;
- height: 100%;
- font-size: 13px;
- }
- </style>
- <script type=“text/javascript” src=“js/ol-debug.js”></script>
- <script src=“js/ol.js” type=“text/javascript”></script>
- </head>
- <body>
- <div id=“Map”>
- </div>
- <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({
- 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: ‘Map’,
- layers: [baidu_layer],
- view: new ol.View({
- center: ol.proj.transform([116.405,39.727487], ‘EPSG:4326’, ‘EPSG:3857’),
- zoom: 12
- })
- });
- //加载比例尺控件
- var scaleLineControl = new ol.control.ScaleLine({
- //设置度量单位为米
- units: ‘metric’,
- target: ‘scalebar’,
- className: ‘ol-scale-line’
- });
- map.addControl(scaleLineControl);
- </script>
- </body>
- </html>
转载自:https://blog.csdn.net/lyd0902/article/details/79481701