amap.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. use yii\helpers\Json;
  3. ?>
  4. <style type="text/css">
  5. html, body {
  6. height: 100%;
  7. width: 100%;
  8. }
  9. #container {
  10. height: 100%;
  11. width: 80%;
  12. }
  13. .map-box {
  14. width: 20%;
  15. height: 100%;
  16. position: fixed;
  17. right: 0;
  18. background-color: #ffffff;
  19. }
  20. .amap-marker-label {
  21. border: 0;
  22. background-color: transparent;
  23. }
  24. </style>
  25. <div class="row">
  26. <div id="container"></div>
  27. <div class="map-box text-center" id="mapVue">
  28. <div class="row p-4">
  29. <div :style="{'height': inventoryBodyHeight, 'overflow-y': 'auto', 'width': '100%'}">
  30. <div class="col-12 mt-2" v-for="(area, index) in areaAll" style="border: 1px solid #ededed">
  31. <div class="row">
  32. <div class="col-12 pb-2 pt-2">
  33. <span class="float-left"># {{index + 1}}</span>
  34. <span class="float-right blue pointer" @click="deleteArea(index)">删除</span>
  35. </div>
  36. </div>
  37. <div class="row">
  38. <div class="col-sm-4 text-right">
  39. <label class="control-label" for="cate-title">区域名称</label>
  40. </div>
  41. <div class="col-sm-8">
  42. <input type="text" class="form-control" v-model="area.name">
  43. <div class="help-block"></div>
  44. </div>
  45. </div>
  46. <div class="row">
  47. <div class="col-sm-4 text-right">
  48. <label class="control-label" for="cate-title">起送价</label>
  49. </div>
  50. <div class="col-sm-8">
  51. <input type="text" class="form-control" v-model="area.shipping_fee">
  52. <div class="help-block"></div>
  53. </div>
  54. </div>
  55. <div class="row">
  56. <div class="col-sm-4 text-right">
  57. <label class="control-label" for="cate-title">划分方式</label>
  58. </div>
  59. <div class="col-sm-8 text-left">
  60. <label style="color:#636f7a"><input type="radio" value="circle" v-model="area.type" @click="changeType(index, 'circle')"> 半径</label>
  61. <label style="color:#636f7a"><input type="radio" value="polygon" v-model="area.type" @click="changeType(index, 'polygon')"> 自定义</label>
  62. <div class="help-block"></div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. <span class="btn btn-white" @click="addArea()">增加区域</span>
  69. <span class="btn btn-white hide" @click="getData()">查看数据</span>
  70. <input type="hidden" id="boxId" value="<?= $boxId; ?>">
  71. </div>
  72. </div>
  73. <script type="text/javascript">
  74. var mapVue = new Vue({
  75. el: '#mapVue',
  76. data: {
  77. inventoryBodyHeight: '',
  78. path: [
  79. [<?= $longitude ?> + 0.03, <?= $latitude ?> + 0.03],
  80. [<?= $longitude ?> + 0.03, <?= $latitude ?> - 0.03],
  81. [<?= $longitude ?> - 0.03, <?= $latitude ?> - 0.03],
  82. [<?= $longitude ?> - 0.03, <?= $latitude ?> + 0.03],
  83. ],
  84. areaAll: [],
  85. defaultArea: JSON.parse('<?= Json::encode($overlay) ?>'),
  86. },
  87. methods: {
  88. // 删除优惠券
  89. addArea: function (type = 'polygon') {
  90. var overlay = this.addOverlay(type, 5000, this.path);
  91. var overlayEditor = this.openOverlay(type, overlay);
  92. var area = {
  93. 'name': '',
  94. 'type': type,
  95. 'shipping_fee': '',
  96. 'overlay': overlay,
  97. 'overlayEditor': overlayEditor,
  98. 'path': [],
  99. 'radius': 0,
  100. };
  101. this.areaAll.push(area)
  102. },
  103. deleteArea: function (index) {
  104. this.areaAll[index].overlayEditor.close();
  105. map.remove(this.areaAll[index].overlay)
  106. this.areaAll.splice(index, 1);
  107. },
  108. changeType: function (index, type) {
  109. // 移除原先的地图
  110. this.areaAll[index].overlayEditor.close();
  111. map.remove(this.areaAll[index].overlay)
  112. this.areaAll[index].overlay = '';
  113. this.areaAll[index].overlayEditor = '';
  114. // 添加新的进地图
  115. var overlay = this.addOverlay(type, 5000, this.path);
  116. // 开启编辑
  117. var overlayEditor = this.openOverlay(type, overlay);
  118. // 写入
  119. this.areaAll[index].overlay = overlay;
  120. this.areaAll[index].overlayEditor = overlayEditor;
  121. },
  122. addOverlay: function (type, radius = 5000, path = []) {
  123. var overlay;
  124. switch (type) {
  125. // 圆形
  126. case 'circle' :
  127. overlay = new AMap.Circle({
  128. center: new AMap.LngLat(<?= $longitude ?>, <?= $latitude ?>),
  129. radius: radius,
  130. strokeColor: getRandomColor(),
  131. strokeWeight: 2,
  132. // strokeOpacity: 0.2,
  133. fillOpacity: 0.4,
  134. fillColor: '#1791fc',
  135. zIndex: 50,
  136. extData: '', // 自定义参数
  137. })
  138. break;
  139. // 多边形
  140. case 'polygon' :
  141. overlay = new AMap.Polygon({
  142. path: path,
  143. strokeColor: getRandomColor(),
  144. strokeWeight: 2,
  145. // strokeOpacity: 0.2,
  146. fillOpacity: 0.4,
  147. fillColor: '#1791fc',
  148. zIndex: 50,
  149. extData: '', // 自定义参数
  150. })
  151. break;
  152. }
  153. // 添加地图并 缩放地图到合适的视野级别
  154. map.add(overlay)
  155. map.setFitView([overlay])
  156. return overlay;
  157. },
  158. openOverlay: function (type, overlay) {
  159. var overlayEditor;
  160. switch (type) {
  161. // 圆形
  162. case 'circle' :
  163. overlayEditor = new AMap.CircleEditor(map, overlay);
  164. break;
  165. // 多边形
  166. case 'polygon' :
  167. overlayEditor = new AMap.PolyEditor(map, overlay);
  168. break;
  169. }
  170. overlayEditor.open();
  171. return overlayEditor;
  172. },
  173. getData: function () {
  174. var data = [];
  175. if (this.areaAll.length === 0) {
  176. rfMsg("请添加区域范围");
  177. return;
  178. }
  179. for (let i = 0; i < this.areaAll.length; i++) {
  180. if (this.areaAll[i].name.length === 0) {
  181. rfMsg("请填写 # " + (i + 1) + "区域名称");
  182. return;
  183. }
  184. if (this.areaAll[i].shipping_fee === '') {
  185. rfMsg("请填写 # " + (i + 1) + "配送费");
  186. return;
  187. }
  188. if (isNaN(this.areaAll[i].shipping_fee)) {
  189. rfMsg("# " + (i + 1) + "配送费只能为数字");
  190. return;
  191. }
  192. if (parseFloat(this.areaAll[i].shipping_fee) < 0) {
  193. rfMsg("# " + (i + 1) + "配送费不能小于 0");
  194. return;
  195. }
  196. var path = [];
  197. var overlayPath = this.areaAll[i].overlay.getPath();
  198. for (let j = 0; j < overlayPath.length; j++) {
  199. path.push([overlayPath[j].lng, overlayPath[j].lat])
  200. }
  201. data.push({
  202. 'name': this.areaAll[i].name,
  203. 'type': this.areaAll[i].type,
  204. 'shipping_fee': this.areaAll[i].shipping_fee,
  205. 'path': path,
  206. 'radius': this.areaAll[i].type === 'polygon' ? 0 : this.areaAll[i].overlay.getRadius(),
  207. })
  208. }
  209. console.log(data);
  210. return data;
  211. },
  212. mapInit: function () {
  213. if (this.defaultArea.length === 0) {
  214. this.addArea();
  215. return;
  216. }
  217. console.log(this.defaultArea)
  218. for (let i = 0; i < this.defaultArea.length; i++) {
  219. var path = JSON.parse(this.defaultArea[i].path);
  220. var overlay = this.addOverlay(this.defaultArea[i].type, this.defaultArea[i].radius, path);
  221. var overlayEditor = this.openOverlay(this.defaultArea[i].type, overlay);
  222. var area = {
  223. 'name': this.defaultArea[i].name,
  224. 'type': this.defaultArea[i].type,
  225. 'shipping_fee': this.defaultArea[i].shipping_fee,
  226. 'overlay': overlay,
  227. 'overlayEditor': overlayEditor,
  228. 'path': [],
  229. 'radius': 0,
  230. };
  231. // console.log(area)
  232. this.areaAll.push(area)
  233. }
  234. },
  235. },
  236. // 初始化
  237. mounted() {
  238. let mapVueScrollHeight = $('#mapVue').prop('scrollHeight'); // 可视角高度
  239. this.inventoryBodyHeight = (mapVueScrollHeight - 100) + 'px';
  240. console.log(mapVueScrollHeight)
  241. },
  242. })
  243. var map;
  244. $(function () {
  245. var label = "<?= $label; ?>";
  246. var geocoder;
  247. var init = function () {
  248. AMapUI.loadUI(['misc/PositionPicker', 'misc/PoiPicker'], function (PositionPicker, PoiPicker) {
  249. //加载PositionPicker,loadUI的路径参数为模块名中 'ui/' 之后的部分
  250. map = new AMap.Map('container', {
  251. zoom: parseInt('<?= $zoom ?>'),
  252. center: [<?= $longitude ?>, <?= $latitude ?>], //初始地图中心点
  253. });
  254. // 初始化完成
  255. map.on('complete', function() {
  256. mapVue.mapInit();
  257. });
  258. geocoder = new AMap.Geocoder({
  259. radius: 1000 //范围,默认:500
  260. });
  261. var marker = new AMap.Marker({
  262. position: map.getCenter(),
  263. icon: new AMap.Icon({
  264. image: 'https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
  265. imageSize: new AMap.Size(25, 34),
  266. }),
  267. offset: new AMap.Pixel(-13, -30)
  268. });
  269. marker.setMap(map);
  270. // 设置label标签
  271. // label默认蓝框白底左上角显示,样式className为:amap-marker-label
  272. if (label) {
  273. marker.setLabel({
  274. offset: new AMap.Pixel(5, 5), //设置文本标注偏移量
  275. content: "<div class='info'>" + label + "</div>", //设置文本标注内容
  276. direction: 'right' //设置文本标注方位
  277. });
  278. }
  279. //加载工具条
  280. var tool = new AMap.ToolBar();
  281. map.addControl(tool);
  282. });
  283. };
  284. init();
  285. });
  286. /**
  287. * 随机生成颜色
  288. * @returns {string}
  289. */
  290. function getRandomColor() {
  291. return '#' + (function(color){
  292. return (color += '0123456789abcdef'[Math.floor(Math.random()*16)])
  293. && (color.length === 6) ? color : arguments.callee(color);
  294. })('');
  295. }
  296. </script>
粤ICP备19079148号