小言_互联网的博客

RuoYi vue-element-admin 增加原生高德地图并显示海量点

1257人阅读  评论(0)

1、在public/index.html中增加js引用

记得放到head中


  
  1. <!-- 引入高德地图API -->
  2. <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.15&key=1edb3d323fb05a961b3321111111ddff145"></script>

2、vue.config.js 增加配置内容


  
  1. externals: {
  2. 'AMap': 'AMap',
  3. 'AMapUI': 'AMapUI'
  4. }

3、要引用地图的页面

要显示地图的div


  
  1. <!-- 显示地图的div -->
  2. <div id="mapDiv" ref="mapDiv" class="mapDiv">
  3. loading...
  4. </div>

 引用AMap

import AMap from 'AMap'

初始化地图 


  
  1. // 初始化地图
  2. initMap() {
  3. const map = new AMap.Map( 'mapDiv', {
  4. center: [ 120.56587, 36.20366],
  5. resizeEnable: true,
  6. zoom: 12
  7. // lang: 'en'
  8. })
  9. this.map = map
  10. // 地图添加各种控件
  11. AMap.plugin([ 'AMap.ToolBar', 'AMap.Scale', 'AMap.MapType', 'AMap.Geolocation'], function() {
  12. // 在图面添加工具条控件,工具条控件集成了缩放、平移、定位等功能按钮在内的组合控件
  13. map.addControl( new AMap.ToolBar())
  14. // 在图面添加类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
  15. map.addControl( new AMap.MapType())
  16. // 在图面添加定位控件,用来获取和展示用户主机所在的经纬度位置
  17. // map.addControl(new AMap.Geolocation())
  18. // 在图面添加比例尺控件,展示地图在当前层级和纬度下的比例尺
  19. map.addControl( new AMap.Scale())
  20. })
  21. // 海量点的样式
  22. var style = [{
  23. // 必填参数,图标的地址
  24. url: 'https://a.amap.com/jsapi_demos/static/images/mass0.png',
  25. // 必填参数,图标显示位置偏移量,以图标的左上角为基准点(0,0)点,例如:anchor:new AMap.Pixel(5,5)
  26. anchor: new AMap.Pixel( 6, 6),
  27. // 必填参数,图标的尺寸;例如:size:new AMap.Size(11,11)
  28. size: new AMap.Size( 11, 11)
  29. },
  30. // 红色 未除治
  31. {
  32. url: this.baseURL + '/storage/dfs/display?path=group1/M00/00/0E/wKgABV-2IPiAEO2hAAAEJdf9ZKs794.png',
  33. anchor: new AMap.Pixel( 6, 6),
  34. size: new AMap.Size( 11, 11)
  35. },
  36. // 绿色 已经除治
  37. {
  38. url: this.baseURL + '/storage/dfs/display?path=group1/M00/00/0E/wKgABV-2IriAQ7-IAAAEJWeQRis039.png',
  39. anchor: new AMap.Pixel( 6, 6),
  40. size: new AMap.Size( 11, 11)
  41. },
  42. // 灰色以前的数据
  43. {
  44. url: this.baseURL + '/storage/dfs/display?path=group1/M00/00/0E/wKgABV-2IyGAS1xQAAAEJeELcZs238.png',
  45. anchor: new AMap.Pixel( 6, 6),
  46. size: new AMap.Size( 11, 11)
  47. }]
  48. this.mass = new AMap.MassMarks( null, {
  49. // 图层的透明度,取值范围[0,1],1代表完全不透明,0代表完全透明
  50. opacity: 0.6,
  51. // 图层叠加的顺序值,0表示最底层。默认zIndex:5
  52. zIndex: 111,
  53. // 指定鼠标悬停时的鼠标样式,自定义cursor,IE仅支持cur/ani/ico格式,Opera不支持自定义cursor
  54. cursor: 'pointer',
  55. style: style,
  56. // 表示是否在拖拽缩放过程中实时重绘,默认true,建议超过10000的时候设置false
  57. alwaysRender: false
  58. })
  59. // 鼠标悬浮 弹出窗口
  60. var marker = new AMap.Marker({ content: ' ', map: map })
  61. this.mass.on( 'mouseover', function(e) {
  62. marker.setPosition(e.data.lnglat)
  63. marker.setLabel({ content: e.data.name })
  64. })
  65. // 海量点绑定到地图上
  66. this.mass.setMap(map)
  67. },

附:整个vue代码 


  
  1. <template>
  2. <div class="app-container">
  3. <!-- 搜索工具栏 -->
  4. <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
  5. <el-form-item label="" prop="deptId">
  6. <!-- <treeselect v-model="form.deptId" :options="deptOptions" :disable-branch-nodes="true" :show-count="true" placeholder="请选择归属部门" /> -->
  7. <treeselect v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="归属区域" clearable size="small" style="width: 180px" />
  8. </el-form-item>
  9. <el-form-item label="" prop="compart">
  10. <el-input
  11. v-model= "queryParams.compart"
  12. placeholder= "请输入小班编号"
  13. clearable
  14. size= "small"
  15. @ keyup.enter.native= "handleQuery"
  16. />
  17. </el-form-item>
  18. <el-form-item label="" prop="markSn">
  19. <el-input
  20. v-model= "queryParams.markSn"
  21. placeholder= "请输入疫木编号"
  22. clearable
  23. size= "small"
  24. @ keyup.enter.native= "handleQuery"
  25. />
  26. </el-form-item>
  27. <!-- <el-form-item label="" prop="deptId">
  28. <el-input
  29. v-model="queryParams.deptId"
  30. placeholder="请输入部门ID"
  31. clearable
  32. size="small"
  33. @keyup.enter.native="handleQuery"
  34. />
  35. </el-form-item> -->
  36. <el-form-item label="" prop="treeStatus">
  37. <el-select v-model="queryParams.treeStatus" placeholder="疫木除治状态" clearable size="small">
  38. <el-option
  39. v-for= "dict in treeStatusOptions"
  40. :key= "dict.dictValue"
  41. :label= "dict.dictLabel"
  42. :value= "dict.dictValue"
  43. />
  44. </el-select>
  45. </el-form-item>
  46. <el-form-item label="" prop="markOperate">
  47. <el-input
  48. v-model= "queryParams.markOperate"
  49. placeholder= "请输入定标操作人"
  50. clearable
  51. size= "small"
  52. @ keyup.enter.native= "handleQuery"
  53. />
  54. </el-form-item>
  55. <el-form-item label="" prop="markTime">
  56. <el-date-picker
  57. v-model= "queryParams.beginTime"
  58. clearable
  59. size= "small"
  60. style= "width: 200px"
  61. type= "date"
  62. value-format= "yyyy-MM-dd"
  63. placeholder= "开始时间"
  64. />
  65. </el-form-item>
  66. <el-form-item label="" prop="markTime">
  67. <el-date-picker
  68. v-model= "queryParams.endTime"
  69. clearable
  70. size= "small"
  71. style= "width: 200px"
  72. type= "date"
  73. value-format= "yyyy-MM-dd"
  74. placeholder= "结束时间"
  75. />
  76. </el-form-item>
  77. <el-form-item>
  78. <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索 </el-button>
  79. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置 </el-button>
  80. </el-form-item>
  81. </el-form>
  82. <!-- 显示地图的div -->
  83. <div id="mapDiv" ref="mapDiv" class="mapDiv">
  84. loading...
  85. </div>
  86. </div>
  87. </template>
  88. <script>
  89. import AMap from 'AMap'
  90. import { listMarkAll } from '@/api/tree/mark'
  91. import { treeselect } from '@/api/system/dept'
  92. import Treeselect from '@riophae/vue-treeselect'
  93. import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  94. // import tdtmap from '@/utils/tdt-map'
  95. export default {
  96. name: 'Mark',
  97. components: { Treeselect },
  98. data() {
  99. return {
  100. baseURL: process.env.VUE_APP_BASE_API,
  101. // 遮罩层
  102. loading: true,
  103. // 选中数组
  104. ids: [],
  105. // 非单个禁用
  106. single: true,
  107. // 非多个禁用
  108. multiple: true,
  109. // 显示搜索条件
  110. showSearch: true,
  111. // 总条数
  112. total: 0,
  113. // 定标信息表格数据
  114. markList: [],
  115. // 弹出层标题
  116. title: '',
  117. // 是否显示弹出层
  118. open: false,
  119. // 部门树选项
  120. deptOptions: undefined,
  121. // 疫木状态字典
  122. treeStatusOptions: [],
  123. // 定标标志字典
  124. markFlagOptions: [],
  125. // 除治标志字典
  126. sealedFlagOptions: [],
  127. // 定标类型字典
  128. markTypeOptions: [],
  129. // 查询参数
  130. queryParams: {
  131. pageNum: 1,
  132. pageSize: 10,
  133. markSn: null,
  134. deptId: null,
  135. treeStatus: null,
  136. compart: null,
  137. diam: null,
  138. markPic1: null,
  139. markPic2: null,
  140. markPic3: null,
  141. markPic4: null,
  142. markOperate: null,
  143. markTime: null,
  144. markUploadTime: null,
  145. markFlag: null,
  146. markRemark: null,
  147. operate: null
  148. },
  149. // 表单参数
  150. form: {},
  151. // 地图对象
  152. map: '',
  153. mass: '',
  154. infowindowL: null // 信息窗体
  155. }
  156. },
  157. created() {
  158. this.getList()
  159. this.getTreeselect()
  160. this.getDicts( 'tree_status').then( response => {
  161. this.treeStatusOptions = response.data
  162. })
  163. this.getDicts( 'work_flag').then( response => {
  164. this.markFlagOptions = response.data
  165. this.sealedFlagOptions = response.data
  166. })
  167. this.getDicts( 'mark_type').then( response => {
  168. this.markTypeOptions = response.data
  169. })
  170. },
  171. mounted() {
  172. this.initMap()
  173. },
  174. methods: {
  175. /** 查询疫木位置列表 */
  176. getList() {
  177. this.loading = true
  178. listMarkAll( this.queryParams).then( response => {
  179. // 查询到的列表
  180. this.markList = response.rows
  181. // 查询到的总数
  182. this.total = response.total
  183. this.loading = false
  184. // 处理接收到的数据
  185. this.markPoint( this.markList)
  186. })
  187. },
  188. // 疫木状态字典翻译
  189. treeStatusFormat(row, column) {
  190. return this.selectDictLabel( this.treeStatusOptions, row.treeStatus)
  191. },
  192. // 定标类型字典翻译
  193. markTypeFormat(row, column) {
  194. return this.selectDictLabel( this.markTypeOptions, row.markSelf)
  195. },
  196. /** 查询部门下拉树结构 */
  197. getTreeselect() {
  198. treeselect().then( response => {
  199. this.deptOptions = response.data
  200. })
  201. },
  202. // 定标标志字典翻译
  203. markFlagFormat(row, column) {
  204. return this.selectDictLabel( this.markFlagOptions, row.markFlag)
  205. },
  206. // 除治标志字典翻译
  207. sealedFlagFormat(row, column) {
  208. return this.selectDictLabel( this.sealedFlagOptions, row.sealedFlag)
  209. },
  210. // 取消按钮
  211. cancel() {
  212. this.open = false
  213. this.reset()
  214. },
  215. /** 搜索按钮操作 */
  216. handleQuery() {
  217. this.queryParams.pageNum = 1
  218. this.getList()
  219. },
  220. /** 重置按钮操作 */
  221. resetQuery() {
  222. this.resetForm( 'queryForm')
  223. this.handleQuery()
  224. },
  225. // 初始化地图
  226. initMap() {
  227. const map = new AMap.Map( 'mapDiv', {
  228. center: [ 120.56587, 36.20366],
  229. resizeEnable: true,
  230. zoom: 12
  231. // lang: 'en'
  232. })
  233. this.map = map
  234. // 地图添加各种控件
  235. AMap.plugin([ 'AMap.ToolBar', 'AMap.Scale', 'AMap.MapType', 'AMap.Geolocation'], function() {
  236. // 在图面添加工具条控件,工具条控件集成了缩放、平移、定位等功能按钮在内的组合控件
  237. map.addControl( new AMap.ToolBar())
  238. // 在图面添加类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
  239. map.addControl( new AMap.MapType())
  240. // 在图面添加定位控件,用来获取和展示用户主机所在的经纬度位置
  241. // map.addControl(new AMap.Geolocation())
  242. // 在图面添加比例尺控件,展示地图在当前层级和纬度下的比例尺
  243. map.addControl( new AMap.Scale())
  244. })
  245. // 海量点的样式
  246. var style = [{
  247. // 必填参数,图标的地址
  248. url: 'https://a.amap.com/jsapi_demos/static/images/mass0.png',
  249. // 必填参数,图标显示位置偏移量,以图标的左上角为基准点(0,0)点,例如:anchor:new AMap.Pixel(5,5)
  250. anchor: new AMap.Pixel( 6, 6),
  251. // 必填参数,图标的尺寸;例如:size:new AMap.Size(11,11)
  252. size: new AMap.Size( 11, 11)
  253. },
  254. // 红色 未除治
  255. {
  256. url: this.baseURL + '/storage/dfs/display?path=group1/M00/00/0E/wKgABV-2IPiAEO2hAAAEJdf9ZKs794.png',
  257. anchor: new AMap.Pixel( 6, 6),
  258. size: new AMap.Size( 11, 11)
  259. },
  260. // 绿色 已经除治
  261. {
  262. url: this.baseURL + '/storage/dfs/display?path=group1/M00/00/0E/wKgABV-2IriAQ7-IAAAEJWeQRis039.png',
  263. anchor: new AMap.Pixel( 6, 6),
  264. size: new AMap.Size( 11, 11)
  265. },
  266. // 灰色以前的数据
  267. {
  268. url: this.baseURL + '/storage/dfs/display?path=group1/M00/00/0E/wKgABV-2IyGAS1xQAAAEJeELcZs238.png',
  269. anchor: new AMap.Pixel( 6, 6),
  270. size: new AMap.Size( 11, 11)
  271. }]
  272. this.mass = new AMap.MassMarks( null, {
  273. // 图层的透明度,取值范围[0,1],1代表完全不透明,0代表完全透明
  274. opacity: 0.6,
  275. // 图层叠加的顺序值,0表示最底层。默认zIndex:5
  276. zIndex: 111,
  277. // 指定鼠标悬停时的鼠标样式,自定义cursor,IE仅支持cur/ani/ico格式,Opera不支持自定义cursor
  278. cursor: 'pointer',
  279. style: style,
  280. // 表示是否在拖拽缩放过程中实时重绘,默认true,建议超过10000的时候设置false
  281. alwaysRender: false
  282. })
  283. // 鼠标悬浮 弹出窗口
  284. var marker = new AMap.Marker({ content: ' ', map: map })
  285. this.mass.on( 'mouseover', function(e) {
  286. marker.setPosition(e.data.lnglat)
  287. marker.setLabel({ content: e.data.name })
  288. })
  289. // 海量点绑定到地图上
  290. this.mass.setMap(map)
  291. },
  292. markPoint(pointData) {
  293. // this.initMap()
  294. var len = pointData.length
  295. console.log( '收到数据,开始处理数据,树木信息数量 :' + len)
  296. var lnglat1 = []
  297. // {"lnglat":[116.258446,37.686622],"name":"景县","style":2}
  298. for ( var i = 0; i < len; i++) {
  299. var tree1 = { 'lnglat': [pointData[i].lng, pointData[i].lat], 'name': pointData[i].markSn, 'style': pointData[i].treeStatus }
  300. // 坐标转换
  301. // AMap.convertFrom(tree1.lnglat, 'gps', function(status, result) {
  302. // if (result.info === 'ok') {
  303. // var resLnglat = result.locations[0]
  304. // tree1.lnglat = resLnglat
  305. // }
  306. // })
  307. lnglat1.push(tree1)
  308. }
  309. if ( document.createElement( 'canvas').getContext) { // 判断当前浏览器是否支持绘制海量点
  310. console.log( '数据处理完毕,进入绘制')
  311. // 清理位置信息
  312. this.mass.clear()
  313. // 绑定新的位置信息
  314. this.mass.setData(lnglat1)
  315. console.log( '数据绘制完毕')
  316. } else {
  317. alert( '此示例目前只有在IE9及以上浏览器打开')
  318. }
  319. }
  320. }
  321. }
  322. </script>
  323. <style scoped>
  324. .mapDiv{
  325. width: 100%;
  326. height: 80vh;
  327. }
  328. </style>

最终显示效果

 

 


转载:https://blog.csdn.net/u010299411/article/details/109831592
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场