1. 程式人生 > >OpenLayers筆記--載入百度地圖和新增比例尺

OpenLayers筆記--載入百度地圖和新增比例尺

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>
  5.     <title></title>
  6.     <linkrel="stylesheet"type="text/css"href="css/ol.css"/>
  7.     <styletype="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.     </style>
  17.     <scripttype="text/javascript"src="js/ol-debug.js"></script>
  18.     <scriptsrc="js/ol.js"type="text/javascript"></script>
  19. </head>
  20. <body
    >
  21.     <divid="Map">
  22.     </div>
  23.   <scripttype="text/javascript">
  24.     var projection = ol.proj.get("EPSG:3857");    
  25.     var resolutions = [];    
  26.     for (var i = 0; i <19; i++) {    
  27.         resolutions[i] = Math.pow(2, 18 - i);    
  28.     }    
  29.     var tilegrid = new ol.tilegrid.TileGrid({    
  30.         origin: [0, 0],    
  31.         resolutions: resolutions    
  32.     });    
  33.     var baidu_source = new ol.source.TileImage({    
  34.         projection: projection,    
  35.         tileGrid: tilegrid,    
  36.         tileUrlFunction: function (tileCoord, pixelRatio, proj) {    
  37.             if (!tileCoord) {    
  38.                 return "";    
  39.             }    
  40.             var z = tileCoord[0];    
  41.             var x = tileCoord[1];    
  42.             var y = tileCoord[2];    
  43.             if (x <0) {    
  44.                 x = "M" + (-x);    
  45.             }    
  46.             if (y <0) {    
  47.                 y = "M" + (-y);    
  48.             }    
  49.             return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1";    
  50.         }    
  51.     });    
  52.     var baidu_layer = new ol.layer.Tile({    
  53.         source: baidu_source    
  54.     });    
  55.     var map = new ol.Map({    
  56.         target: 'Map',    
  57.         layers: [baidu_layer],    
  58.         view: new ol.View({    
  59.             center: ol.proj.transform([116.405,39.727487], 'EPSG:4326', 'EPSG:3857'),    
  60.             zoom: 12    
  61.         })    
  62.     });   
  63.     //載入比例尺控制元件  
  64.     var scaleLineControl = new ol.control.ScaleLine({    
  65.             //設定度量單位為米    
  66.             units: 'metric',    
  67.             target: 'scalebar',    
  68.             className: 'ol-scale-line'    
  69.         });  
  70.         map.addControl(scaleLineControl);  
  71. </script>
  72. </body>
  73. </html>