baseAll.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <!-- mapui-->
  2. <template>
  3. <div class='map'>
  4. <div id="container">加载数据,请稍候...</div>
  5. <div class="leftNavBox">
  6. <div class="searchBox">
  7. <el-select size="small" @change="searchPro()" filterable v-model="form.province" placeholder="选择省">
  8. <el-option label="--请选择--" value=""></el-option>
  9. <el-option
  10. v-for="item in provinceList"
  11. :key="item.adcode"
  12. :label="item.name"
  13. :value="item.adcode">
  14. </el-option>
  15. </el-select>
  16. <el-select size="small" @change="searchCity()" filterable v-model="form.city" placeholder="选择市">
  17. <el-option label="--请选择--" value=""></el-option>
  18. <el-option
  19. v-for="item in cityList"
  20. :key="item.adcode"
  21. :label="item.name"
  22. :value="item.adcode">
  23. </el-option>
  24. </el-select>
  25. <el-select size="small" @change="searchDis()" filterable v-model="form.district" placeholder="选择区">
  26. <el-option label="--请选择--" value=""></el-option>
  27. <el-option
  28. v-for="item in districtList"
  29. :key="item.adcode"
  30. :label="item.name"
  31. :value="item.adcode">
  32. </el-option>
  33. </el-select>
  34. </div>
  35. <div class="listBox">
  36. <div class="projectSearch">
  37. <div>项目列表</div>
  38. <div>
  39. <el-input
  40. size="small"
  41. placeholder="请输入项目名称"
  42. v-model="input"
  43. suffix-icon="el-icon-search"
  44. @change='getEquipList'
  45. clearable>
  46. </el-input>
  47. </div>
  48. </div>
  49. <div class="inner">
  50. <div class="scroll">
  51. <el-scrollbar>
  52. <div class="equipList">
  53. <div class="item"
  54. @click="equipLocate(item)"
  55. @dblclick="enterBase(item.uid)"
  56. v-for="item in mapData" :key="item.uuid">
  57. <img src="../../../../static/images/baseIcon.png" width="25px" alt="">
  58. {{item.projectname}}
  59. </div>
  60. <div class="item noDate" v-if="!mapData.length">暂无数据</div>
  61. </div>
  62. </el-scrollbar>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </template>
  69. <script>
  70. //这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  71. //例如:import 《组件名称》 from '《组件路径》';
  72. export default {
  73. //import引入的组件需要注入到对象中才能使用
  74. components: {},
  75. data() {
  76. //这里存放数据
  77. return {
  78. map:null,
  79. data:[],
  80. mapData:[],
  81. topAdcodes:[100000],//全国区划编码
  82. center:[116.3683244,39.915085],
  83. zoom:4.5,
  84. // 搜索
  85. form:{
  86. province:'',
  87. city:'',
  88. district:'',
  89. },
  90. provinceList:[],
  91. cityList:[],
  92. districtList:[],
  93. district:null,
  94. input:'',
  95. polygons:[],
  96. distCluster:null,
  97. pointSimplifierIns:null,
  98. };
  99. },
  100. //监听属性 类似于data概念
  101. computed: {},
  102. //监控data中的数据变化
  103. watch: {},
  104. //方法集合
  105. methods: {
  106. initMap(){
  107. //创建地图
  108. var that = this
  109. this.map = new AMap.Map('container', {
  110. center:that.center,
  111. zoom: that.zoom
  112. });
  113. AMapUI.loadUI(['control/BasicControl'], function(BasicControl) {
  114. //缩放控件,显示Zoom值
  115. that.map.addControl(new BasicControl.Zoom({
  116. position: 'rb',
  117. showZoomNum: true
  118. }));
  119. });
  120. //加载相关组件
  121. AMapUI.load(['ui/geo/DistrictCluster', 'lib/$','ui/misc/PointSimplifier'], function(DistrictCluster, $,PointSimplifier) {
  122. //启动页面
  123. that.initPage(DistrictCluster, $,PointSimplifier);
  124. });
  125. },
  126. initPage(DistrictCluster, $,PointSimplifier) {
  127. var that = this;
  128. //创建组件实例
  129. this.pointSimplifierIns = new PointSimplifier({
  130. map: this.map, //关联的map
  131. zIndex:113,
  132. autoSetFitView: false,
  133. compareDataItem: function(a, b, aIndex, bIndex) {
  134. //数据源中靠后的元素优先,index大的排到前面去
  135. // return aIndex > bIndex ? -1 : 1;
  136. },
  137. getPosition: function(item) {
  138. if (!item.lnglat) {
  139. return null;
  140. }
  141. //返回经纬度
  142. return [parseFloat(item.lnglat[0]), parseFloat(item.lnglat[1])];
  143. },
  144. getHoverTitle: function(dataItem, idx) {
  145. //返回数据项的Title信息,鼠标hover时显示
  146. // return '序号: ' + idx;
  147. // console.log(dataItem);
  148. return '<div style="font-size:14px">' + dataItem.projectname+'</div>';
  149. },
  150. renderOptions: {
  151. //点的样式
  152. pointStyle: {
  153. //绘制点占据的矩形区域
  154. content: function(ctx, x, y, width, height) {
  155. ctx.moveTo(x, y); //移动到左上角
  156. ctx.lineTo(x + 25, y); //连接到右上角
  157. ctx.lineTo(x, y+height/1.5); //连接到右上角
  158. ctx.lineTo(x, y + height);//连接到右下角
  159. ctx.lineTo(x, y + height); //连接到左下角
  160. ctx.lineTo(x, y);//连接到左上角
  161. },
  162. //宽度
  163. width: 33,
  164. //高度
  165. height: 30,
  166. fillStyle: 'red',
  167. lineWidth: 1,
  168. offset:['0', '-100%'],
  169. strokeStyle: '#000'
  170. },
  171. }
  172. });
  173. this.pointSimplifierIns.setData(this.mapData)
  174. this.pointSimplifierIns.on('pointClick',(e,point)=>{
  175. this.$store.commit('setData',{
  176. name:'plantProtectionUserId',
  177. val:point.data.uid
  178. })
  179. that.$router.push('/plantGuard/base')
  180. })
  181. this.distCluster = new DistrictCluster({
  182. map: that.map, //所属的地图实例
  183. zIndex:13,
  184. autoSetFitView: false,
  185. topAdcodes: that.topAdcodes,
  186. getPosition: function(item) {
  187. if (!item.lnglat) {
  188. return null;
  189. }
  190. //返回经纬度
  191. return [parseFloat(item.lnglat[0]), parseFloat(item.lnglat[1])];
  192. },
  193. renderOptions: {
  194. // clusterMarkerKeepConsistent: false, //marker的位置随交互变化
  195. featureStyle: {
  196. fillStyle: 'rgba(102,170,0,0.5)', //填充色
  197. lineWidth: 2, //描边线宽
  198. strokeStyle: '#4e6ef2', //描边色
  199. //鼠标Hover后的样式
  200. hoverOptions: {
  201. fillStyle: 'rgba(78,110,242,0.4)'
  202. }
  203. },
  204. // 特定区划级别的默认样式
  205. featureStyleByLevel: {
  206. //全国
  207. country: {
  208. fillStyle: 'rgba(78,110,242,0)'
  209. },
  210. //省
  211. province: {
  212. fillStyle: 'rgba(78,110,242,0)'
  213. },
  214. //市
  215. city: {
  216. fillStyle: 'rgba(78,110,242,0)'
  217. },
  218. //区县
  219. district: {
  220. fillStyle:'rgba(78,110,242,0)'
  221. }
  222. },
  223. getClusterMarker: function(feature, dataItems, recycledMarker) {
  224. //不是当前feature,直接返回null
  225. if(dataItems.length){
  226. var container, title, body, nodeClassNames = {
  227. title: 'amap-ui-district-cluster-marker-title',
  228. body: 'amap-ui-district-cluster-marker-body',
  229. container: 'amap-ui-district-cluster-marker'
  230. };
  231. console.log(recycledMarker);
  232. if (recycledMarker) {
  233. console.log(recycledMarker.getContent());
  234. container = recycledMarker.getContent();
  235. title = $(container).find('.' + nodeClassNames.title)[0];
  236. body = $(container).find('.' + nodeClassNames.body)[0];
  237. } else {
  238. container = document.createElement('div');
  239. title = document.createElement('span');
  240. title.className = nodeClassNames.title;
  241. body = document.createElement('span');
  242. body.className = nodeClassNames.body;
  243. container.appendChild(title);
  244. container.appendChild(body);
  245. }
  246. console.log(feature.properties);
  247. var props = feature.properties,
  248. routeNames = [];
  249. var classNameList = [nodeClassNames.container, 'level_' + props.level, 'adcode_' + props.adcode];
  250. container.className = classNameList.join(' ');
  251. if (routeNames.length > 0) {
  252. routeNames.push(props.name);
  253. container.setAttribute('title', routeNames.join('>'));
  254. } else {
  255. container.removeAttribute('title');
  256. }
  257. $(title).html(props.name);
  258. $(body).html(dataItems.length);
  259. var resultMarker = recycledMarker || new AMap.Marker({
  260. topWhenClick: true,
  261. offset: new AMap.Pixel(-20, -30),
  262. content: container
  263. });
  264. console.log(container);
  265. console.log(resultMarker);
  266. return resultMarker;
  267. }
  268. }
  269. },
  270. });
  271. //监听区划面的点击
  272. this.distCluster.on('clusterMarkerClick', function(e, feature) {
  273. console.log(e)
  274. console.log(feature)
  275. console.log(that);
  276. //重绘
  277. //distCluster.renderLater();
  278. });
  279. // window.distCluster = distCluster;
  280. $('<div id="loadingTip">加载数据,请稍候...</div>').appendTo(document.body);
  281. $.get('https://a.amap.com/amap-ui/static/data/10w.txt', (csv)=>{
  282. $('#loadingTip').remove();
  283. // var data = [
  284. // "113.665412,34.757975",
  285. // "113.671767,34.815528",
  286. // "113.671867,34.825528",
  287. // "113.735978,34.728131",
  288. // "120.412618,36.382612",
  289. // "112.985037,23.15046",
  290. // "126.687123,45.787618"
  291. // ]
  292. var data = csv.split('\n').slice(0,1000);
  293. // that.data = data
  294. this.distCluster.setData(that.data);
  295. });
  296. },
  297. getEquipList() {
  298. this.$axios({
  299. method: "POST",
  300. url: "/api/api_gateway?method=user.login.user_project_addr",
  301. data: this.qs.stringify({
  302. projectname:this.input
  303. })
  304. }).then(res => {
  305. if (res.data.message == ""){
  306. this.data = res.data.data.user_project;
  307. this.mapData = res.data.data.user_project;
  308. this.$nextTick(()=>{
  309. this.initMap()
  310. })
  311. } else {
  312. this.$message.error(res.data.message);
  313. }
  314. });
  315. },
  316. districtFun(){
  317. this.map = new AMap.Map('container', {
  318. resizeEnable: true,
  319. center: [116.30946, 39.937629],
  320. zoom: 3
  321. });
  322. //行政区划查询
  323. var opts = {
  324. subdistrict: 1, //返回下一级行政区
  325. showbiz:false //最后一级返回街道信息
  326. };
  327. this.district = new AMap.DistrictSearch(opts);//注意:需要使用插件同步下发功能才能这样直接使用
  328. this.district.search('中国', (status, result)=>{
  329. if(status=='complete'){
  330. this.getData(result.districtList[0],'country');
  331. }
  332. });
  333. },
  334. getData(data,level) {
  335. console.log(data);
  336. var bounds = data.boundaries;
  337. if (bounds) {
  338. for (var i = 0, l = bounds.length; i < l; i++) {
  339. var polygon = new AMap.Polygon({
  340. map: this.map,
  341. zIndex:5,
  342. bubble:true,
  343. strokeWeight: 2,
  344. strokeColor: 'red',
  345. fillOpacity: 0.2,
  346. path: bounds[i]
  347. });
  348. this.polygons.push(polygon);
  349. }
  350. this.map.setFitView();//地图自适应
  351. }
  352. var subList = data.districtList;
  353. //清空下一级别的下拉列表
  354. if (level === 'country') {
  355. console.log('country');
  356. this.provinceList = subList;
  357. this.cityList = []
  358. this.districtList = [];
  359. this.form.province=''
  360. this.form.city=''
  361. this.form.district=''
  362. } else if (level === 'province') {
  363. console.log('province');
  364. this.cityList = subList;
  365. this.districtList = [];
  366. this.form.city=''
  367. this.form.district=''
  368. } else if (level === 'city') {
  369. console.log('city');
  370. this.districtList = subList;
  371. this.form.district=''
  372. }
  373. console.log(subList);
  374. // if (subList) {
  375. // var contentSub = new Option('--请选择--');
  376. // var curlevel = subList[0].level;
  377. // var curList = document.querySelector('#' + curlevel);
  378. // curList.add(contentSub);
  379. // for (var i = 0, l = subList.length; i < l; i++) {
  380. // var name = subList[i].name;
  381. // var levelSub = subList[i].level;
  382. // var cityCode = subList[i].citycode;
  383. // contentSub = new Option(name);
  384. // contentSub.setAttribute("value", levelSub);
  385. // contentSub.center = subList[i].center;
  386. // contentSub.adcode = subList[i].adcode;
  387. // curList.add(contentSub);
  388. // }
  389. // }
  390. },
  391. searchPro(){
  392. var keyword = this.form.province; //关键字
  393. console.log(this.form.province);
  394. console.log(this.form.city);
  395. console.log(this.form.district);
  396. if(!keyword){
  397. this.cityList = []
  398. this.districtList = [];
  399. this.form.city=''
  400. this.form.district='';
  401. this.mapData=this.data;
  402. this.pointSimplifierIns.setData(this.mapData)
  403. }else{
  404. this.distCluster.getClusterRecord(keyword,(error, result)=>{
  405. // console.log(result);
  406. //获取此区域下的数据点
  407. this.mapData = [];
  408. result.dataItems.forEach((item) => {
  409. this.mapData.push(item.dataItem)
  410. });
  411. this.$nextTick(()=>{
  412. this.pointSimplifierIns.setData(this.mapData)
  413. })
  414. })
  415. }
  416. // if(this.form.province||this.form.city||this.form.district){
  417. // this.distCluster.setData([])
  418. // }else{
  419. // this.distCluster.setData(this.data)
  420. // }
  421. this.search('province',keyword)
  422. },
  423. searchCity(){
  424. var keyword = this.form.city || this.form.province; //关键字
  425. this.distCluster.getClusterRecord(keyword,(error, result)=>{
  426. // console.log(result);
  427. //获取此区域下的数据点
  428. this.mapData = [];
  429. result.dataItems.forEach((item) => {
  430. this.mapData.push(item.dataItem)
  431. });
  432. this.$nextTick(()=>{
  433. this.pointSimplifierIns.setData(this.mapData)
  434. })
  435. })
  436. this.search('city',keyword)
  437. },
  438. searchDis(){
  439. var keyword = this.form.district ||this.form.city; //关键字
  440. this.distCluster.getClusterRecord(keyword,(error, result)=>{
  441. // console.log(result);
  442. //获取此区域下的数据点
  443. this.mapData = [];
  444. result.dataItems.forEach((item) => {
  445. this.mapData.push(item.dataItem)
  446. });
  447. this.$nextTick(()=>{
  448. this.pointSimplifierIns.setData(this.mapData)
  449. })
  450. })
  451. this.search('district',keyword)
  452. },
  453. search(levelSub,keyword) {
  454. //清除地图上所有覆盖物
  455. for (var i = 0, l = this.polygons.length; i < l; i++) {
  456. this.polygons[i].setMap(null);
  457. }
  458. console.log(this.distCluster);
  459. // this.distCluster.setData([
  460. // "113.665412,34.757975",
  461. // "113.671767,34.815528",
  462. // "113.671867,34.825528",
  463. // "113.735978,34.728131",
  464. // "120.412618,36.382612",
  465. // "112.985037,23.15046",
  466. // "126.687123,45.787618"
  467. // ])
  468. if(!keyword){
  469. return;
  470. }
  471. this.district.setLevel(levelSub); //行政区级别
  472. this.district.setExtensions('all');
  473. //行政区查询
  474. //按照adcode进行查询可以保证数据返回的唯一性
  475. this.district.search(keyword,(status, result)=>{
  476. if(status === 'complete'){
  477. this.getData(result.districtList[0],levelSub);
  478. }
  479. });
  480. },
  481. equipLocate(item){
  482. this.map.setCenter(item.lnglat);
  483. this.map.setZoom(10)
  484. },
  485. enterBase(id){
  486. this.$store.commit('setData',{
  487. name:'plantProtectionUserId',
  488. val:id
  489. })
  490. this.$router.push('/plantGuard/base')
  491. }
  492. },
  493. //生命周期 - 创建完成(可以访问当前this实例)
  494. created() {
  495. },
  496. //生命周期 - 挂载完成(可以访问DOM元素)
  497. mounted() {
  498. this.getEquipList()
  499. this.districtFun()
  500. },
  501. beforeCreate() {}, //生命周期 - 创建之前
  502. beforeMount() {}, //生命周期 - 挂载之前
  503. beforeUpdate() {}, //生命周期 - 更新之前
  504. updated() {}, //生命周期 - 更新之后
  505. beforeDestroy() {}, //生命周期 - 销毁之前
  506. destroyed() {}, //生命周期 - 销毁完成
  507. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  508. }
  509. </script>
  510. <style scoped lang='less'>
  511. .map,#container{
  512. width: 100%;
  513. height: 100%;
  514. }
  515. .leftNavBox{
  516. position: absolute;
  517. left: 100px;
  518. top: 160px;
  519. bottom:0;
  520. /deep/.el-select{
  521. width: 1.4rem;
  522. }
  523. .listBox{
  524. position: relative;
  525. overflow: hidden;
  526. width: 4.25rem;
  527. height: 90%;
  528. margin-top: 16px;
  529. border-radius: 6px;
  530. background: rgba(255,255,255,.58);
  531. box-shadow: 0px 3px 9px 0px #a7a7a7;
  532. .projectSearch{
  533. display: flex;
  534. justify-content: space-between;
  535. align-items: center;
  536. padding: .15rem;
  537. background: #182037;
  538. color: #fff;
  539. font-size: .175rem;
  540. border-radius: 6px 6px 0 0;
  541. /deep/.el-input{
  542. width: 2.5rem;
  543. }
  544. }
  545. .inner{
  546. position: absolute;
  547. top: 57px;
  548. bottom: -18px;
  549. left: 0;
  550. right: 0;
  551. .scroll{
  552. height: 100%;
  553. overflow: auto;
  554. .equipList{
  555. .item{
  556. padding: .175rem;
  557. color: #454545;
  558. font-size: .175rem;
  559. // background: red;
  560. cursor: pointer;
  561. img{
  562. vertical-align: middle;
  563. margin-right: 2px;
  564. }
  565. &:hover{
  566. background: #182037;
  567. color: #fff;
  568. }
  569. }
  570. .noDate{
  571. text-align: center;
  572. }
  573. .item.active{
  574. background: darkblue;
  575. }
  576. }
  577. }
  578. .scroll {
  579. height: 100%;
  580. overflow-y: hidden;
  581. }
  582. .el-scrollbar{
  583. height: 100%;
  584. }
  585. .el-scrollbar__bar{
  586. &.is-vertical{
  587. width:100px;//滚动条宽度
  588. }
  589. }
  590. .el-scrollbar__wrap{
  591. overflow-y: auto;
  592. overflow-x:hidden;
  593. }
  594. .el-scrollbar__thumb {
  595. // 可设置滚动条颜色
  596. background: red;
  597. }
  598. }
  599. }
  600. }
  601. .amap-layers .amap-markers{
  602. z-index: 320 !important;
  603. }
  604. </style>