fence.js 4.6 KB

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