fence.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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(!oldCircle){
  37. oldCircle = new AMap.Circle({
  38. center: [0,0],
  39. radius: 0,
  40. strokeOpacity: 0,
  41. strokeWeight: 0,
  42. strokeOpacity: 0.2,
  43. fillOpacity: 0.3,
  44. fillColor : '#ff1111',
  45. zIndex: 50,
  46. });
  47. oldCircle.setMap(map);
  48. }
  49. if(!rangeObj){
  50. rangeObj = $('#fence-radius');
  51. }
  52. rangeObj[0].value = fenceInfo.radius;
  53. $('#display-radius')[0].innerHTML = rangeObj[0].value;
  54. circleFence.setMap(map)
  55. map.setFitView(circleFence);
  56. return;
  57. }
  58. if(!oldCircle){
  59. oldCircle = new AMap.Circle({
  60. center: [0,0],
  61. radius: 0,
  62. strokeOpacity: 0,
  63. strokeWeight: 0,
  64. strokeOpacity: 0.2,
  65. fillOpacity: 0.3,
  66. fillColor : '#ff1111',
  67. zIndex: 50,
  68. });
  69. }
  70. var fence = JSON.parse(res.data);
  71. var fenceStatus = fence.fenceStatus;
  72. if(fenceStatus !== false){
  73. $('#fence-switch').addClass('f-active');
  74. }
  75. if(fence.fenceInfo){
  76. var fenceInfo = fence.fenceInfo.data;
  77. fenceInfo.radius = parseInt(fenceInfo.radius)
  78. fenceInfo.center = new AMap.LngLat(fenceInfo.center.lng, fenceInfo.center.lat)
  79. oldCircle.setCenter(fenceInfo.center);
  80. oldCircle.setRadius(fenceInfo.radius);
  81. oldCircle.setMap(map);
  82. }else{
  83. }
  84. if(!circleFence){
  85. circleFence = new AMap.Circle({
  86. center: fenceInfo.center,
  87. radius: fenceInfo.radius,
  88. strokeOpacity: 0,
  89. strokeWeight: 0,
  90. strokeOpacity: 0.2,
  91. fillOpacity: 0.3,
  92. fillColor : '#1791fc',
  93. zIndex: 50,
  94. });
  95. }
  96. if(!rangeObj){
  97. rangeObj = $('#fence-radius');
  98. }
  99. rangeObj[0].value = fenceInfo.radius;
  100. $('#display-radius')[0].innerHTML = rangeObj[0].value;
  101. circleFence.setMap(map)
  102. map.setFitView(circleFence);
  103. });
  104. }
  105. // getFenceData()
  106. //获取要设置的半径
  107. function getSetRadius(){
  108. return rangeObj[0].value;
  109. }
  110. $('#fence-radius').on('input',function(){
  111. $('#display-radius')[0].innerHTML = this.value;
  112. var radius = getSetRadius();
  113. circleFence.setRadius(radius);
  114. map.setFitView([ circleFence ]);
  115. });
  116. map.on('moveend', function(){
  117. var center = map.getCenter();
  118. circleFence.setCenter(center);
  119. });
  120. //保存
  121. $page.find('#save-fence').on('tap', function(){
  122. renlian.layer.confirm('保存将会覆盖原围栏', function (_index) {
  123. if(_index === '1'){
  124. var data = getPostData();
  125. if(!data){
  126. renlian.layer.toast('围栏信息有误或者为空');
  127. return;
  128. }
  129. postFence(data);
  130. }else{
  131. return;
  132. }
  133. }, '保存围栏', ['否', '是']);
  134. });
  135. //获取提交数据
  136. function getPostData(){
  137. var fenceEnable = getAbleStatus();
  138. var circleCenter = circleFence.getCenter();
  139. var center = {lng: circleCenter.lng, lat:circleCenter.lat}
  140. var fenceData = {};
  141. fenceData.type = 'circle';
  142. fenceData.data = {};
  143. fenceData.data.radius = circleFence.getRadius();
  144. fenceData.data.center = center;
  145. var data = {};
  146. data.vehicleNumber = userinfo.baseinfo.deviceNumber;
  147. data.plate = userinfo.baseinfo.plate;
  148. data.data = JSON.stringify(fenceData);
  149. data.fenceAlarmEnable = fenceEnable;
  150. return data;
  151. }
  152. //获取启用状态
  153. function getAbleStatus(){
  154. var status = $('#fence-switch').hasClass('f-active');
  155. if(status){
  156. return true;
  157. }else{
  158. return false;
  159. }
  160. }
  161. //post围栏信息
  162. function postFence(data){
  163. renlian.httppost(config.webapi+'?s=api/save_fence_info',data,function(res){
  164. if(!res.success){
  165. renlian.layer.toast(res.message);
  166. return;
  167. }
  168. var oldInfo = JSON.parse(data.data);
  169. var center = new AMap.LngLat(oldInfo.data.center.lng, oldInfo.data.center.lat)
  170. if(oldCircle){
  171. oldCircle.setCenter(center);
  172. oldCircle.setRadius(oldInfo.data.radius);
  173. }
  174. renlian.layer.toast('保存成功');
  175. });
  176. }
  177. }
  178. };
  179. return page;
  180. })();