OpenLayers笔记–加载百度地图和添加比例尺

  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4. <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”/>    
  5.     <title></title>    
  6.     <link rel=“stylesheet” type=“text/css” href=“css/ol.css” />    
  7.     <style type=“text/css”>    
  8.         body, #Map {    
  9.             border: 0px;    
  10.             margin: 0px;    
  11.             padding: 0px;    
  12.             width: 100%;    
  13.             height: 100%;    
  14.             font-size: 13px;    
  15.         }   
  16.           
  17.     </style>    
  18.     <script type=“text/javascript” src=“js/ol-debug.js”></script>  
  19.     <script src=“js/ol.js” type=“text/javascript”></script>  
  20. </head>    
  21. <body>    
  22.     
  23.     <div id=“Map”>    
  24.            
  25.     </div>    
  26.   <script type=“text/javascript”>    
  27.     
  28.     var projection = ol.proj.get(“EPSG:3857”);    
  29.     var resolutions = [];    
  30.     for (var i = 0; i < 19; i++) {    
  31.         resolutions[i] = Math.pow(2, 18 – i);    
  32.     }    
  33.     var tilegrid = new ol.tilegrid.TileGrid({    
  34.         origin: [0, 0],    
  35.         resolutions: resolutions    
  36.     });    
  37.     
  38.     var baidu_source = new ol.source.TileImage({    
  39.         projection: projection,    
  40.         tileGrid: tilegrid,    
  41.         tileUrlFunction: function (tileCoord, pixelRatio, proj) {    
  42.             if (!tileCoord) {    
  43.                 return “”;    
  44.             }    
  45.             var z = tileCoord[0];    
  46.             var x = tileCoord[1];    
  47.             var y = tileCoord[2];    
  48.             if (x < 0) {    
  49.                 x = “M” + (-x);    
  50.             }    
  51.             if (y < 0) {    
  52.                 y = “M” + (-y);    
  53.             }    
  54.             return “http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=” + x + “&y=” + y + “&z=” + z + “&styles=pl&udt=20151021&scaler=1&p=1“;    
  55.         }    
  56.     });    
  57.     
  58.     var baidu_layer = new ol.layer.Tile({    
  59.         source: baidu_source    
  60.     });    
  61.     
  62.     var map = new ol.Map({    
  63.         target: ‘Map’,    
  64.         layers: [baidu_layer],    
  65.         view: new ol.View({    
  66.             center: ol.proj.transform([116.405,39.727487], ‘EPSG:4326’, ‘EPSG:3857’),    
  67.             zoom: 12    
  68.         })    
  69.     });   
  70.     //加载比例尺控件  
  71.     var scaleLineControl = new ol.control.ScaleLine({    
  72.             //设置度量单位为米    
  73.             units: ‘metric’,    
  74.             target: ‘scalebar’,    
  75.             className: ‘ol-scale-line’    
  76.         });  
  77.         map.addControl(scaleLineControl);  
  78.           
  79. </script>  
  80. </body>    
  81. </html>    
  82.     

转载自:https://blog.csdn.net/lyd0902/article/details/79481701

You may also like...