TrailMap.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <el-row class="movie-list">
  3. <el-col :md="24">
  4. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  5. <el-card class="box-card">
  6. <div slot="header" class="clearfix">
  7. <el-select
  8. v-model="province"
  9. size="small"
  10. style="width: 170px"
  11. placeholder="请选择省份(直辖市)"
  12. @change="changeProvince()"
  13. >
  14. <el-option
  15. v-for="item in provinces"
  16. :key="item.value"
  17. :label="item.label"
  18. :value="item.label"
  19. />
  20. </el-select> 省
  21. <el-select
  22. v-model="city"
  23. size="small"
  24. style="width: 160px;margin-left: 10px"
  25. placeholder="请选择城市"
  26. @change="changeCity"
  27. >
  28. <el-option
  29. v-for="item in citys"
  30. :key="item.value"
  31. :label="item.label"
  32. :value="item.label"
  33. />
  34. </el-select> 市
  35. <el-select
  36. v-model="district"
  37. size="small"
  38. style="width: 160px;margin-left: 10px"
  39. placeholder="请选择区(县)"
  40. @change="changeDistrict"
  41. >
  42. <el-option
  43. v-for="item in districts"
  44. :key="item.value"
  45. :label="item.label"
  46. :value="item.label"
  47. />
  48. </el-select> 区
  49. <el-button style="float: right; padding: 3px 0" type="text" @click="onClick">加载</el-button>
  50. </div>
  51. <div id="container" class="text item amap-wrapper" />
  52. </el-card>
  53. </el-row>
  54. </el-col>
  55. </el-row>
  56. </template>
  57. <script>
  58. import AMapLoader from '@amap/amap-jsapi-loader'
  59. import { addGeoPoint, getGeoPoint } from '@/api/map'
  60. export default {
  61. name: 'TrailMap',
  62. data() {
  63. return {
  64. amap: null,
  65. plugins: ['Scale'],
  66. mapCenter: [114.0000, 30.0000],
  67. // zoom=6 的比例尺为 100km
  68. zoom: 6,
  69. province: '',
  70. provinces: [],
  71. city: '',
  72. citys: [],
  73. district: '',
  74. districts: [],
  75. path1: [{
  76. path: [
  77. [116.361904, 39.913423],
  78. [116.367904, 39.913423]
  79. ]
  80. }],
  81. path: []
  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: ''
  97. }
  98. AMapLoader.load({
  99. 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: 6, // 初始化地图级别
  113. center: [114.0000, 30.0000] // 初始化地图中心点位置
  114. })
  115. this.map.on('click', (e) => {
  116. const geoPoint = {}
  117. geoPoint.lng = e.lnglat.getLng()
  118. geoPoint.lat = e.lnglat.getLat()
  119. addGeoPoint(geoPoint).then(resp => {
  120. if (resp.code === 0) {
  121. // 获取经纬度
  122. this.$message.info('map click: ' + e.lnglat.getLng() + ' ' + e.lnglat.getLat())
  123. }
  124. })
  125. })
  126. // this.loadPathSimplifier()
  127. // this.getDistrict(this.map)
  128. }).catch((e) => {
  129. console.log(e)
  130. })
  131. },
  132. zoomchange(e) {
  133. console.log('当前缩放级别: ' + this.amap.getZoom())
  134. },
  135. // 轨迹巡航
  136. loadPathSimplifier() {
  137. AMapUI.load(['ui/misc/PathSimplifier'], (PathSimplifier) => {
  138. if (!PathSimplifier.supportCanvas) {
  139. alert('当前环境不支持 Canvas!')
  140. return
  141. }
  142. // 创建组件实例
  143. var pathSimplifierIns = new PathSimplifier({
  144. map: this.map,
  145. zIndex: 100, // 图层叠加顺序
  146. data: this.path, // 巡航路径
  147. // 获取巡航路径中的路径坐标数组
  148. getPath: (pathData, pathIndex) => {
  149. return pathData.path
  150. }
  151. })
  152. // 创建巡航器
  153. var pathNavigator = pathSimplifierIns.createPathNavigator(0, {
  154. loop: false, // 是否循环
  155. speed: 2000 // 速度(km/h)
  156. })
  157. pathNavigator.start()
  158. })
  159. },
  160. getDistrict(map) {
  161. // 创建行政区查询对象
  162. var district = new AMap.DistrictSearch({
  163. // 返回行政区边界坐标等具体信息
  164. extensions: 'all',
  165. // 设置查询行政区级别为 区
  166. level: 'street',
  167. subdistrict: 3
  168. })
  169. this.zoom = 11
  170. const area = '双流'
  171. district.search(area, function(status, result) {
  172. var bounds = result.districtList[0].boundaries
  173. var polygons = []
  174. if (bounds) {
  175. for (var i = 0, l = bounds.length; i < l; i++) {
  176. // 生成行政区划polygon
  177. var polygon = new AMap.Polygon({
  178. map: map,
  179. strokeWeight: 1,
  180. path: bounds[i],
  181. fillOpacity: 0.7,
  182. fillColor: '#CCF3FF',
  183. strokeColor: '#CC66CC'
  184. })
  185. polygons.push(polygon)
  186. }
  187. // 地图自适应
  188. map.setFitView()
  189. }
  190. })
  191. },
  192. changeProvince() {
  193. },
  194. changeCity() {
  195. },
  196. changeDistrict() {
  197. },
  198. onClick() {
  199. this.$message.info('加载路径')
  200. getGeoPoint().then(resp => {
  201. if (resp.code === 0) {
  202. const pathList = resp.data
  203. if (pathList.length === 0) {
  204. return
  205. }
  206. this.path = pathList
  207. AMapUI.load(['ui/misc/PathSimplifier'], (PathSimplifier) => {
  208. if (!PathSimplifier.supportCanvas) {
  209. alert('当前环境不支持 Canvas!')
  210. return
  211. }
  212. // 创建组件实例
  213. var pathSimplifierIns = new PathSimplifier({
  214. map: this.map,
  215. zIndex: 100, // 图层叠加顺序
  216. data: this.path, // 巡航路径
  217. // 获取巡航路径中的路径坐标数组
  218. getPath: (pathData, pathIndex) => {
  219. return pathData.path
  220. }
  221. })
  222. // 创建巡航器
  223. var pathNavigator = pathSimplifierIns.createPathNavigator(0, {
  224. loop: false, // 是否循环
  225. speed: 2000 // 速度(km/h)
  226. })
  227. pathNavigator.start()
  228. })
  229. }
  230. })
  231. }
  232. }
  233. }
  234. </script>
  235. <style>
  236. .amap-wrapper {
  237. width: 100%;
  238. height: 600px;
  239. }
  240. .movie-list {
  241. padding-top: 15px;
  242. padding-left: 3%;
  243. padding-right: 3%;
  244. }
  245. </style>