fence.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. (function () {
  2. var page = {
  3. init: function ($page, param) {
  4. $page.find('#fence-switch').rlswitch();
  5. var userinfo = renlian.storage.get('userinfo');
  6. // //地图初始化
  7. var map = new AMap.Map("fence-map", {
  8. center: [120.210649,30.246071],//地图中心点
  9. zoom: 12 //地图显示的缩放级别
  10. });
  11. renlian.layer.loading(true, "加载数据中");
  12. map.on('complete', function(){
  13. getFenceData();
  14. renlian.layer.loading(false);
  15. });
  16. var oldCircle = null;
  17. var circleFence = null;
  18. var rangeObj = $('#fence-radius');
  19. function getFenceData(){
  20. renlian.httpget(config.webapi+'?s=api/get_fence_info',userinfo.baseinfo.plate,function(res){
  21. if(!res.success){
  22. // renlian.layer.toast(res.message);
  23. var fenceInfo = {radius:500, center:param.default}
  24. if(!circleFence){
  25. circleFence = new AMap.Circle({
  26. center: fenceInfo.center,
  27. radius: fenceInfo.radius,
  28. strokeOpacity: 0,
  29. strokeWeight: 0,
  30. strokeOpacity: 0.2,
  31. fillOpacity: 0.3,
  32. fillColor : '#1791fc',
  33. zIndex: 50,
  34. });
  35. }
  36. if(!rangeObj){
  37. rangeObj = $('#fence-radius');
  38. }
  39. rangeObj[0].value = fenceInfo.radius;
  40. $('#display-radius')[0].innerHTML = rangeObj[0].value;
  41. circleFence.setMap(map)
  42. map.setFitView(circleFence);
  43. return;
  44. }
  45. if(!oldCircle){
  46. oldCircle = new AMap.Circle({
  47. center: [0,0],
  48. radius: 0,
  49. strokeOpacity: 0,
  50. strokeWeight: 0,
  51. strokeOpacity: 0.2,
  52. fillOpacity: 0.3,
  53. fillColor : '#ff1111',
  54. zIndex: 50,
  55. });
  56. }
  57. var fence = JSON.parse(res.data);
  58. var fenceStatus = fence.fenceStatus;
  59. if(fenceStatus !== false){
  60. $('#fence-switch').addClass('f-active');
  61. }
  62. if(fence.fenceInfo){
  63. var fenceInfo = fence.fenceInfo.data;
  64. fenceInfo.radius = parseInt(fenceInfo.radius)
  65. fenceInfo.center = new AMap.LngLat(fenceInfo.center.lng, fenceInfo.center.lat)
  66. oldCircle.setCenter(fenceInfo.center);
  67. oldCircle.setRadius(fenceInfo.radius);
  68. oldCircle.setMap(map);
  69. }else{
  70. }
  71. if(!circleFence){
  72. circleFence = new AMap.Circle({
  73. center: fenceInfo.center,
  74. radius: fenceInfo.radius,
  75. strokeOpacity: 0,
  76. strokeWeight: 0,
  77. strokeOpacity: 0.2,
  78. fillOpacity: 0.3,
  79. fillColor : '#1791fc',
  80. zIndex: 50,
  81. });
  82. }
  83. if(!rangeObj){
  84. rangeObj = $('#fence-radius');
  85. }
  86. rangeObj[0].value = fenceInfo.radius;
  87. $('#display-radius')[0].innerHTML = rangeObj[0].value;
  88. circleFence.setMap(map)
  89. map.setFitView(circleFence);
  90. });
  91. }
  92. // getFenceData()
  93. //获取要设置的半径
  94. function getSetRadius(){
  95. return rangeObj[0].value;
  96. }
  97. $('#fence-radius').on('input',function(){
  98. $('#display-radius')[0].innerHTML = this.value;
  99. var radius = getSetRadius();
  100. circleFence.setRadius(radius);
  101. map.setFitView([ circleFence ]);
  102. });
  103. map.on('moveend', function(){
  104. var center = map.getCenter();
  105. circleFence.setCenter(center);
  106. });
  107. //保存
  108. $page.find('#save-fence').on('tap', function(){
  109. renlian.layer.confirm('保存将会覆盖原围栏', function (_index) {
  110. if(_index === '1'){
  111. var data = getPostData();
  112. if(!data){
  113. renlian.layer.toast('围栏信息有误或者为空');
  114. return;
  115. }
  116. postFence(data);
  117. }else{
  118. return;
  119. }
  120. }, '保存围栏', ['否', '是']);
  121. });
  122. //获取提交数据
  123. function getPostData(){
  124. var fenceEnable = getAbleStatus();
  125. var circleCenter = circleFence.getCenter();
  126. var center = {lng: circleCenter.lng, lat:circleCenter.lat}
  127. var fenceData = {};
  128. fenceData.type = 'circle';
  129. fenceData.data = {};
  130. fenceData.data.radius = circleFence.getRadius();
  131. fenceData.data.center = center;
  132. var data = {};
  133. data.vehicleNumber = userinfo.baseinfo.deviceNumber;
  134. data.plate = userinfo.baseinfo.plate;
  135. data.data = JSON.stringify(fenceData);
  136. data.fenceAlarmEnable = fenceEnable;
  137. return data;
  138. }
  139. //获取启用状态
  140. function getAbleStatus(){
  141. var status = $('#fence-switch').hasClass('f-active');
  142. if(status){
  143. return true;
  144. }else{
  145. return false;
  146. }
  147. }
  148. //post围栏信息
  149. function postFence(data){
  150. renlian.httppost(config.webapi+'?s=api/save_fence_info',data,function(res){
  151. if(!res.success){
  152. renlian.layer.toast(res.message);
  153. return;
  154. }
  155. var oldInfo = JSON.parse(data.data);
  156. var center = new AMap.LngLat(oldInfo.data.center.lng, oldInfo.data.center.lat)
  157. oldCircle.setCenter(center);
  158. oldCircle.setRadius(oldInfo.data.radius);
  159. renlian.layer.toast('保存成功');
  160. });
  161. }
  162. }
  163. };
  164. return page;
  165. })();