fence.js 8.3 KB

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