map.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title></title>
  6. <script src="../map/jquery.min.js"></script>
  7. <script src="../map/ol.js"></script>
  8. <link href="../map/ol.css" rel="stylesheet" />
  9. <style type="text/css">
  10. *{
  11. border: none;
  12. padding: 0px;
  13. margin: 0px;
  14. }
  15. #map {
  16. border: 0px;
  17. margin: 0px;
  18. padding: 0px;
  19. width: 100vw;
  20. height: 100vh;
  21. font-size: 13px;
  22. }
  23. .mapOpen {
  24. position: absolute;
  25. bottom: 68px;
  26. left: -100px;
  27. width: 200px;
  28. border-radius: 2px;
  29. background-color: #fff;
  30. -webkit-filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.5));
  31. filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.5));
  32. }
  33. .mapOpen:after,
  34. .mapOpen:before {
  35. top: 100%;
  36. border: solid transparent;
  37. content: " ";
  38. height: 0;
  39. width: 0;
  40. position: absolute;
  41. pointer-events: none;
  42. }
  43. .mapOpen:after {
  44. border-top-color: #fff;
  45. border-width: 10px;
  46. left: 50%;;
  47. margin-left: -10px;
  48. }
  49. .mapOpen:before {
  50. border-top-color: #cccccc;
  51. border-width: 11px;
  52. left: 50%;;
  53. margin-left: -11px;
  54. }
  55. #mapTitle{
  56. padding: 6px 10px;
  57. font-weight: bold;
  58. font-size: 15px;
  59. color: #fff;
  60. background-color: #0068ff;
  61. }
  62. #mapCont{
  63. padding: 10px;
  64. }
  65. #mapCont>div{
  66. display: flex;
  67. align-items: flex-start;
  68. color: #474747;
  69. margin-bottom: 6px;
  70. }
  71. #mapCont>div>div:first-child {
  72. color: #0068ff;
  73. width: 50px;
  74. }
  75. .mapClose {
  76. text-decoration: none;
  77. position: absolute;
  78. cursor: pointer;
  79. top: 2px;
  80. right: 8px;
  81. font-size: 18px;
  82. font-weight: bold;
  83. color: #fff;
  84. }
  85. .mapClose:after {
  86. content: "✖";
  87. }
  88. .ol-zoom{
  89. left: auto;
  90. right: 2%;
  91. top: 7%;
  92. }
  93. .ol-control button{
  94. font-size: 20px;
  95. cursor: pointer;
  96. }
  97. .qlayer{
  98. position: absolute;
  99. right: 2%;
  100. top: 14%;
  101. padding: 0;
  102. cursor: pointer;
  103. color: #fff;
  104. font-weight: 700;
  105. text-decoration: none;
  106. font-size: 20px;
  107. text-align: center;
  108. height: 1.375em;
  109. width: 1.375em;
  110. line-height: 1.6em;
  111. background-color: rgba(0,60,136,.4);
  112. border: 3px solid rgba(255,255,255,0.7);
  113. border-radius: 2px;
  114. }
  115. .qlayer:hover{
  116. background-color: rgba(0,60,136,.6);
  117. }
  118. .qlayer img{
  119. width: 20px;
  120. }
  121. .layerOpen{
  122. display: none;
  123. position: absolute;
  124. left: 50%;
  125. top: 50%;
  126. font-size: 14px;
  127. transform: translate(-50%, -50%);
  128. background-color: #fff;
  129. box-shadow: 0 3px 2px rgba(0, 0, 0, 0.5);
  130. }
  131. .layerOpen .openTil{
  132. padding: 10px;
  133. text-align: center;
  134. color: #fff;
  135. background-color: #0068ff;
  136. }
  137. .layerOpen .openCon{
  138. display: flex;
  139. justify-content: space-around;
  140. width: 360px;
  141. padding: 15px 0;
  142. }
  143. .layerOpen .mapClose{
  144. top: 6px;
  145. font-weight: normal;
  146. }
  147. .mapImg.act{
  148. position: relative;
  149. overflow: hidden;
  150. border: 1px solid #1E9FFF;
  151. }
  152. .mapImg.act::before{
  153. content: "";
  154. position: absolute;
  155. bottom: 0;
  156. right:0;
  157. border-bottom: 30px solid #1E9FFF;
  158. border-left: 30px solid transparent;
  159. }
  160. .mapImg.act::after{
  161. content: "";
  162. position: absolute;
  163. bottom: 2px;
  164. right:1px;
  165. width: 14px;
  166. height: 14px;
  167. z-index: 444;
  168. background: url(../map/chead.png) no-repeat;
  169. background-size: 100%;
  170. }
  171. </style>
  172. </head>
  173. <body>
  174. <div id="map"></div>
  175. <!-- 图层切换 -->
  176. <div class="qlayer" title="图层切换" onclick="$('.layerOpen').css('display','block')"><img src="../map/layer.png" alt=""></div>
  177. <div class="layerOpen">
  178. <div class="openTil">
  179. <span>图层切换</span>
  180. <span class="mapClose"></span>
  181. </div>
  182. <div class="openCon">
  183. <div class="mapImg" data-type="nora"><img src="../map/mapNora.png" style="width: 140px;" alt=""></div>
  184. <div class="mapImg act" data-type="moon"><img src="../map/mapMoon.png" style="width: 140px;" alt=""></div>
  185. </div>
  186. </div>
  187. <script type="text/javascript">
  188. var gutter = 6, fontS = 21, nowTab = {};
  189. var newArr = [];
  190. var center = ol.proj.transform([118.26718,22.45402], "EPSG:4326", "EPSG:3857");
  191. var norMap = new ol.layer.Tile({
  192. visible: false,
  193. source: new ol.source.XYZ({
  194. url: 'http://t0.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=317e52a409b6b382957e09003ee7e235'
  195. }),
  196. isGroup: true,
  197. name: '天地图路网'
  198. });
  199. var moonMap = new ol.layer.Tile({
  200. visible: true,
  201. source: new ol.source.XYZ({
  202. url: 'http://t0.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=317e52a409b6b382957e09003ee7e235'
  203. }),
  204. isGroup: true,
  205. name: '卫星图路网'
  206. });
  207. var callMap = new ol.layer.Tile({
  208. visible: true,
  209. source: new ol.source.XYZ({
  210. url: 'http://t0.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=317e52a409b6b382957e09003ee7e235'
  211. }),
  212. isGroup: true,
  213. name: '天地图文字标注'
  214. });
  215. //初始化地图
  216. var map = new ol.Map({
  217. target: 'map',
  218. layers: [norMap,moonMap,callMap],
  219. view: new ol.View({
  220. center: center,
  221. zoom: 12,
  222. maxZoom: 18,
  223. minZoom: 2
  224. })
  225. });
  226. var createLabel = function (feature) {
  227. return new ol.style.Style({
  228. image: new ol.style.Icon({
  229. anchor: feature["A"] == null?[0,0]: (feature["A"].anchor == undefined?[0.5, 0.95]:feature["A"].anchor),
  230. anchorOrigin: 'top-right',
  231. scale: gutter*0.2,
  232. src: '../map/ship_tblr.png',
  233. rotation: (feature["A"] == null?0: (feature["A"].cog == undefined?0:feature["A"].cog))* (Math.PI / 180)
  234. })
  235. });
  236. }
  237. var checkFeature = new ol.Feature({});
  238. checkFeature.setStyle(createLabel(checkFeature));
  239. var vectorSources = new ol.source.Vector({features: [checkFeature]});
  240. var vectorLayers = new ol.layer.Vector({source: vectorSources});
  241. map.addLayer(vectorLayers);
  242. window.addEventListener('message',(res)=>{
  243. if(res.data.type == "changsn"){
  244. nowTab = res.data.nowSn;
  245. setMap();
  246. }else{
  247. gutter = res.data.gutter;
  248. fontS = res.data.fontS;
  249. nowTab = res.data.nowSn;
  250. getLi(res.data.shipLi);
  251. }
  252. })
  253. $(document).ready(function(){})
  254. function getLi(_obj){
  255. newArr = [];
  256. for (let i = 0; i < _obj.length; i++) {
  257. newArr[i] = _obj[i];
  258. newArr[i].ol = [];
  259. newArr[i].ol[0] = parseFloat(_obj[i].lon);
  260. newArr[i].ol[1] = parseFloat(_obj[i].lat);
  261. }
  262. setMap();
  263. }
  264. // 显示点标注
  265. function setMap(){
  266. for (let i = 0; i < newArr.length; i++) {
  267. let info = newArr[i];
  268. let iconFeature = new ol.Feature({
  269. geometry: new ol.geom.Point(ol.proj.fromLonLat(info.ol)),
  270. name: info.prod,
  271. key: info.mmsi,
  272. cog: info.cog,
  273. anchor: info.anchor
  274. });
  275. iconFeature.setStyle(createLabelStyle(iconFeature));
  276. let vectorLayer = new ol.layer.Vector({
  277. source: new ol.source.Vector({features: [iconFeature]})
  278. });
  279. map.addLayer(vectorLayer);
  280. if(info.mmsi == nowTab.mmsi){
  281. setOpen(info);
  282. }
  283. }
  284. }
  285. //创建标签的样式
  286. var createLabelStyle = function(feature) {
  287. return new ol.style.Style({
  288. image: new ol.style.Icon({
  289. anchor: feature["A"] == null?[0,0]: (feature["A"].anchor == undefined?[0.5, 1]:feature["A"].anchor),
  290. anchorOrigin: 'top-right',
  291. scale: gutter*0.2,
  292. src: '../map/ship.png',
  293. rotation: (feature["A"] == null?0: (feature["A"].cog == undefined?0:feature["A"].cog))* (Math.PI / 180)
  294. }),
  295. text: new ol.style.Text({
  296. textAlign: 'center',
  297. font: (fontS*0.6)+'px 微软雅黑',
  298. offsetX: 0,
  299. offsetY: 16,
  300. text: feature.get('name'),
  301. fill: new ol.style.Fill({
  302. color: '#000'
  303. }),
  304. backgroundFill: new ol.style.Fill({
  305. color: "rgba(255, 255, 255, 0.8)",
  306. }),
  307. padding: [2, 4, 2, 4]
  308. })
  309. });
  310. };
  311. // 点标注点击事件
  312. map.on('click', function(evt) {
  313. var feature = map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) { return feature; });
  314. if(feature != undefined){
  315. let _sn = feature.A.key;
  316. let _ind = newArr.findIndex((item,indx) => { return item.mmsi == _sn});
  317. setOpen(newArr[_ind]);
  318. window.parent.postMessage({nowSn: newArr[_ind].mmsi}, '*');
  319. }
  320. });
  321. // 显示弹框
  322. function setOpen(_obj){
  323. let _pos = ol.proj.transform([_obj.ol[0],_obj.ol[1]], "EPSG:4326", "EPSG:3857")
  324. vectorSources.clear();
  325. var newFeature = new ol.Feature({
  326. geometry: new ol.geom.Point(_pos),
  327. cog: _obj.cog,
  328. anchor: _obj.anchor
  329. });
  330. newFeature.setStyle(createLabel(newFeature));
  331. vectorSources.addFeature(newFeature);
  332. map.getView().animate({
  333. center: _pos,
  334. duration: 1000
  335. })
  336. }
  337. // 切换卫星图
  338. $(document).on("click",".layerOpen .mapImg",function(){
  339. $(".layerOpen .mapImg").removeClass("act");
  340. $(this).addClass("act");
  341. if($(this).data("type") == "nora"){
  342. norMap.setVisible(true);
  343. moonMap.setVisible(false);
  344. }else{
  345. norMap.setVisible(false);
  346. moonMap.setVisible(true);
  347. }
  348. $('.layerOpen').css('display','none');
  349. })
  350. // 切换卫星图关闭
  351. $(document).on("click",".layerOpen .mapClose",function(){
  352. $('.layerOpen').css('display','none')
  353. })
  354. </script>
  355. </body>
  356. </html>