fence.js 8.4 KB

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