概述:
在前文中分别讲到了在Arcgis for js、Openlayers2中去加载天地图,同时也讲到了天地图的离线加载方式。在本文,讲述在Openlayers3中实现在线/离线的天地图的加载。
实现:
直接贴代码吧,效果就不贴了:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>TDT</title> <link rel="stylesheet" type="text/css" href="../../../../plugin/ol3/css/ol.css"/> <style type="text/css"> body, #map { border: 0px; margin: 0px; padding: 0px; width: 100%; height: 100%; font-size: 13px; } #map{ background: url("../../images/bgImg.gif"); background-repeat: inherit; } </style> <script type="text/javascript" src="../../../../plugin/ol3/build/ol-debug.js"></script> <script type="text/javascript" src="../../../../plugin/jquery/jquery-1.8.3.js"></script> <script type="text/javascript"> var map; function init(){ var bounds = [73.4510046356223, 18.1632471876417, 134.976797646506, 53.5319431522236]; var projection = new ol.proj.Projection({ code: 'EPSG:4326', units: 'degrees' }); var vec_c = getTdtLayer("vec_c"); var cva_c = getTdtLayer("cva_c"); var wms = new ol.layer.Image({ source: new ol.source.ImageWMS({ ratio: 1, url: 'http://localhost:8088/geoserver/lzugis/wms', params: { 'FORMAT': 'image/png', 'VERSION': '1.1.1', LAYERS: 'lzugis:capital', STYLES: '' } }) }); var map = new ol.Map({ controls: ol.control.defaults({ attribution: false }), target: 'map', layers: [vec_c,cva_c,wms], view: new ol.View({ projection: projection, minZoom:2, maxZoom:8 }) }); map.getView().fitExtent(bounds, map.getSize()); } function getTdtLayer(lyr){ var url = "http://localhost:8081/lzugis/tdttile?T="+lyr+"&X={x}&Y={y}&L={z}"; // var url = "http://t0.tianditu.com/DataServer?T="+lyr+"&X={x}&Y={y}&L={z}";在线 var projection = ol.proj.get("EPSG:4326"); var projectionExtent = [ -180, -90, 180, 90 ]; var maxResolution = (ol.extent.getWidth(projectionExtent) / (256 * 2)); var resolutions = new Array(16); var z; for (z = 0; z < 16; ++z) { resolutions[z] = maxResolution / Math.pow(2, z); } var tileOrigin = ol.extent.getTopLeft(projectionExtent); var layer = new ol.layer.Tile({ extent: [ -180, -90, 180, 90 ], source: new ol.source.TileImage({ tileUrlFunction: function(tileCoord) { var z = tileCoord[0]+1; var x = tileCoord[1]; var y = -tileCoord[2]-1; var n = Math.pow(2, z + 1); x = x % n; if (x * n < 0) { x = x + n; } return url.replace('{z}', z.toString()) .replace('{y}', y.toString()) .replace('{x}', x.toString()); }, projection: projection, tileGrid: new ol.tilegrid.TileGrid({ origin: tileOrigin, resolutions: resolutions, tileSize: 256 }) }) }); return layer; } </script> </head> <body onLoad="init()"> <div id="map"> </div> </body> </html>