home_map1.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * Created by Wandergis on 2015/7/8.
  3. * 提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
  4. */
  5. var map, geoc;
  6. $.ajax({
  7. type: "post",
  8. url: "user_location",
  9. data: {
  10. // req: "cbd",
  11. },
  12. dataType: "json",
  13. success: function (data) {
  14. //百度地图获取坐标
  15. map = new BMap.Map("allmap", { mapType: BMAP_HYBRID_MAP });// 创建地图实例
  16. // map.centerAndZoom(new BMap.Point(113.64964385, 34.75661006), 13);// 创建点坐标
  17. var parentUserName = $('#parentUserName', parent.document).html();
  18. if (parentUserName == '罗普特') {
  19. map.centerAndZoom(new BMap.Point(112.55, 37.87), 13);// 创建点坐标
  20. } else if (data.user_pro != '' || data.user_city != '' ||data.user_area != '') {
  21. if(data.user_area){
  22. map.centerAndZoom(data.user_area,11);
  23. }else if(data.user_city){
  24. map.centerAndZoom(data.user_city,11);
  25. }else{
  26. map.centerAndZoom(data.user_pro,11);
  27. }
  28. }else{
  29. map.centerAndZoom(new BMap.Point(113.64964385, 34.75661006), 13);// 创建点坐标
  30. }
  31. map.enableScrollWheelZoom(true);
  32. map.addControl(new BMap.NavigationControl()); // 添加平移缩放控件
  33. map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
  34. map.addControl(new BMap.OverviewMapControl()); //添加缩略地图控件
  35. map.enableScrollWheelZoom(); //启用滚轮放大缩小
  36. map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
  37. // map.setMapStyle({style:'midnight'});
  38. map.addEventListener("tilesloaded", function () {
  39. $(".BMap_cpyCtrl").hide();
  40. // $(".anchorBL").hide();
  41. });//地图加载完毕后 隐藏百度图标
  42. geoc = new BMap.Geocoder();
  43. if ($('.content').height() > 750) {
  44. $('#iframe-main', parent.document).css('height', $('.content').height() + 20)
  45. } else {
  46. $('#iframe-main', parent.document).css('height', '750px')
  47. }
  48. }
  49. });
  50. // //百度地图获取坐标
  51. // var map = new BMap.Map("allmap", { mapType: BMAP_HYBRID_MAP });// 创建地图实例
  52. // // map.centerAndZoom(new BMap.Point(113.64964385, 34.75661006), 13);// 创建点坐标
  53. // var parentUserName = $('#parentUserName', parent.document).html();
  54. // if (parentUserName == '罗普特') {
  55. // map.centerAndZoom(new BMap.Point(112.55, 37.87), 13);// 创建点坐标
  56. // } else {
  57. // map.centerAndZoom(new BMap.Point(113.64964385, 34.75661006), 13);// 创建点坐标
  58. // }
  59. // map.enableScrollWheelZoom(true);
  60. // map.addControl(new BMap.NavigationControl()); // 添加平移缩放控件
  61. // map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
  62. // map.addControl(new BMap.OverviewMapControl()); //添加缩略地图控件
  63. // map.enableScrollWheelZoom(); //启用滚轮放大缩小
  64. // map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
  65. // // map.setMapStyle({style:'midnight'});
  66. // var stCtrl = new BMap.PanoramaControl(); //全景图
  67. // stCtrl.setOffset(new BMap.Size(20, 20));
  68. // map.addControl(stCtrl);
  69. var pointArray = [];
  70. window.onload = function () {
  71. var ele = $('#scdlist').children();
  72. var num = 0;
  73. var currjson = {};
  74. for(var i = 0;i<ele.length;i++){
  75. num++;
  76. if(num == 1){
  77. currjson.equip_id = $(ele[i]).html()
  78. }
  79. if(num == 2){
  80. currjson.lat = $(ele[i]).html()
  81. }
  82. if(num == 3){
  83. currjson.lng = $(ele[i]).html()
  84. }
  85. if(num>3){
  86. console.log(currjson.lng)
  87. console.log(currjson.lat)
  88. console.log(currjson.equip_id)
  89. addArea(currjson.lng,currjson.lat,2,"equip_name",currjson.equip_id,'')
  90. currjson = {}
  91. num = 0;
  92. }
  93. console.log($(ele[0]).html())
  94. console.log($(ele[i]).html())
  95. }
  96. //请求数据:
  97. // $.ajax({
  98. // type: "post",
  99. // url: "home_map/map_locat",
  100. // data: {
  101. // req: "cbd",
  102. // },
  103. // dataType: "json",
  104. // success: function (data) {
  105. // eachItem(data)
  106. // }
  107. // });
  108. // $.ajax({
  109. // type: "post",
  110. // url: "home_map/map_locat",
  111. // data: {
  112. // req: "scd",
  113. // },
  114. // dataType: "json",
  115. // success: function (data) {
  116. // eachItem(data)
  117. // }
  118. // });
  119. // $.ajax({
  120. // type: "post",
  121. // url: "home_map/map_locat",
  122. // data: {
  123. // req: "qxz",
  124. // },
  125. // dataType: "json",
  126. // success: function (data) {
  127. // eachItem(data)
  128. // }
  129. // });
  130. }
  131. function addArea(lng,lat,equip_type,equip_name,equip_id,equip_location){
  132. if (lng > 999) {
  133. var lng = Math.floor(lng / 100) + ((lng % 100) / 60);
  134. var lat = Math.floor(lat / 100) + ((lat % 100) / 60);
  135. } else {
  136. var lat = lat;
  137. var lng = lng;
  138. }
  139. alert(111)
  140. if (lat && lng) {
  141. //标注图标
  142. var orgType = equip_type;//parseInt(0) data_info[i].equip_type
  143. if (orgType == 5) {
  144. var equip_type = '环境监测';
  145. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 1 * 23) });
  146. } else if (orgType == 2) {
  147. var equip_type = '杀虫灯';
  148. var myIcon = new BMap.Icon("/static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 2 * 23) });
  149. } else if (orgType == 3) {
  150. var equip_type = '虫情测报';
  151. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -13), imageOffset: new BMap.Size(0, 0 - 3 * 23) });
  152. } else if (orgType == 4) {
  153. var equip_type = '性诱器设备';
  154. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 6 * 23) });
  155. } else if (orgType == 6) {
  156. var equip_type = '监控设备';
  157. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 5 * 23) });
  158. } else if (orgType == 7) {
  159. var equip_type = '孢子仪设备';
  160. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 0 * 23) });
  161. } else if (orgType == 8) {
  162. var equip_type = '农药残留';
  163. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 4 * 23) });
  164. }
  165. var lng1 = wgs84togcj02(lng, lat);
  166. var lng2 = gcj02tobd09(lng1[0], lng1[1]);
  167. // pointArray[i] = new BMap.Point(lng2[0], lng2[1]);
  168. pointArray.push(new BMap.Point(lng2[0], lng2[1]))
  169. var marker = new BMap.Marker(new BMap.Point(lng2[0], lng2[1]), { icon: myIcon }); // 创建标注
  170. map.addOverlay(marker);
  171. var enterNum = equip_type;//data_info[i].equip_name
  172. var content = '<p>名称:' + equip_name + '</p><p class="gotoDetial" onclick="gotoManageDetial(\'' + equip_id + ' \',' + orgType + ')">IMEI:' + equip_id + '</p><p>经度:' + lng2[0] + '</p><p>纬度:' + lng2[1] + '</p><p class="area">位置:' + equip_location + '</p></br>';
  173. if (equip_location) {
  174. var flag = true;
  175. } else {
  176. var flag = false;
  177. }
  178. // 将标注添加到地图中
  179. addClickHandler(content, marker, enterNum, flag);
  180. }
  181. }
  182. function eachItem(data_info) {
  183. var pointArray = [];
  184. for (var i = 0; i < data_info.length; i++) {
  185. if (data_info[i].lng > 999) {
  186. var lng = Math.floor(data_info[i].lng / 100) + ((data_info[i].lng % 100) / 60);
  187. var lat = Math.floor(data_info[i].lat / 100) + ((data_info[i].lat % 100) / 60);
  188. } else {
  189. var lat = data_info[i].lat;
  190. var lng = data_info[i].lng;
  191. }
  192. if (data_info[i].lat && data_info[i].lng) {
  193. //标注图标
  194. var orgType = data_info[i].equip_type;//parseInt(0) data_info[i].equip_type
  195. if (orgType == 5) {
  196. var equip_type = '环境监测';
  197. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 1 * 23) });
  198. } else if (orgType == 2) {
  199. var equip_type = '杀虫灯';
  200. var myIcon = new BMap.Icon("/static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 2 * 23) });
  201. } else if (orgType == 3) {
  202. var equip_type = '虫情测报';
  203. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -13), imageOffset: new BMap.Size(0, 0 - 3 * 23) });
  204. } else if (orgType == 4) {
  205. var equip_type = '性诱器设备';
  206. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 6 * 23) });
  207. } else if (orgType == 6) {
  208. var equip_type = '监控设备';
  209. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 5 * 23) });
  210. } else if (orgType == 7) {
  211. var equip_type = '孢子仪设备';
  212. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 0 * 23) });
  213. } else if (orgType == 8) {
  214. var equip_type = '农药残留';
  215. var myIcon = new BMap.Icon("static/img/locationIcon2.png", new BMap.Size(17, 23), { offset: new BMap.Size(0, -18), imageOffset: new BMap.Size(0, 0 - 4 * 23) });
  216. }
  217. var lng1 = wgs84togcj02(lng, lat);
  218. var lng2 = gcj02tobd09(lng1[0], lng1[1]);
  219. // pointArray[i] = new BMap.Point(lng2[0], lng2[1]);
  220. pointArray.push(new BMap.Point(lng2[0], lng2[1]))
  221. var marker = new BMap.Marker(new BMap.Point(lng2[0], lng2[1]), { icon: myIcon }); // 创建标注
  222. map.addOverlay(marker);
  223. var enterNum = equip_type;//data_info[i].equip_name
  224. var content = '<p>名称:' + data_info[i].equip_name + '</p><p class="gotoDetial" onclick="gotoManageDetial(\'' + data_info[i].equip_id + ' \',' + orgType + ')">IMEI:' + data_info[i].equip_id + '</p><p>经度:' + lng2[0] + '</p><p>纬度:' + lng2[1] + '</p><p class="area">位置:' + data_info[i].equip_location + '</p></br>';
  225. if (data_info[i].equip_location) {
  226. var flag = true;
  227. } else {
  228. var flag = false;
  229. }
  230. // 将标注添加到地图中
  231. addClickHandler(content, marker, enterNum, flag);
  232. }
  233. }
  234. //让所有点在视野范围内
  235. map.setViewport(pointArray);
  236. }
  237. function addClickHandler(content, marker, enterNum, flag) {
  238. marker.addEventListener("click", function (e) {
  239. if (!flag) {
  240. var pt = marker.point;
  241. geoc.getLocation(pt, function (rs) {
  242. var addComp = rs.addressComponents;
  243. $('.area').html('位置:' + addComp.province + ", " + addComp.city + ", " + addComp.district)
  244. // alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
  245. });
  246. }
  247. openInfo(content, e, enterNum)
  248. }
  249. );
  250. }
  251. function openInfo(content, event, orgname) {
  252. var p = event.target;
  253. var opts = {
  254. title: orgname, // 信息窗口标题
  255. enableMessage: true//设置允许信息窗发送短息
  256. };
  257. var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
  258. var infoWindow = new BMap.InfoWindow(content, opts); // 创建信息窗口对象
  259. map.openInfoWindow(infoWindow, point); //开启信息窗口
  260. }
  261. // 查询相对类型的数据
  262. $('#selectType').on('click', 'span', function () {
  263. var dtype = $(this).data('type');
  264. if (dtype == 2 || dtype == 3 || dtype == 5 || dtype == 7 || dtype == 8) {
  265. if (dtype == 2) { dtype = "scd" }
  266. else if (dtype == 3) { dtype = "cbd" }
  267. else if (dtype == 5) { dtype = "qxz" }
  268. else if (dtype == 7) { dtype = "bzy" }
  269. else if (dtype == 8) { dtype = "xyq" }
  270. map.clearOverlays();//清除全部
  271. $.ajax({
  272. type: "post",
  273. url: "home_map/map_locat",
  274. data: { req: dtype },
  275. dataType: "json",
  276. success: function (data) {
  277. map.clearOverlays();//清除全部
  278. eachItem(data)
  279. }
  280. });
  281. } else if (dtype == 0) {
  282. map.clearOverlays();//清除全部
  283. $.ajax({
  284. type: "post",
  285. url: "home_map/map_locat",
  286. data: {
  287. req: "cbd",
  288. },
  289. dataType: "json",
  290. success: function (data) {
  291. eachItem(data)
  292. }
  293. });
  294. $.ajax({
  295. type: "post",
  296. url: "home_map/map_locat",
  297. data: {
  298. req: "scd",
  299. },
  300. dataType: "json",
  301. success: function (data) {
  302. eachItem(data)
  303. }
  304. });
  305. $.ajax({
  306. type: "post",
  307. url: "home_map/map_locat",
  308. data: {
  309. req: "qxz",
  310. },
  311. dataType: "json",
  312. success: function (data) {
  313. eachItem(data)
  314. }
  315. });
  316. $.ajax({
  317. type: "post",
  318. url: "home_map/map_locat",
  319. data: {
  320. req: "bzy",
  321. },
  322. dataType: "json",
  323. success: function (data) {
  324. eachItem(data)
  325. }
  326. });
  327. $.ajax({
  328. type: "post",
  329. url: "home_map/map_locat",
  330. data: {
  331. req: "xyq",
  332. },
  333. dataType: "json",
  334. success: function (data) {
  335. eachItem(data)
  336. }
  337. });
  338. } else {
  339. // layer.msg("此功能暂未开启!", { time: 2000 });
  340. }
  341. })
  342. //搜索城市
  343. $('#searchCityBtn').on('click', searchCity);
  344. $('#searchCity').on('keyup', function (event) {
  345. if (event.keyCode == 13) {
  346. searchCity();
  347. }
  348. })
  349. function searchCity() {
  350. var local = new BMap.LocalSearch(map, {
  351. renderOptions: { map: map, panel: "r-result" },
  352. pageCapacity: 5
  353. });
  354. local.search($('#searchCity').val());
  355. }
  356. //搜索imei
  357. $('#searchBtn').on('click', searchImei);
  358. $('#searchImei').on('keyup', function (event) {
  359. if (event.keyCode == 13) {
  360. searchImei();
  361. }
  362. })
  363. function searchImei() {
  364. var imei = $('#searchImei').val();
  365. if (imei != '') {
  366. $.ajax({
  367. type: "post",
  368. url: "home_map/map_locat",
  369. data: {
  370. req: "imei",
  371. equip_id: imei
  372. },
  373. dataType: "json",
  374. success: function (data) {
  375. if (data == '0') {
  376. layer.msg("没有找到该设备,请核对设备id!", { time: 2000 });
  377. map.clearOverlays();//清除全部
  378. }
  379. else {
  380. map.clearOverlays();//清除全部
  381. eachItem(data)
  382. }
  383. }
  384. });
  385. } else {
  386. layer.msg("请输入imei!", { time: 2000 });
  387. }
  388. }
  389. var as = [{ "equip_id": "11111111", "equip_type": 3, "equip_name": "", "lng": "113.66080475001185", "lat": "34.81039429", "equip_location": "" }]
  390. function gotoManageDetial(imei, orgType) {
  391. var sidebar = $('#sidebar', parent.document);
  392. var menuLi = sidebar.find('.nav-list.nav').children();
  393. menuLi.each(function (idx, val) {
  394. $(val).removeClass('active');
  395. if ($(val).children('a').find('.menu-text').html() == '报表管理') {
  396. $(val).addClass('active');
  397. $(val).addClass('open');
  398. $(val).children('ul').removeClass('nav-hide').addClass('nav-show').show();
  399. if (orgType == 3) {
  400. $(val).children('ul').children().each(function (index, obj) {
  401. if ($(obj).find('.menu-text').html() == '测报灯') {
  402. $(obj).addClass('active');
  403. }
  404. })
  405. } else if (orgType == 2) {
  406. $(val).children('ul').children().each(function (index, obj) {
  407. if ($(obj).find('.menu-text').html() == '杀虫灯') {
  408. $(obj).addClass('active');
  409. }
  410. })
  411. }
  412. }
  413. })
  414. if (orgType == 3) {
  415. location.href = 'cbd_report_detail?id=' + imei;
  416. } else if (orgType == 2) {
  417. location.href = 'scd_report_detail?id=' + imei;
  418. }
  419. }