h10_link_leaflet.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  6. <meta name="author" content="火星科技 http://mars3d.cn " />
  7. <meta name="apple-touch-fullscreen" content="yes" />
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <meta name="format-detection" content="telephone=no" />
  11. <meta name="x5-fullscreen" content="true" />
  12. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  13. <!-- 标题及搜索关键字 -->
  14. <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  15. <meta
  16. name="description"
  17. content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型 gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包 地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
  18. />
  19. <link rel="shortcut icon" type="image/x-icon" href="" />
  20. <title>二三维联动 (leaflet+cesium) </title>
  21. <!--第三方lib-->
  22. <script
  23. type="text/javascript"
  24. src="../lib/include-lib.js"
  25. libpath="../lib/"
  26. include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars2d,mars3d"
  27. ></script>
  28. <link href="css/style.css" rel="stylesheet" />
  29. <style>
  30. #centerDiv {
  31. position: absolute;
  32. top: 0;
  33. left: 0;
  34. height: 100%;
  35. width: 100%;
  36. margin: 0;
  37. padding: 0;
  38. }
  39. #centerDiv3D {
  40. position: absolute;
  41. top: 0;
  42. right: 0;
  43. height: 100%;
  44. width: 100%;
  45. margin: 0;
  46. padding: 0;
  47. }
  48. /**cesium工具栏位置*/
  49. .cesium-viewer-toolbar {
  50. top: auto;
  51. left: auto;
  52. right: 5px;
  53. bottom: 5px;
  54. }
  55. .cesium-viewer-toolbar > .cesium-toolbar-button,
  56. .cesium-navigationHelpButton-wrapper,
  57. .cesium-viewer-geocoderContainer {
  58. margin-bottom: 5px;
  59. float: right;
  60. clear: both;
  61. }
  62. .cesium-baseLayerPicker-dropDown {
  63. bottom: 0;
  64. right: 40px;
  65. max-height: 700px;
  66. margin-bottom: 5px;
  67. }
  68. .cesium-navigation-help {
  69. top: auto;
  70. bottom: 0;
  71. right: 40px;
  72. transform-origin: right bottom;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <div id="centerDiv">
  78. <div id="map2d" class="mars2d-container"></div>
  79. </div>
  80. <div id="centerDiv3D">
  81. <div id="mars3dContainer" class="mars3d-container"></div>
  82. </div>
  83. <!-- 面板 -->
  84. <div class="infoview" style="left: 43%; top: 2px">
  85. <input type="button" class="btn btn-primary" value="分屏" onclick="mapManager.viewTo23D();" />
  86. <input type="button" class="btn btn-primary" value="仅2D" onclick="mapManager.viewTo2d();" />
  87. <input type="button" class="btn btn-primary" value="仅3D" onclick="mapManager.viewTo3d();" />
  88. </div>
  89. <script src="./js/common.js"></script>
  90. <script type="text/javascript">
  91. "use script"; //开发环境建议开启严格模式
  92. //最佳的二三维联动是:https://github.com/openlayers/ol-cesium
  93. var map3d;
  94. function initMap(options) {
  95. //合并属性参数,可覆盖config.json中的对应配置
  96. var mapOptions = mars3d.Util.merge(options, {});
  97. //创建三维地球场景
  98. map3d = new mars3d.Map("mars3dContainer", mapOptions);
  99. haoutil.loading.show();
  100. //读取 config.json 配置文件
  101. var configUrl = "http://mars2d.cn/example/config/config.json";
  102. return mars2d.axios
  103. .get(configUrl)
  104. .then(function (response) {
  105. //构建地图
  106. mapManager.createMap2D("map2d", response.data.map);
  107. mapManager.bind3D(map3d);
  108. mapManager.viewTo23D(); //默认
  109. haoutil.loading.hide();
  110. })
  111. .catch(function (error) {
  112. console.log(error);
  113. haoutil.alert(error && error.message, "出错了");
  114. });
  115. }
  116. //二三维地图联动控制
  117. var mapManager = {
  118. map2d: null, //leaflet map 对象
  119. map3d: null, //cesium map3d对象
  120. createMap2D: function (id, mapOptions) {
  121. //创建地图
  122. this.map2d = new mars2d.Map(id, mapOptions);
  123. this.bind2dEvent(this.map2d);
  124. },
  125. bind3D: function (map3d) {
  126. this.map3d = map3d;
  127. this.bind3dEvent();
  128. this._map_extentChangeHandler();
  129. },
  130. viewTo2d: function () {
  131. $("#centerDiv3D").hide();
  132. $("#centerDiv").css({ width: "100%" }).show();
  133. if (this.map2d) {
  134. this.map2d.invalidateSize(false);
  135. }
  136. },
  137. viewTo3d: function () {
  138. $("#centerDiv").hide();
  139. $("#centerDiv3D").css({ width: "100%" }).show();
  140. },
  141. viewTo23D: function () {
  142. $("#centerDiv").css({ width: "50%" }).show();
  143. $("#centerDiv3D").css({ width: "50%" }).show();
  144. if (this.map2d) {
  145. this.map2d.invalidateSize(false);
  146. }
  147. },
  148. //二维地图变化事件
  149. bind2dEvent: function () {
  150. this.map2d.on("drag", this._map_extentChangeHandler, this);
  151. this.map2d.on("zoomend", this._map_extentChangeHandler, this);
  152. },
  153. unbind2dEvent: function () {
  154. this.map2d.off("drag", this._map_extentChangeHandler, this);
  155. this.map2d.off("zoomend", this._map_extentChangeHandler, this);
  156. },
  157. _map_extentChangeHandler: function (e) {
  158. var bounds = this.map2d.getBounds();
  159. var extent = {
  160. xmin: bounds.getWest(),
  161. xmax: bounds.getEast(),
  162. ymin: bounds.getSouth(),
  163. ymax: bounds.getNorth(),
  164. };
  165. console.log(`'二维地图变化了,区域: ${JSON.stringify(extent)} `);
  166. this.unbind3dEvent();
  167. this.map3d.camera.setView({
  168. destination: Cesium.Rectangle.fromDegrees(extent.xmin, extent.ymin, extent.xmax, extent.ymax),
  169. });
  170. this.bind3dEvent();
  171. },
  172. //三维地图相机移动结束事件
  173. bind3dEvent: function () {
  174. this.map3d.on(mars3d.EventType.cameraChanged, this.camera_moveEndHandler, this);
  175. },
  176. unbind3dEvent: function () {
  177. this.map3d.off(mars3d.EventType.cameraChanged, this.camera_moveEndHandler, this);
  178. },
  179. camera_moveEndHandler: function (e) {
  180. // 范围对象
  181. var point = this.map3d.getCenter();
  182. var level = this.map3d.level;
  183. console.log(`'三维地图变化了,位置: ${point.toString()},层级 ${level} `);
  184. this.unbind2dEvent();
  185. this.map2d.setView([point.lat, point.lng], level, { animate: false });
  186. this.bind2dEvent();
  187. },
  188. };
  189. </script>
  190. </body>
  191. </html>