myMapHtml.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <div :style="{width: '100%', height:mainAreaHeight+'px'}">
  3. <div v-if="mapUrl">
  4. <iframe v-bind:src="mapUrl" frameborder="0" style="width: 100%; height: 100%"></iframe>
  5. </div>
  6. <div v-else>
  7. <baidu-map v-if="isShow" class="map" :style="{height: mainAreaHeight+'px'}" :center="center" :zoom="zoom" scroll-wheel-zoom @ready="handler"
  8. @click="clickMap">
  9. <bm-control>
  10. <div style="margin: 20px">
  11. <el-tooltip class="item" effect="dark" :content="$t('tcpType.LOCATION')" placement="right-start">
  12. <el-button type="success" icon="el-icon-location-outline" :disabled="disable" circle plain
  13. @click="changeType(0)"></el-button>
  14. </el-tooltip>
  15. <el-tooltip class="item" effect="dark" content="刷新" placement="right-start">
  16. <el-button type="primary" icon="el-icon-refresh-left" :disabled="disable" circle plain
  17. @click="changeType(1)"></el-button>
  18. </el-tooltip>
  19. <el-tooltip class="item" effect="dark" :content="$t('customerManage.footprint')" placement="right-start">
  20. <el-button type="warning" icon="el-icon-rank" circle plain @click="changeType(-1)"></el-button>
  21. </el-tooltip>
  22. <el-tooltip class="item" effect="dark" content="安全围栏" placement="right-start">
  23. <el-button type="danger" icon="el-icon-s-help" circle plain @click="changeType(2)"></el-button>
  24. </el-tooltip>
  25. </div>
  26. </bm-control>
  27. <!-- <bm-control>-->
  28. <!-- <div style="margin: 20px">-->
  29. <!-- <button @click="addZoom(19)">缩放至最大</button>-->
  30. <!-- <button @click="addZoom(10)">还原</button>-->
  31. <!-- <button @click="addZoom(3)">缩放至最小</button>-->
  32. <!-- </div>-->
  33. <!-- </bm-control>-->
  34. <div v-if="type === -1">
  35. <!-- 轨迹 -->
  36. <!-- 添加起点标记 -->
  37. <bm-marker v-if="dataList.length > 1" :position="endPoint" @mouseover="clickMap">
  38. <bm-label content="终点" :offset="{width: 0, height: 30}"/>
  39. <bm-info-window v-if="myContent" :show="true" @close="infoWindowClose">
  40. <p>{{ unixDateFormatter(dataList[dataList.length - 1].position_time) }}</p>
  41. <p style="color: #4abe84">{{ myContent }}</p>
  42. </bm-info-window>
  43. </bm-marker>
  44. <!-- 添加终点标记 -->
  45. <bm-marker :position="markers[0]">
  46. <bm-label content="起点" :offset="{width: 0, height: 30}"/>
  47. </bm-marker>
  48. <!-- 折线 -->
  49. <bm-polyline :path="markers" stroke-color="#4aa4f3" :stroke-opacity="1" :stroke-weight="6"
  50. stroke-style="dashed"
  51. @lineupdate="updatePolylinePath"/>
  52. <!-- <bm-polyline :path="markers" stroke-color="red" :stroke-opacity="0.5" :stroke-weight="2"></bm-polyline>-->
  53. <bml-lushu @stop="reset" :path="markers" :icon="icon" :play="play" :autoView="true" :speed="700"></bml-lushu>
  54. <!-- 轨迹 -->
  55. </div>
  56. <div v-else-if="type === 2">
  57. <!-- 圆形围栏 -->
  58. <bm-circle v-if="boolCircle" :center="circleCenter" :radius="securityFence.radius" stroke-color="red"
  59. :stroke-opacity="0.5" :stroke-weight="2" @lineupdate="updateCirclePath"
  60. :editing="editing"></bm-circle>
  61. <bm-marker v-if="boolCircle" :position="circleCenter">
  62. <bm-info-window :show="true">
  63. <p>
  64. <el-input v-model="securityFence.address"/>
  65. </p>
  66. <div style="color: #4abe84">半径:{{ securityFence.radius }} 米
  67. <el-button type="primary" icon="el-icon-edit" size="mini" plain @click="API_SaveFence">保存</el-button>
  68. <el-button v-if="securityFence.id" type="danger" icon="el-icon-delete" size="mini" plain
  69. @click="API_DelFence">删除
  70. </el-button>
  71. </div>
  72. </bm-info-window>
  73. </bm-marker>
  74. <!-- 圆形围栏 -->
  75. </div>
  76. <div v-else>
  77. <bm-marker :position="markers[0]" animation="BMAP_ANIMATION_BOUNCE" @mouseover="mouseoverMap">
  78. <!-- <bm-label content="我爱北京天安门" :labelStyle="{color: 'red', fontSize : '24px'}" :offset="{width: -35, height: 30}"/>-->
  79. <bm-info-window v-if="myContent" :show="true" @close="infoWindowClose">
  80. <p>{{ unixDateFormatter(dataList[0].position_time) }}</p>
  81. <p style="color: #4abe84">{{ myContent }}</p>
  82. </bm-info-window>
  83. <bm-label content="最后位置" :labelStyle="{color: 'red', fontSize : '14px'}" :offset="{width: -20, height: 30}"/>
  84. </bm-marker>
  85. </div>
  86. <!-- <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="false" :autoLocation="true" @locationSuccess="locationSuccess"></bm-geolocation>-->
  87. </baidu-map>
  88. </div>
  89. </div>
  90. </template>
  91. <script>
  92. import walkImg from '@/icons/images/walk.png'
  93. import {BmlLushu} from 'vue-baidu-map'
  94. import {getLocationList} from '@/api/initialize'
  95. import {unix2Date} from '@/utils/Foundation'
  96. import * as API_MemberLocation from '@/api/member_location'
  97. import {getDeviceList} from '@/api/ncs_device'
  98. import {DEVICE_TYPE} from '@/utils/enum/DeviceTypeEnum'
  99. export default {
  100. name: "myMapHtml",
  101. components: {
  102. BmlLushu
  103. },
  104. data() {
  105. return {
  106. mapUrl: null,
  107. uuid: null,
  108. frameId: null,
  109. center: '北京',
  110. zoom: 16,
  111. isShow: false,
  112. markers: [{
  113. lng: 113.971151,
  114. lat: 22.570388
  115. }],
  116. play: false, //是否行进
  117. icon: { // 覆盖物的图标
  118. url: walkImg,
  119. size: {width: 220, height: 220},
  120. opts: {anchor: {width: 0, height: 113}}
  121. },
  122. boolCircle: false,
  123. type: 1,
  124. myContent: '',
  125. params: {
  126. page_size: 1,
  127. page_no: 1,
  128. sort: 'position_time',
  129. dir: 'desc',
  130. },
  131. dataList: [],
  132. disable: false,
  133. deviceList: [],
  134. circleCenter: {
  135. lng: 113.971151,
  136. lat: 22.570388
  137. },
  138. securityFence: {
  139. radius: 200
  140. },
  141. editing: false
  142. }
  143. },
  144. created() {
  145. },
  146. computed: {
  147. //终点计算
  148. endPoint() {
  149. if (this.markers && this.markers.length > 0) {
  150. return this.markers[this.markers.length - 1]
  151. }
  152. return null;
  153. }
  154. },
  155. mounted() {
  156. this.mapUrl = this.$route.query.mapUrl
  157. if (!this.mapUrl) {
  158. this.uuid = this.$route.query.uuid
  159. this.frameId = this.$route.query.frameId
  160. this.params.fixedCondition = " member_union_id = '" + this.uuid + "'"
  161. this.handleMapData()
  162. this.API_GetDeviceList()
  163. }
  164. },
  165. methods: {
  166. //处理数据
  167. handleMapData() {
  168. let markers = []
  169. this.myContent = null
  170. getLocationList(this.params).then(res => {
  171. this.disable = false
  172. this.dataList = res.data
  173. this.dataList.sort(function (a, b) {
  174. return a.position_time - b.position_time
  175. })
  176. if (res.data.length > 0) {
  177. this.dataList.forEach(item => {
  178. markers.push({lng: item.longitude, lat: item.latitude})
  179. })
  180. // const geoCoder = new BMap.Geocoder()
  181. // // 利用坐标获取地址的详细信息
  182. // geoCoder.getLocation(e.point, (res) => {
  183. // console.log(res)
  184. // if (res.surroundingPois[0]) {
  185. // this.myContent = res.surroundingPois[0].title + '附近'
  186. // } else {
  187. // this.myContent = res.address + '附近'
  188. // }
  189. // console.log('地址为' + this.myContent)
  190. // })
  191. this.markers = markers
  192. this.center = this.markers[0]
  193. }
  194. this.play = true
  195. this.isShow = true
  196. })
  197. },
  198. locationSuccess({point, AddressComponent, marker}){
  199. console.log('locationSuccess', point, AddressComponent, marker)
  200. this.center={...point}
  201. },
  202. handler({BMap, map}) {
  203. if (this.markers.length > 0 && this.type !== 2) {
  204. let view = map.getViewport(eval(this.markers))
  205. this.zoom = view.zoom
  206. console.log('zoom===', this.zoom)
  207. }
  208. },
  209. async API_GetDeviceList() {
  210. getDeviceList(this.frameId, DEVICE_TYPE.WATCH_IW).then(res => {
  211. this.deviceList = res
  212. })
  213. },
  214. // 停止本次移动
  215. reset() {
  216. this.play = false
  217. },
  218. // 覆盖物的属性发生变化时触发
  219. updatePolylinePath(e) {
  220. this.markers = e.target.getPath()
  221. },
  222. updateCirclePath(e) {
  223. this.circleCenter = e.target.getCenter()
  224. this.securityFence.radius = Math.ceil(e.target.getRadius())
  225. console.log('修改围栏参数:', this.circleCenter, this.securityFence.radius)
  226. },
  227. mouseoverMap(e) {
  228. if (this.myContent) {
  229. return
  230. }
  231. const geoCoder = new BMap.Geocoder()
  232. // 利用坐标获取地址的详细信息
  233. geoCoder.getLocation(e.point, (res) => {
  234. console.log(res)
  235. if (res.surroundingPois[0]) {
  236. this.myContent = res.surroundingPois[0].title + '附近'
  237. } else {
  238. this.myContent = res.address + '附近'
  239. }
  240. console.log('地址为' + this.myContent)
  241. })
  242. },
  243. clickMap(e) {
  244. console.log(e.point)
  245. if (this.type !== 2) {
  246. return
  247. }
  248. this.circleCenter = e.point
  249. this.boolCircle = false
  250. this.getAddress(e.point)
  251. },
  252. getAddress(point) {
  253. const geoCoder = new BMap.Geocoder()
  254. // 利用坐标获取地址的详细信息
  255. geoCoder.getLocation(point, (res) => {
  256. console.log(res)
  257. if (res.surroundingPois[0]) {
  258. this.securityFence.address = res.surroundingPois[0].title + '附近'
  259. } else {
  260. this.securityFence.address = res.address + '附近'
  261. }
  262. this.boolCircle = true
  263. console.log('circleContent地址为' + this.securityFence.address)
  264. })
  265. const _this = this
  266. if (!this.editing) {
  267. setTimeout(() => {
  268. _this.editing = true
  269. }, 1000)
  270. }
  271. },
  272. changeType(type) {
  273. switch (type) {
  274. case 0:
  275. this.type = 0
  276. if (this.deviceList.length === 0) {
  277. this.$message({
  278. message: '没有定位设备',
  279. type: 'info'
  280. })
  281. return
  282. }
  283. this.disable = true
  284. this.API_GetLocation()
  285. break
  286. case 1:
  287. this.type = 1
  288. this.disable = true
  289. this.params.page_size = 1
  290. this.isShow = false
  291. this.handleMapData()
  292. break
  293. case 2:
  294. if (this.deviceList.length === 0) {
  295. this.$message({
  296. message: '没有定位设备',
  297. type: 'info'
  298. })
  299. return
  300. }
  301. this.type = 2
  302. this.boolCircle = true
  303. this.API_GetFenceList()
  304. break
  305. default:
  306. this.type = -1
  307. this.params.page_size = 20
  308. this.isShow = false
  309. this.handleMapData()
  310. break
  311. }
  312. console.log(this.type)
  313. },
  314. infoWindowClose() {
  315. this.myContent = null
  316. this.circleCenter = null
  317. },
  318. /** 格式化时间函数 */
  319. unixDateFormatter(time) {
  320. return unix2Date(time * 1000)
  321. },
  322. API_GetLocation() {
  323. API_MemberLocation.getLocation(this.deviceList[0].eth_mac).then(res => {
  324. if (res.success) {
  325. if (Array.isArray(res.data)) {
  326. console.log(res.data)
  327. this.markers = [{lng: res.data[1] , lat:res.data[0]}]
  328. this.center = this.markers[0]
  329. console.log(this.markers)
  330. // const geoCoder = new BMap.Geocoder()
  331. // // 利用坐标获取地址的详细信息
  332. // geoCoder.getLocation(this.center, (res) => {
  333. // console.log(res)
  334. // if (res.surroundingPois[0]) {
  335. // this.myContent = res.surroundingPois[0].title + '附近'
  336. // } else {
  337. // this.myContent = res.address + '附近'
  338. // }
  339. // console.log('地址为' + this.myContent)
  340. // })
  341. // this.play = true
  342. // this.isShow = true
  343. return;
  344. }
  345. const data = JSON.parse(res.data)
  346. if (data.Data.Code === 'iot.messagebroker.OFFLINE') {
  347. this.$message({
  348. message: '设备离线,操作失败',
  349. type: 'info'
  350. })
  351. this.disable = false
  352. return
  353. }
  354. this.$message({
  355. message: '正在定位,请稍后',
  356. type: 'info'
  357. })
  358. const _this = this
  359. setTimeout(() => {
  360. _this.params.page_size = 1
  361. _this.isShow = false
  362. _this.handleMapData()
  363. _this.disable = false
  364. }, 15000)
  365. } else {
  366. this.$message.error(this.$t('action.handleFailed'))
  367. this.disable = false
  368. }
  369. }).catch(err => {
  370. console.log('定位异常...', err)
  371. this.disable = false
  372. })
  373. },
  374. API_GetFenceList() {
  375. const data = {
  376. unionId: this.uuid,
  377. imei: this.deviceList[0].eth_mac,
  378. type: 0
  379. }
  380. const _this = this
  381. API_MemberLocation.getMemberSecurityFence(data).then(res => {
  382. if (res.length > 0) {
  383. _this.securityFence = res[0]
  384. _this.circleCenter = {lng: _this.securityFence.longitude, lat: _this.securityFence.latitude}
  385. _this.center = this.circleCenter
  386. _this.boolCircle = true
  387. } else {
  388. _this.securityFence = {
  389. radius: 200,
  390. imei: _this.deviceList[0].eth_mac,
  391. member_union_id: _this.uuid,
  392. type: 0,
  393. address: ''
  394. }
  395. _this.center = {lng: 113.971151, lat: 22.570388}
  396. if (_this.center['lng']) {
  397. _this.circleCenter = _this.center
  398. }
  399. }
  400. console.log('this.boolCircle==', _this.boolCircle, _this.circleCenter)
  401. }).catch(err => {
  402. console.log('获取围栏异常...', err)
  403. })
  404. },
  405. API_SaveFence() {
  406. if (this.securityFence.radius < 200) {
  407. this.$message({
  408. message: '围栏半径不能小于200',
  409. type: 'info'
  410. })
  411. return
  412. }
  413. if (this.securityFence.radius > 2000) {
  414. this.$message({
  415. message: '围栏半径不能超过2000米',
  416. type: 'info'
  417. })
  418. return
  419. }
  420. this.securityFence.longitude = this.circleCenter.lng
  421. this.securityFence.latitude = this.circleCenter.lat
  422. API_MemberLocation.saveMemberSecurityFence(this.securityFence).then(res => {
  423. this.$message.success(this.$t('action.handleSuccess'))
  424. }).catch(err => {
  425. console.log('设置围栏异常...', err)
  426. })
  427. },
  428. API_DelFence() {
  429. this.$confirm(this.$t('action.sureDelete'), this.$t('action.waring'), {
  430. confirmButtonText: this.$t('action.yes'),
  431. cancelButtonText: this.$t('action.cancel'),
  432. type: 'warning'
  433. }).then(() => {
  434. API_MemberLocation.delMemberSecurityFence(this.securityFence.id).then(res => {
  435. this.$message.success(this.$t('action.handleSuccess'))
  436. this.API_GetFenceList()
  437. }).catch(err => {
  438. console.log('删除围栏异常...', err)
  439. })
  440. }).catch(() => {
  441. this.$message({
  442. type: 'info',
  443. message: this.$t('action.cancelDelete')
  444. })
  445. })
  446. },
  447. }
  448. }
  449. </script>
  450. <style >
  451. .map img {
  452. max-width: none!important;
  453. background: none!important;
  454. }
  455. </style>