AMap.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <el-container>
  3. <el-header height="60">
  4. <span style="float: left; padding: 5px; color: red">点击地图上任意点触发操作</span>
  5. <el-button style="float: right; padding: 5px" type="text" @click="clearCircle">清除圆形</el-button>
  6. <el-button style="float: right; padding: 5px" type="text" @click="onPathNavigator">路径巡航</el-button>
  7. <el-button style="float: right; padding: 5px" type="text" @click="addPath">{{ addPathText }}</el-button>
  8. </el-header>
  9. <el-main>
  10. <div id="container" class="text item amap-wrapper" />
  11. </el-main>
  12. <el-dialog :visible.sync="showPositionDialog" width="30%" center>
  13. <el-card class="box-card" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  14. <div slot="header" class="clearfix">
  15. <span>操作</span>
  16. <el-button style="float: right; padding: 5px" type="text" @click="onSavePosition">保存坐标</el-button>
  17. <el-button v-if="!showInput" style="float: right; padding: 5px" type="text" @click="onDrawCircle">绘制圆形</el-button>
  18. <el-button style="float: right; padding: 5px" type="text" @click="onSaveMyPosition">保存为我的位置</el-button>
  19. </div>
  20. <div class="text item">
  21. <el-row>
  22. <span style="color: #0a84ff">
  23. 选中的坐标: <span style="color: red">({{ positionForm.lng }}, {{ positionForm.lat }})</span>
  24. </span>
  25. </el-row>
  26. <el-row>
  27. <span v-if="showInput">
  28. 选择半径(单位米):
  29. <el-input-number v-model="radius" :min="0" :max="10000" style="margin-left: 5px" />
  30. <el-button style="float: right; padding: 5px" type="text" @click="handleNumChange">确定半径</el-button>
  31. </span>
  32. </el-row>
  33. </div>
  34. </el-card>
  35. </el-dialog>
  36. </el-container>
  37. </template>
  38. <script>
  39. import AMapLoader from '@amap/amap-jsapi-loader'
  40. import { addGeoPosition, addMyPosition, getGeoPoint } from '@/api/map'
  41. export default {
  42. name: 'AMap',
  43. data() {
  44. return {
  45. amap: null,
  46. plugins: ['Scale'],
  47. mapCenter: [114.0000, 30.0000],
  48. // zoom=16 的比例尺为 100m
  49. zoom: 16,
  50. // zoom=6 的比例尺为 100km
  51. zoom1: 6,
  52. province: '',
  53. provinces: [],
  54. city: '',
  55. citys: [],
  56. district: '',
  57. districts: [],
  58. path1: [{
  59. path: [
  60. [116.361904, 39.913423],
  61. [116.367904, 39.913423]
  62. ]
  63. }],
  64. path: [],
  65. mapCircle: null,
  66. circleEditor: null,
  67. showPositionDialog: false,
  68. showInput: false,
  69. radius: 1000,
  70. positionForm: {
  71. lng: null,
  72. lat: null
  73. },
  74. pointArr: [],
  75. mapKeys: {
  76. securityJsCode: '983d6ee43bab3edf3693e91508f94aa9',
  77. key: '7b75ab2839ce68b884c7a682501ea774'
  78. },
  79. addPathText: '添加路径',
  80. enableAddPath: false,
  81. pathPointList: []
  82. }
  83. },
  84. mounted() {
  85. this.initAMap()
  86. },
  87. unmounted() {
  88. this.map?.destroy()
  89. },
  90. created() {
  91. document.title = '地图'
  92. },
  93. methods: {
  94. initAMap() {
  95. window._AMapSecurityConfig = {
  96. securityJsCode: this.mapKeys.securityJsCode
  97. }
  98. AMapLoader.load({
  99. key: this.mapKeys.key, // 申请好的Web端开发者Key,首次调用 load 时必填
  100. version: '1.4.15', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  101. plugins: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar',
  102. 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor', 'AMap.MassMarks', 'AMap.Size',
  103. 'AMap.Pixel'], // 需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
  104. AMapUI: { // 重点就是这个
  105. version: '1.0',
  106. plugins: ['misc/PathSimplifier', 'overlay/SimpleMarker']// SimpleMarker设置自定义图标,PathSimplifier轨迹展示组件
  107. }
  108. }).then((AMap) => {
  109. // 设置地图容器id
  110. this.map = new AMap.Map('container', {
  111. viewMode: '2D', // 是否为3D地图模式
  112. zoom: this.zoom, // 初始化地图级别
  113. center: [104.065753, 30.657462] // 初始化地图中心点位置
  114. })
  115. const that = this
  116. // 点击事件
  117. this.map.on('click', (e) => {
  118. // 获取经纬度
  119. var lng = e.lnglat.getLng()
  120. var lat = e.lnglat.getLat()
  121. const pointArr = [lng, lat]
  122. if (that.enableAddPath) {
  123. const point = { lng: lng, lat: lat }
  124. this.pathPointList.push(point)
  125. this.$message.info('add point -> (' + lng + ', ' + lat + ')')
  126. } else {
  127. that.pointArr = pointArr
  128. that.positionForm.lng = lng
  129. that.positionForm.lat = lat
  130. that.showPositionDialog = true
  131. }
  132. })
  133. // 缩放事件
  134. this.map.on('zoomchange', (e) => {
  135. console.log('当前缩放级别: ' + this.map.getZoom())
  136. })
  137. }).catch((e) => {
  138. console.log(e)
  139. })
  140. },
  141. // 轨迹巡航
  142. loadPathSimplifier() {
  143. AMapUI.load(['ui/misc/PathSimplifier'], (PathSimplifier) => {
  144. if (!PathSimplifier.supportCanvas) {
  145. alert('当前环境不支持 Canvas!')
  146. return
  147. }
  148. // 创建组件实例
  149. var pathSimplifierIns = new PathSimplifier({
  150. map: this.map,
  151. zIndex: 100, // 图层叠加顺序
  152. data: this.path, // 巡航路径
  153. // 获取巡航路径中的路径坐标数组
  154. getPath: (pathData, pathIndex) => {
  155. return pathData.path
  156. }
  157. })
  158. // 创建巡航器
  159. var pathNavigator = pathSimplifierIns.createPathNavigator(0, {
  160. loop: false, // 是否循环
  161. speed: 2000 // 速度(km/h)
  162. })
  163. pathNavigator.start()
  164. })
  165. },
  166. getDistrict(map) {
  167. // 创建行政区查询对象
  168. var district = new AMap.DistrictSearch({
  169. // 返回行政区边界坐标等具体信息
  170. extensions: 'all',
  171. // 设置查询行政区级别为 区
  172. level: 'street',
  173. subdistrict: 3
  174. })
  175. this.zoom = 11
  176. const area = '双流'
  177. district.search(area, function(status, result) {
  178. var bounds = result.districtList[0].boundaries
  179. var polygons = []
  180. if (bounds) {
  181. for (var i = 0, l = bounds.length; i < l; i++) {
  182. // 生成行政区划polygon
  183. var polygon = new AMap.Polygon({
  184. map: map,
  185. strokeWeight: 1,
  186. path: bounds[i],
  187. fillOpacity: 0.7,
  188. fillColor: '#CCF3FF',
  189. strokeColor: '#CC66CC'
  190. })
  191. polygons.push(polygon)
  192. }
  193. // 地图自适应
  194. map.setFitView()
  195. }
  196. })
  197. },
  198. drawCircle(pointArr, radius) {
  199. // 设置圆形位置
  200. // var center = new AMap.LngLat(104.065753, 30.657462)
  201. var center = new AMap.LngLat(pointArr[0], pointArr[1])
  202. // 设置圆的半径大小
  203. // var radius = 1000
  204. // 创建圆形 Circle 实例
  205. var circle = new AMap.Circle({
  206. center: center, // 圆心
  207. radius: radius, // 半径
  208. borderWeight: 3, // 描边的宽度
  209. strokeColor: '#ff3333', // 轮廓线颜色
  210. strokeOpacity: 1, // 轮廓线透明度
  211. strokeWeight: 1, // 轮廓线宽度
  212. fillOpacity: 0.4, // 圆形填充透明度
  213. strokeStyle: 'line', // 轮廓线样式
  214. strokeDasharray: [10, 10],
  215. fillColor: '#1791fc', // 圆形填充颜色
  216. zIndex: 50 // 圆形的叠加顺序
  217. })
  218. // 圆形 Circle 对象添加到 Map
  219. this.map.add(circle)
  220. // 根据覆盖物范围调整视野
  221. this.map.setFitView([circle])
  222. this.mapCircle = circle
  223. // 鼠标移入事件
  224. circle.on('mouseover', function() {
  225. console.log('鼠标移入')
  226. })
  227. var that = this
  228. // 引入圆形编辑器插件
  229. this.map.plugin(['AMap.CircleEditor'], function() {
  230. // 实例化圆形编辑器,传入地图实例和要进行编辑的圆形实例
  231. var circleEditor = new AMap.CircleEditor(that.map, circle)
  232. // 开启编辑模式
  233. circleEditor.open()
  234. that.circleEditor = circleEditor
  235. })
  236. },
  237. onPathNavigator() {
  238. getGeoPoint().then(resp => {
  239. if (resp.code === 0) {
  240. const pathList = resp.data
  241. if (pathList.length === 0) {
  242. return
  243. }
  244. this.path = pathList
  245. AMapUI.load(['ui/misc/PathSimplifier'], (PathSimplifier) => {
  246. if (!PathSimplifier.supportCanvas) {
  247. alert('当前环境不支持 Canvas!')
  248. return
  249. }
  250. // 创建组件实例
  251. var pathSimplifierIns = new PathSimplifier({
  252. map: this.map,
  253. zIndex: 100, // 图层叠加顺序
  254. data: this.path, // 巡航路径
  255. // 获取巡航路径中的路径坐标数组
  256. getPath: (pathData, pathIndex) => {
  257. return pathData.path
  258. }
  259. })
  260. // 创建巡航器
  261. var pathNavigator = pathSimplifierIns.createPathNavigator(0, {
  262. loop: false, // 是否循环
  263. speed: 2000 // 速度(km/h)
  264. })
  265. pathNavigator.start()
  266. })
  267. }
  268. })
  269. },
  270. addPath() {
  271. if (!this.enableAddPath) {
  272. this.enableAddPath = true
  273. this.addPathText = '完成添加'
  274. } else {
  275. this.enableAddPath = false
  276. this.addPathText = '添加路径'
  277. console.log(this.pathPointList)
  278. const pathTrail = { path: [] }
  279. for (const item of this.pathPointList) {
  280. const arr = []
  281. arr.push(item.lng)
  282. arr.push(item.lat)
  283. pathTrail.path.push(arr)
  284. }
  285. this.path.push(pathTrail)
  286. }
  287. },
  288. onSavePosition() {
  289. this.showPositionDialog = false
  290. addGeoPosition(this.positionForm).then(resp => {
  291. if (resp.code !== 0) {
  292. this.$message.info('添加坐标失败')
  293. }
  294. })
  295. },
  296. onSaveMyPosition() {
  297. this.showPositionDialog = false
  298. addMyPosition(this.positionForm).then(resp => {
  299. if (resp.code !== 0) {
  300. this.$message.info('设置位置失败')
  301. }
  302. }).catch(error => {
  303. this.$notify({
  304. message: error.message,
  305. type: 'warning',
  306. duration: 1000
  307. })
  308. })
  309. },
  310. onDrawCircle() {
  311. this.showInput = true
  312. },
  313. handleNumChange() {
  314. if (this.radius < 10) {
  315. this.$message.error('半径至少应大于 10m')
  316. return
  317. }
  318. this.showInput = false
  319. this.showPositionDialog = false
  320. console.log(this.pointArr)
  321. this.drawCircle(this.pointArr, this.radius)
  322. },
  323. clearCircle() {
  324. this.$message.info('清除图形')
  325. if (this.mapCircle !== null) {
  326. this.map.remove(this.mapCircle)
  327. this.mapCircle = null
  328. }
  329. if (this.circleEditor !== null) {
  330. this.circleEditor.close()
  331. this.circleEditor = null
  332. }
  333. }
  334. }
  335. }
  336. </script>
  337. <style>
  338. .amap-wrapper {
  339. width: 100%;
  340. height: 600px;
  341. }
  342. </style>