fence.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. (function () {
  2. var page = {
  3. init: function ($page, param) {
  4. // //地图初始化
  5. var map = new AMap.Map("fence-map", {
  6. center: [113.955439, 22.542956],//地图中心点
  7. zoom: 12 //地图显示的缩放级别
  8. });
  9. // // var polygonFence = new AMap.Polygon({
  10. // // strokeColor: "#FF33FF",
  11. // // strokeWeight: 6,
  12. // // strokeOpacity: 0.2,
  13. // // fillOpacity: 0.4,
  14. // // fillColor: '#1791fc',
  15. // // zIndex: 50,
  16. // // })
  17. var fenceInfo = param.fenceInfo;
  18. var defaultPt = param.defaultLocation;
  19. var ableStatus = parm.fenceAlarmStatus;
  20. if(ableStatus){
  21. $('#fence-switch').addClass('f-active');
  22. }
  23. console.log(param);
  24. var circleFence = new AMap.Circle({
  25. center: defaultPt,
  26. radius: fenceInfo.data.radius,
  27. strokeOpacity: 0,
  28. strokeWeight: 0,
  29. strokeOpacity: 0.2,
  30. fillOpacity: 0.3,
  31. fillColor : '#1791fc',
  32. zIndex: 50,
  33. });
  34. circleFence.setMap(map)
  35. map.setFitView(circleFence);
  36. // displayFence(fenceInfo);
  37. $page.find('#fence-switch').lrswitch();
  38. var rangeObj = $('#fence-radius');
  39. $('#display-radius')[0].innerHTML = rangeObj[0].value;
  40. //获取要设置的半径
  41. function getSetRadius(){
  42. return rangeObj[0].value;
  43. }
  44. $('#fence-radius').on('input',function(){
  45. $('#display-radius')[0].innerHTML = this.value;
  46. var radius = getSetRadius();
  47. circleFence.setRadius(radius);
  48. // setFenceView(circleFence);
  49. map.setFitView([ circleFence ]);
  50. });
  51. map.on('moveend', function(){
  52. var center = map.getCenter();
  53. circleFence.setCenter(center);
  54. });
  55. //保存
  56. $page.find('#save-fence').on('tap', function(){
  57. learun.layer.confirm('保存将会覆盖原围栏', function (_index) {
  58. if(_index === '1'){
  59. var data = getPostData();
  60. if(!data){
  61. learun.layer.toast('围栏信息有误或者为空');
  62. return;
  63. }
  64. postFence(data);
  65. }else{
  66. return;
  67. }
  68. }, '保存围栏', ['否', '是']);
  69. });
  70. //获取提交数据
  71. function getPostData(){
  72. var fenceEnable = getAbleStatus();
  73. var radius = circleFence.getRadius();
  74. var circleCenter = circleFence.getCenter();
  75. var center = {lng: circleCenter.lng, lat:circleCenter.lat}
  76. var data = {
  77. type: 'circle',
  78. data: {radius: radius, center: center},
  79. fenceAlarmEnable: fenceEnable
  80. };
  81. return data;
  82. }
  83. //获取启用状态
  84. function getAbleStatus(){
  85. var status = $('#fence-switch').hasClass('f-active');
  86. if(status){
  87. return true;
  88. }else{
  89. return false;
  90. }
  91. }
  92. //post围栏信息
  93. function postFence(data){
  94. learun.httppost(config.webapi+'?s=api/save_fence_info',data,function(res){
  95. if(!res.success){
  96. learun.layer.toast(res.message);
  97. return;
  98. }
  99. learun.layer.toast('保存成功');
  100. });
  101. }
  102. //显示围栏(初始)
  103. // function displayFence(fenceInfo){
  104. // if(!fenceInfo){
  105. // circleFence.setCenter(defaultPt);
  106. // var radius = getSetRadius();
  107. // circleFence.setRadius(radius);
  108. // // circleFence.setMap(map);
  109. // map.setFitView([ circleFence ]);
  110. // return;
  111. // }
  112. // var fence = fenceInfo;
  113. // console.log(fence);
  114. // if(fence.type == 'circle'){
  115. // var pt = new AMap.LngLat(fence.data.center.lng, fence.data.center.lat);
  116. // // circleFence.setCenter(pt);
  117. // circleFence.setRadius(fence.data.radius);
  118. // // circleFence.setMap(map);
  119. // map.setFitView([ circleFence ]);
  120. // return;
  121. // }
  122. // // if(fence.type == 'polygon'){
  123. // // var pts = [];
  124. // // var paths = fence.data.vertex;
  125. // // paths.forEach(v => {
  126. // // pts.push([v.lng, v.lat]);
  127. // // });
  128. // // polygonFence.setPath(pts);
  129. // // polygonFence.setMap(map);
  130. // // map.setFitView([ polygonFence ]);
  131. // // return;
  132. // // }
  133. // }
  134. }
  135. };
  136. return page;
  137. })();