threejsLayer_facility.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="UTF-8" />
  8. <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
  9. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width" />
  10. <title data-i18n="resources.title_facility"></title>
  11. <style>
  12. html,
  13. body {
  14. margin: 0;
  15. padding: 0;
  16. height: 100%;
  17. width: 100%;
  18. position: relative;
  19. }
  20. #map {
  21. width: 100%;
  22. height: 100%;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="map"></div>
  28. <script type="text/javascript" include="jquery" src="../js/include-web.js"></script>
  29. <script
  30. type="text/javascript"
  31. include="three,MTLLoader,OBJLoader"
  32. src="../../dist/mapboxgl/include-mapboxgl.js"
  33. ></script>
  34. <script>
  35. var attribution =
  36. "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
  37. " with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
  38. " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
  39. var map,
  40. threeLayer,
  41. position = [116.466502, 39.990706];
  42. //https://iclient.supermap.io https://iserver.supermap.io
  43. var host = window.isLocal ? window.server : 'https://iserver.supermap.io',
  44. url = host + '/iserver/services/map-china400/rest/maps/ChinaDark';
  45. map = new mapboxgl.Map({
  46. container: 'map',
  47. style: {
  48. version: 8,
  49. sources: {
  50. 'raster-tiles': {
  51. attribution: attribution,
  52. type: 'raster',
  53. tiles: [url + '/zxyTileImage.png?z={z}&x={x}&y={y}'],
  54. tileSize: 256
  55. }
  56. },
  57. layers: [
  58. {
  59. id: 'simple-tiles',
  60. type: 'raster',
  61. source: 'raster-tiles',
  62. minzoom: 0,
  63. maxzoom: 22
  64. }
  65. ],
  66. glyphs:
  67. host +
  68. '/iserver/services/map-beijing/rest/maps/beijingMap/tileFeature/sdffonts/{fontstack}/{range}.pbf'
  69. },
  70. center: [116.46548484988489, 39.990389660427695],
  71. zoom: 15.91,
  72. bearing: 59.19,
  73. pitch: 60
  74. });
  75. map.addControl(new mapboxgl.NavigationControl(), 'top-left');
  76. //加载obj模型
  77. loaderModels();
  78. //添加设施点,路径以及标签
  79. map.on('load', function () {
  80. loadOverlays();
  81. });
  82. function loaderModels() {
  83. var mtlLoader = new THREE.MTLLoader(new THREE.LoadingManager());
  84. mtlLoader.load('./js/facility/building.mtl', function (materials) {
  85. materials.preload();
  86. var objLoader = new THREE.OBJLoader(new THREE.LoadingManager());
  87. objLoader.setMaterials(materials);
  88. objLoader.load(
  89. './js/facility/building.obj',
  90. function (obj) {
  91. // https://discourse.threejs.org/t/obj-mtl-model-is-loaded-but-is-black/18488
  92. obj.traverse(function (child) {
  93. if (child.isMesh) child.geometry.computeVertexNormals();
  94. });
  95. var meshes = obj;
  96. meshes.rotation.x = -Math.PI / 2;
  97. meshes.rotation.y = (1.4 * Math.PI) / 5;
  98. meshes.scale.multiplyScalar(3);
  99. addThreeLayer(meshes);
  100. },
  101. null,
  102. null,
  103. null,
  104. false
  105. );
  106. });
  107. }
  108. function addThreeLayer(meshes) {
  109. threeLayer = new mapboxgl.supermap.ThreeLayer('three');
  110. threeLayer.on('initialized', render);
  111. function render() {
  112. var renderer = threeLayer.getThreeRenderer(),
  113. scene = threeLayer.getScene(),
  114. camera = threeLayer.getCamera();
  115. this.light = new THREE.PointLight(0xffffff, 0.8);
  116. this.light.position.copy(camera.position);
  117. scene.add(this.light);
  118. scene.add(new THREE.AmbientLight(0xffffff));
  119. threeLayer.setPosition(meshes, position);
  120. scene.add(meshes);
  121. }
  122. //均匀光照,与相机位置同步
  123. threeLayer.on('render', function () {
  124. threeLayer.light.position.copy(threeLayer.renderer.camera.position);
  125. });
  126. threeLayer.addTo(map);
  127. }
  128. function loadOverlays() {
  129. var facilityPositions = {
  130. shopping: [116.462834, 39.991234],
  131. hospital: [116.467154, 39.995506],
  132. school: [116.46506, 39.992958]
  133. };
  134. var labelPositions = {
  135. shoppingText: [116.464668, 39.990965],
  136. hospitalText: [116.466828, 39.993106],
  137. schoolText: [116.465781, 39.991832]
  138. };
  139. //添加设施点
  140. var shoppingImg = new Image();
  141. shoppingImg.src = './js/facility/shopping.png';
  142. var shoppingMall = new mapboxgl.Marker(shoppingImg, { x: -32, y: -54 })
  143. .setLngLat(facilityPositions.shopping)
  144. .addTo(map);
  145. var hospitalImg = new Image();
  146. hospitalImg.src = './js/facility/hospital.png';
  147. var hospital = new mapboxgl.Marker(hospitalImg, { x: -32, y: -54 })
  148. .setLngLat(facilityPositions.hospital)
  149. .addTo(map);
  150. var schoolImg = new Image();
  151. schoolImg.src = './js/facility/school.png';
  152. var school = new mapboxgl.Marker(schoolImg, { x: -32, y: -54 })
  153. .setLngLat(facilityPositions.school)
  154. .addTo(map);
  155. //添加购物中心距离路径
  156. map.addLayer({
  157. id: 'shopping-route',
  158. type: 'line',
  159. source: {
  160. type: 'geojson',
  161. data: {
  162. type: 'Feature',
  163. properties: {},
  164. geometry: {
  165. type: 'LineString',
  166. coordinates: [position, facilityPositions.shopping]
  167. }
  168. }
  169. },
  170. layout: {
  171. 'line-join': 'round',
  172. 'line-cap': 'round'
  173. },
  174. paint: {
  175. 'line-color': '#0000ff',
  176. 'line-width': 5
  177. }
  178. });
  179. //添加医院距离路径
  180. map.addLayer({
  181. id: 'hospital-route',
  182. type: 'line',
  183. source: {
  184. type: 'geojson',
  185. data: {
  186. type: 'Feature',
  187. properties: {},
  188. geometry: {
  189. type: 'LineString',
  190. coordinates: [position, facilityPositions.hospital]
  191. }
  192. }
  193. },
  194. layout: {
  195. 'line-join': 'round',
  196. 'line-cap': 'round'
  197. },
  198. paint: {
  199. 'line-color': '#ff0000',
  200. 'line-width': 5
  201. }
  202. });
  203. //添加学校距离路径
  204. map.addLayer({
  205. id: 'school-route',
  206. type: 'line',
  207. source: {
  208. type: 'geojson',
  209. data: {
  210. type: 'Feature',
  211. properties: {},
  212. geometry: {
  213. type: 'LineString',
  214. coordinates: [position, facilityPositions.school]
  215. }
  216. }
  217. },
  218. layout: {
  219. 'line-join': 'round',
  220. 'line-cap': 'round'
  221. },
  222. paint: {
  223. 'line-color': '#00ff00',
  224. 'line-width': 5
  225. }
  226. });
  227. //添加购物中心距离提示标签
  228. map.addLayer({
  229. id: 'text-shopping',
  230. type: 'symbol',
  231. source: {
  232. type: 'geojson',
  233. data: {
  234. type: 'FeatureCollection',
  235. features: [
  236. {
  237. type: 'Feature',
  238. properties: { text: '购物320米' },
  239. geometry: {
  240. type: 'Point',
  241. coordinates: labelPositions.shoppingText
  242. }
  243. }
  244. ]
  245. }
  246. },
  247. paint: {
  248. 'text-halo-color': '#ffffff',
  249. 'text-halo-width': 2
  250. },
  251. layout: {
  252. 'text-field': '{text}'
  253. }
  254. });
  255. //添加医院距离提示标签
  256. map.addLayer({
  257. id: 'text-hospital',
  258. type: 'symbol',
  259. source: {
  260. type: 'geojson',
  261. data: {
  262. type: 'FeatureCollection',
  263. features: [
  264. {
  265. type: 'Feature',
  266. properties: { text: '医院550米' },
  267. geometry: {
  268. type: 'Point',
  269. coordinates: labelPositions.hospitalText
  270. }
  271. }
  272. ]
  273. }
  274. },
  275. paint: {
  276. 'text-halo-color': '#ffffff',
  277. 'text-halo-width': 2
  278. },
  279. layout: {
  280. 'text-field': '{text}'
  281. }
  282. });
  283. //添加学校距离提示标签
  284. map.addLayer({
  285. id: 'text-school',
  286. type: 'symbol',
  287. source: {
  288. type: 'geojson',
  289. data: {
  290. type: 'FeatureCollection',
  291. features: [
  292. {
  293. type: 'Feature',
  294. properties: { text: '学校300米' },
  295. geometry: {
  296. type: 'Point',
  297. coordinates: labelPositions.schoolText
  298. }
  299. }
  300. ]
  301. }
  302. },
  303. paint: {
  304. 'text-halo-color': '#ffffff',
  305. 'text-halo-width': 2
  306. },
  307. layout: {
  308. 'text-field': '{text}'
  309. }
  310. });
  311. //添加大厦名称标签
  312. map.addLayer({
  313. id: 'text-building',
  314. type: 'symbol',
  315. source: {
  316. type: 'geojson',
  317. data: {
  318. type: 'FeatureCollection',
  319. features: [
  320. {
  321. type: 'Feature',
  322. properties: { text: 'xxxx大厦' },
  323. geometry: {
  324. type: 'Point',
  325. coordinates: position
  326. }
  327. }
  328. ]
  329. }
  330. },
  331. paint: {
  332. 'text-translate': [0, -140],
  333. 'text-color': '#ffffff',
  334. 'text-halo-color': '#ff0000',
  335. 'text-halo-width': 2,
  336. 'text-translate-anchor': 'viewport'
  337. },
  338. layout: {
  339. 'text-field': '{text}'
  340. }
  341. });
  342. }
  343. </script>
  344. </body>
  345. </html>