AMap.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <el-container class="map-sub-container">
  3. <el-header height="50px" class="operation-header">
  4. <div class="left-tip">
  5. <i class="el-icon-info" />
  6. <span>点击地图触发操作</span>
  7. </div>
  8. <div class="right-actions">
  9. <el-button
  10. :type="enableAddPath ? 'success' : 'primary'"
  11. size="small"
  12. icon="el-icon-plus"
  13. @click="addPath"
  14. >
  15. {{ addPathText }}
  16. </el-button>
  17. <el-button type="info" size="small" icon="el-icon-guide" @click="onPathNavigator">
  18. 路径巡航
  19. </el-button>
  20. <el-button type="danger" size="small" plain icon="el-icon-delete" @click="clearCircle">
  21. 清除圆形
  22. </el-button>
  23. </div>
  24. </el-header>
  25. <el-main class="map-main-wrapper">
  26. <div id="container" class="amap-instance" />
  27. </el-main>
  28. <el-dialog
  29. :visible.sync="showPositionDialog"
  30. width="400px"
  31. custom-class="custom-map-dialog"
  32. center
  33. append-to-body
  34. >
  35. <div slot="title" class="dialog-title">
  36. <i class="el-icon-location-information"></i>
  37. <span>位置详情与操作</span>
  38. </div>
  39. <div class="dialog-body">
  40. <div class="coord-display">
  41. <div class="label">选定坐标:</div>
  42. <div class="value" v-if="positionForm.lng !== null && positionForm.lat !== null">
  43. {{ positionForm.lng.toFixed(6) }}, {{ positionForm.lat.toFixed(6) }}
  44. </div>
  45. <div class="value" v-else>-- , --</div>
  46. </div>
  47. <el-divider></el-divider>
  48. <div v-if="showInput" class="radius-setter">
  49. <p class="input-tip">设置绘制半径 (米)</p>
  50. <div class="input-row">
  51. <el-input-number
  52. v-model="radius"
  53. :min="10"
  54. :max="10000"
  55. controls-position="right"
  56. style="width: 100%"
  57. />
  58. <el-button type="primary" @click="handleNumChange" style="margin-top: 15px; width: 100%">确定半径并绘制</el-button>
  59. </div>
  60. </div>
  61. <div v-else class="action-grid">
  62. <el-button icon="el-icon-document-add" @click="onSavePosition">保存坐标</el-button>
  63. <el-button icon="el-icon-magic-stick" type="success" @click="onDrawCircle">绘制圆形</el-button>
  64. <el-button icon="el-icon-star-off" type="warning" @click="onSaveMyPosition">设为我的位置</el-button>
  65. </div>
  66. </div>
  67. </el-dialog>
  68. </el-container>
  69. </template>
  70. <script>
  71. import AMapLoader from '@amap/amap-jsapi-loader'
  72. import {addGeoPosition, addMyPosition, addPath, getGeoPoint} from '@/api/map'
  73. export default {
  74. name: 'AMap',
  75. data() {
  76. return {
  77. amap: null,
  78. plugins: ['Scale'],
  79. mapCenter: [114.0000, 30.0000],
  80. // zoom=16 的比例尺为 100m
  81. zoom: 16,
  82. // zoom=6 的比例尺为 100km
  83. zoom1: 6,
  84. province: '',
  85. provinces: [],
  86. city: '',
  87. citys: [],
  88. district: '',
  89. districts: [],
  90. path1: [{
  91. path: [
  92. [116.361904, 39.913423],
  93. [116.367904, 39.913423]
  94. ]
  95. }],
  96. path: [],
  97. mapCircle: null,
  98. circleEditor: null,
  99. showPositionDialog: false,
  100. showInput: false,
  101. radius: 1000,
  102. positionForm: {
  103. lng: null,
  104. lat: null
  105. },
  106. pointArr: [],
  107. mapKeys: {
  108. securityJsCode: '983d6ee43bab3edf3693e91508f94aa9',
  109. key: '7b75ab2839ce68b884c7a682501ea774'
  110. },
  111. addPathText: '添加路径',
  112. enableAddPath: false,
  113. pathPointList: []
  114. }
  115. },
  116. mounted() {
  117. this.initAMap()
  118. },
  119. unmounted() {
  120. this.map?.destroy()
  121. },
  122. created() {
  123. document.title = '地图'
  124. },
  125. methods: {
  126. initAMap() {
  127. window._AMapSecurityConfig = {
  128. securityJsCode: this.mapKeys.securityJsCode
  129. }
  130. AMapLoader.load({
  131. key: this.mapKeys.key, // 申请好的Web端开发者Key,首次调用 load 时必填
  132. version: '1.4.15', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  133. plugins: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar',
  134. 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor', 'AMap.MassMarks', 'AMap.Size',
  135. 'AMap.Pixel'], // 需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
  136. AMapUI: { // 重点就是这个
  137. version: '1.0',
  138. plugins: ['misc/PathSimplifier', 'overlay/SimpleMarker']// SimpleMarker设置自定义图标,PathSimplifier轨迹展示组件
  139. }
  140. }).then((AMap) => {
  141. // 设置地图容器id
  142. this.map = new AMap.Map('container', {
  143. viewMode: '2D', // 是否为3D地图模式
  144. zoom: this.zoom, // 初始化地图级别
  145. center: [104.065753, 30.657462] // 初始化地图中心点位置
  146. })
  147. const that = this
  148. // 点击事件
  149. this.map.on('click', (e) => {
  150. // 获取经纬度
  151. var lng = e.lnglat.getLng()
  152. var lat = e.lnglat.getLat()
  153. const pointArr = [lng, lat]
  154. if (that.enableAddPath) {
  155. const point = { lng: lng, lat: lat }
  156. this.pathPointList.push(point)
  157. this.$message.info('add point -> (' + lng + ', ' + lat + ')')
  158. } else {
  159. that.pointArr = pointArr
  160. that.positionForm.lng = lng
  161. that.positionForm.lat = lat
  162. that.showPositionDialog = true
  163. }
  164. })
  165. // 缩放事件
  166. this.map.on('zoomchange', (e) => {
  167. console.log('当前缩放级别: ' + this.map.getZoom())
  168. })
  169. // this.onPathNavigator()
  170. }).catch((e) => {
  171. console.log(e)
  172. })
  173. },
  174. getDistrict(map) {
  175. // 创建行政区查询对象
  176. var district = new AMap.DistrictSearch({
  177. // 返回行政区边界坐标等具体信息
  178. extensions: 'all',
  179. // 设置查询行政区级别为 区
  180. level: 'street',
  181. subdistrict: 3
  182. })
  183. this.zoom = 11
  184. const area = '双流'
  185. district.search(area, function(status, result) {
  186. var bounds = result.districtList[0].boundaries
  187. var polygons = []
  188. if (bounds) {
  189. for (var i = 0, l = bounds.length; i < l; i++) {
  190. // 生成行政区划polygon
  191. var polygon = new AMap.Polygon({
  192. map: map,
  193. strokeWeight: 1,
  194. path: bounds[i],
  195. fillOpacity: 0.7,
  196. fillColor: '#CCF3FF',
  197. strokeColor: '#CC66CC'
  198. })
  199. polygons.push(polygon)
  200. }
  201. // 地图自适应
  202. map.setFitView()
  203. }
  204. })
  205. },
  206. drawCircle(pointArr, radius) {
  207. // 设置圆形位置
  208. // var center = new AMap.LngLat(104.065753, 30.657462)
  209. var center = new AMap.LngLat(pointArr[0], pointArr[1])
  210. // 设置圆的半径大小
  211. // var radius = 1000
  212. // 创建圆形 Circle 实例
  213. var circle = new AMap.Circle({
  214. center: center, // 圆心
  215. radius: radius, // 半径
  216. borderWeight: 3, // 描边的宽度
  217. strokeColor: '#ff3333', // 轮廓线颜色
  218. strokeOpacity: 1, // 轮廓线透明度
  219. strokeWeight: 1, // 轮廓线宽度
  220. fillOpacity: 0.4, // 圆形填充透明度
  221. strokeStyle: 'line', // 轮廓线样式
  222. strokeDasharray: [10, 10],
  223. fillColor: '#1791fc', // 圆形填充颜色
  224. zIndex: 50 // 圆形的叠加顺序
  225. })
  226. // 圆形 Circle 对象添加到 Map
  227. this.map.add(circle)
  228. // 根据覆盖物范围调整视野
  229. this.map.setFitView([circle])
  230. this.mapCircle = circle
  231. // 鼠标移入事件
  232. circle.on('mouseover', function() {
  233. console.log('鼠标移入')
  234. })
  235. var that = this
  236. // 引入圆形编辑器插件
  237. this.map.plugin(['AMap.CircleEditor'], function() {
  238. // 实例化圆形编辑器,传入地图实例和要进行编辑的圆形实例
  239. var circleEditor = new AMap.CircleEditor(that.map, circle)
  240. // 开启编辑模式
  241. circleEditor.open()
  242. that.circleEditor = circleEditor
  243. })
  244. },
  245. // 轨迹巡航
  246. onPathNavigator() {
  247. getGeoPoint().then(resp => {
  248. if (resp.code === 0) {
  249. const pathList = resp.data
  250. if (pathList.length === 0) {
  251. return
  252. }
  253. this.path = pathList
  254. AMapUI.load(['ui/misc/PathSimplifier'], (PathSimplifier) => {
  255. if (!PathSimplifier.supportCanvas) {
  256. alert('当前环境不支持 Canvas!')
  257. return
  258. }
  259. // 创建组件实例
  260. var pathSimplifierIns = new PathSimplifier({
  261. map: this.map,
  262. zIndex: 100, // 图层叠加顺序
  263. data: this.path, // 巡航路径
  264. // 获取巡航路径中的路径坐标数组
  265. getPath: (pathData, pathIndex) => {
  266. return pathData.path
  267. }
  268. })
  269. // 创建巡航器
  270. var pathNavigator = pathSimplifierIns.createPathNavigator(0, {
  271. loop: false, // 是否循环
  272. speed: 3600 // 速度(km/h)
  273. })
  274. pathNavigator.start()
  275. })
  276. }
  277. })
  278. },
  279. addPath() {
  280. if (!this.enableAddPath) {
  281. this.enableAddPath = true
  282. this.addPathText = '完成添加'
  283. } else {
  284. this.enableAddPath = false
  285. this.addPathText = '添加路径'
  286. addPath(this.pathPointList).then(resp => {
  287. if (resp.code === 0) {
  288. }
  289. }).finally(() => {
  290. this.pathPointList = []
  291. })
  292. const pathTrail = { path: [] }
  293. for (const item of this.pathPointList) {
  294. const arr = []
  295. arr.push(item.lng)
  296. arr.push(item.lat)
  297. pathTrail.path.push(arr)
  298. }
  299. this.path.push(pathTrail)
  300. }
  301. },
  302. onSavePosition() {
  303. this.showPositionDialog = false
  304. addGeoPosition(this.positionForm).then(resp => {
  305. if (resp.code !== 0) {
  306. this.$message.info('添加坐标失败')
  307. }
  308. })
  309. },
  310. onSaveMyPosition() {
  311. this.showPositionDialog = false
  312. addMyPosition(this.positionForm).then(resp => {
  313. if (resp.code !== 0) {
  314. this.$message.info('设置位置失败')
  315. }
  316. }).catch(error => {
  317. this.$notify({
  318. message: error.message,
  319. type: 'warning',
  320. duration: 1000
  321. })
  322. })
  323. },
  324. onDrawCircle() {
  325. this.showInput = true
  326. },
  327. handleNumChange() {
  328. if (this.radius < 10) {
  329. this.$message.error('半径至少应大于 10m')
  330. return
  331. }
  332. this.showInput = false
  333. this.showPositionDialog = false
  334. console.log(this.pointArr)
  335. this.drawCircle(this.pointArr, this.radius)
  336. },
  337. clearCircle() {
  338. this.$message.info('清除图形')
  339. if (this.mapCircle !== null) {
  340. this.map.remove(this.mapCircle)
  341. this.mapCircle = null
  342. }
  343. if (this.circleEditor !== null) {
  344. this.circleEditor.close()
  345. this.circleEditor = null
  346. }
  347. }
  348. }
  349. }
  350. </script>
  351. <style scoped lang="scss">
  352. .map-sub-container {
  353. height: 100%;
  354. background-color: #fff;
  355. border-radius: 8px;
  356. overflow: hidden;
  357. box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
  358. }
  359. .operation-header {
  360. display: flex;
  361. align-items: center;
  362. justify-content: space-between;
  363. background-color: #fcfcfc;
  364. border-bottom: 1px solid #ebeef5;
  365. padding: 0 15px !important;
  366. .left-tip {
  367. color: #f56c6c;
  368. font-size: 13px;
  369. display: flex;
  370. align-items: center;
  371. i { margin-right: 5px; }
  372. }
  373. .right-actions {
  374. display: flex;
  375. gap: 10px;
  376. }
  377. }
  378. .map-main-wrapper {
  379. padding: 0 !important;
  380. position: relative;
  381. }
  382. .amap-instance {
  383. width: 100%;
  384. height: calc(100vh - 180px); // 动态计算高度,适配父级 MapIndex
  385. }
  386. /* 对话框美化 */
  387. ::v-deep .custom-map-dialog {
  388. border-radius: 12px;
  389. .el-dialog__header {
  390. padding: 20px;
  391. border-bottom: 1px solid #f2f6fc;
  392. }
  393. .el-dialog__body {
  394. padding: 10px 25px 30px;
  395. }
  396. }
  397. .dialog-title {
  398. display: flex;
  399. align-items: center;
  400. justify-content: center;
  401. gap: 8px;
  402. font-weight: bold;
  403. color: #303133;
  404. }
  405. .coord-display {
  406. background: #f0f7ff;
  407. padding: 15px;
  408. border-radius: 8px;
  409. text-align: center;
  410. .label { font-size: 12px; color: #79bbff; margin-bottom: 5px; }
  411. .value { font-family: 'Courier New', Courier, monospace; font-weight: bold; color: #409EFF; font-size: 16px; }
  412. }
  413. .action-grid {
  414. display: flex;
  415. flex-direction: column;
  416. gap: 10px;
  417. .el-button {
  418. margin-left: 0 !important;
  419. justify-content: flex-start;
  420. }
  421. }
  422. .radius-setter {
  423. .input-tip { font-size: 13px; color: #909399; margin-bottom: 10px; }
  424. }
  425. /* 按钮悬浮动画 */
  426. .el-button {
  427. transition: all 0.2s ease;
  428. &:hover {
  429. transform: translateY(-1px);
  430. box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  431. }
  432. }
  433. </style>