vue使用高德地图

1、注册高德地图账号

2、安装高德依赖包

安装依赖

npm i @amap/amap-jsapi-loader --save

vue文件引用依赖

import AMapLoader from '@amap/amap-jsapi-loader'

3、使用

初始化地图

initMap() {
      // 高德地图key的密钥
      window._AMapSecurityConfig = {
        securityJsCode: '密钥' // 密钥
      }
      AMapLoader.load({
        key: 'key', // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
        AMapUI: {
          // 是否加载 AMapUI,缺省不加载
          version: '1.1', // AMapUI 缺省 1.1
          plugins: [] // 需要加载的 AMapUI ui插件
        },
        Loca: {
          // 是否加载 Loca, 缺省不加载
          version: '2.0.0' // Loca 版本,缺省 1.3.2
        }
      }).then((AMap) => {
        // 标准normal
        // 幻影黑dark
        // 月光银light
        // 远山黛whitesmoke
        // 草色青fresh
        // 雅士灰grey
        // 涂鸦graffiti
        // 马卡龙macaron
        // 靛青蓝blue
        // 极夜蓝darkblue
        // 酱籽wine
        this.map = new AMap.Map('map', {
          resizeEnable: true,
          zoom: 15,
          mapStyle: 'amap://styles/darkblue',
          // center: [108.552500, 34.322700],//全国地址
          center: [121.396552, 31.376321]// 公司地址

        })
        //this.addMarker(AMap)
      }).catch(e => {
        console.log(e)
      })
    },

执行上面的地图就出来了,但是需要给地图添加标点

addMarker(AMap) {
      const that = this
      const mydata = [{
        name: '上海',
        devicesName: 'BL89545',
        devicesNumer: 'BL88X-8',
        devicesStatus: 1,
        value: [121.39585, 31.37661, 55] // 经纬+数据
      },
      {
        name: '北京',
        devicesName: 'BL89545',
        devicesNumer: 'BL88X-8',
        devicesStatus: 2,
        value: [116.40, 39.90, 110]
      },
      {
        name: '西藏',
        devicesName: 'BL89545',
        devicesNumer: 'BL88X-8',
        devicesStatus: 3,
        value: [85.08, 31.39, 110]
      },
      {
        name: '重庆',
        devicesName: 'BL89545',
        devicesNumer: 'BL88X-8',
        devicesStatus: 4,
        value: [106.55, 29.56, 32]
      },
      {
        name: '江西',
        devicesName: 'BL89545',
        devicesNumer: 'BL88X-8',
        devicesStatus: 5,
        value: [115.89, 28.67, 32]
      }
      ]

      mydata.forEach((item, index) => {
        let icon = ''
        let StatusDes = ''
        let StatusColor = ''
        switch (item.devicesStatus) {
          case 1:
            icon = require('@/assets/cockpit/state1.png')
            StatusDes = '正常'
            StatusColor = '#00D901'
            break
          case 2:
            icon = require('@/assets/cockpit/state2.png')
            StatusDes = '离线'
            StatusColor = '#818289'
            break
          case 3:
            icon = require('@/assets/cockpit/state3.png')
            StatusDes = '一般'
            StatusColor = '#EBEC34'
            break
          case 4:
            icon = require('@/assets/cockpit/state4.png')
            StatusDes = '紧急'
            StatusColor = '#F59B22'
            break
          default:
            icon = require('@/assets/cockpit/state5.png')
            StatusDes = '重要'
            StatusColor = '#D9001B'
        }

        // 配置marker
        const marker = new AMap.Marker({
          position: item.value,
          map: that.map,
          icon: new AMap.Icon({
            image: icon,
            imageSize: new AMap.Size(21, 28)
          }),
          // offset: new AMap.Pixel(-20, -62), //设置偏移量
          extData: {
            index
          }
        })

        const content =
						item.name + '<br/>' +
						'设备名称:' + item.devicesName + '<br/>' +
						'设备型号:' + item.devicesNumer + '<br/>' +
						'设备状态:' + '<span style=color:' + StatusColor + '>' + StatusDes + '</span>'

        console.log(content)

        marker.content = content
        marker.on('click', this.markerClick)
      })

      that.infoWindow = new AMap.InfoWindow({
        offset: new AMap.Pixel(16, -10)
      })

      that.map.on('click', function() {
        that.infoWindow.close()
      })
    },

以下是完整静态页面代码

<template>
	<div style="width: 100%;height: 800px;">
		<div id="map" class="map" />
	</div>
</template>
<script>
	// 引用依赖
	import AMapLoader from '@amap/amap-jsapi-loader'
	export default {
		name: 'Map',
		props: {
			tagData: {
				type: Array,
				default: () => []
			}
		},
		data() {
			return {
				map: null,
				infoWindow: ''
			}
		},
		created() {
			this.initMap()
		},
		methods: {
			initMap() {
				// 高德地图key的密钥
				window._AMapSecurityConfig = {
					securityJsCode: '2d2dc5571e67818b7a012b0d5023e233' // 密钥
				}
				AMapLoader.load({
					key: 'ba84633925403b361079e7e4d69517f4', // 申请好的Web端开发者Key,首次调用 load 时必填
					version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
					plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
					AMapUI: {
						// 是否加载 AMapUI,缺省不加载
						version: '1.1', // AMapUI 缺省 1.1
						plugins: [] // 需要加载的 AMapUI ui插件
					},
					Loca: {
						// 是否加载 Loca, 缺省不加载
						version: '2.0.0' // Loca 版本,缺省 1.3.2
					}
				}).then((AMap) => {
					this.map = new AMap.Map('map', {
						resizeEnable: true,
						zoom: 5,
						mapStyle: 'amap://styles/darkblue',
						center: [108.552500, 34.322700]
					})
					this.addMarker(AMap)
				}).catch(e => {
					console.log(e)
				})
			},
			addMarker(AMap) {
				const that = this
				const mydata = [{
						name: '上海',
						devicesName: 'BL89545',
						devicesNumer: 'BL88X-8',
						devicesStatus: 1,
						value: [121.39585, 31.37661, 55] // 经纬+数据
					},
					{
						name: '北京',
						devicesName: 'BL89545',
						devicesNumer: 'BL88X-8',
						devicesStatus: 2,
						value: [116.40, 39.90, 110]
					},
					{
						name: '西藏',
						devicesName: 'BL89545',
						devicesNumer: 'BL88X-8',
						devicesStatus: 3,
						value: [85.08, 31.39, 110]
					},
					{
						name: '重庆',
						devicesName: 'BL89545',
						devicesNumer: 'BL88X-8',
						devicesStatus: 4,
						value: [106.55, 29.56, 32]
					},
					{
						name: '江西',
						devicesName: 'BL89545',
						devicesNumer: 'BL88X-8',
						devicesStatus: 5,
						value: [115.89, 28.67, 32]
					}
				]

				mydata.forEach((item, index) => {
					let icon = ''
					let StatusDes = ''
					let StatusColor = ''
					switch (item.devicesStatus) {
						case 1:
							icon = require('@/assets/cockpit/state1.png')
							StatusDes = '正常'
							StatusColor = '#00D901'
							break
						case 2:
							icon = require('@/assets/cockpit/state2.png')
							StatusDes = '离线'
							StatusColor = '#818289'
							break
						case 3:
							icon = require('@/assets/cockpit/state3.png')
							StatusDes = '一般'
							StatusColor = '#EBEC34'
							break
						case 4:
							icon = require('@/assets/cockpit/state4.png')
							StatusDes = '紧急'
							StatusColor = '#F59B22'
							break
						default:
							icon = require('@/assets/cockpit/state5.png')
							StatusDes = '重要'
							StatusColor = '#D9001B'
					}

					// 配置marker
					const marker = new AMap.Marker({
						position: item.value,
						map: that.map,
						icon: new AMap.Icon({
							image: icon,
							imageSize: new AMap.Size(21, 28)
						}),
						// offset: new AMap.Pixel(-20, -62), //设置偏移量
						extData: {
							index
						}
					})

					const content =
						item.name + '<br/>' +
						'设备名称:' + item.devicesName + '<br/>' +
						'设备型号:' + item.devicesNumer + '<br/>' +
						'设备状态:' + '<span style=color:' + StatusColor + '>' + StatusDes + '</span>'
					marker.content = content
					marker.data = item

					marker.on('mouseover', function(e) {
						that.infoWindow.setContent(e.target.content)
						that.infoWindow.open(that.map, e.target.getPosition())
					})
					marker.on('mouseout', function(e) {
						that.infoWindow.close()
					})

					marker.on('click', this.markerClick)
				})

				that.infoWindow = new AMap.InfoWindow({
					offset: new AMap.Pixel(16, -10)
				})

				that.map.on('click', function() {
					that.infoWindow.close()
				})
			},

			markerClick(e) {
				console.log(e)
			}
		}
	}
</script>
<style>
	.map {
		width: 100%;
		/* height: calc(100vh - 200px) */
		height: 100%;
	}

	.map .amap-info-content {
		background: rgba(0, 0, 0, .6);
		border-radius: 8px;
		box-shadow: none;
		border: 1px solid rgba(147, 235, 248, .8);
		padding: 15px 25px 15px 15px;
		color: white;
	}

	body,
	h1,
	h2,
	h3,
	h4,
	h5,
	h6,
	hr,
	p,
	blockquote,
	dl,
	dt,
	dd,
	ul,
	ol,
	li,
	pre,
	form,
	fieldset,
	legend,
	button,
	input,
	textarea,
	th,
	td,
	img {
		border: medium none;
		margin: 0;
		padding: 0;
		font-size: 100%;
	}
</style>

封装成组件的代码

<template>
  <div style="width: 100%;height: 800px;">
    <div id="map" class="map" />
  </div>
</template>
<script>
// 引用依赖
import AMapLoader from '@amap/amap-jsapi-loader'
export default {
  name: 'Map',
  props: {
    markerList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      map: null,
      infoWindow: ''
    }
  },
  created() {
    console.log(this.markerList)
    this.initMap()
  },
  methods: {
    initMap() {
      // 高德地图key的密钥
      window._AMapSecurityConfig = {
        securityJsCode: '2d2dc5571e67818b7a012b0d5023e233' // 密钥
      }
      AMapLoader.load({
        key: 'ba84633925403b361079e7e4d69517f4', // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
        AMapUI: {
          // 是否加载 AMapUI,缺省不加载
          version: '1.1', // AMapUI 缺省 1.1
          plugins: [] // 需要加载的 AMapUI ui插件
        },
        Loca: {
          // 是否加载 Loca, 缺省不加载
          version: '2.0.0' // Loca 版本,缺省 1.3.2
        }
      }).then((AMap) => {
        // 标准normal
        // 幻影黑dark
        // 月光银light
        // 远山黛whitesmoke
        // 草色青fresh
        // 雅士灰grey
        // 涂鸦graffiti
        // 马卡龙macaron
        // 靛青蓝blue
        // 极夜蓝darkblue
        // 酱籽wine
        this.map = new AMap.Map('map', {
          resizeEnable: true,
          zoom: 15,
          mapStyle: 'amap://styles/darkblue',
          // center: [108.552500, 34.322700],//全国地址
          center: [121.396552, 31.376321]// 公司地址

        })
        this.addMarker(AMap)
      }).catch(e => {
        console.log(e)
      })
    },
    addMarker(AMap) {
      const that = this
      const mydata = this.markerList

      mydata.forEach((item, index) => {
        let icon = ''
        let StatusDes = ''
        let StatusColor = ''
        switch (item.devicesStatus) {
          case 1:
            icon = require('@/assets/cockpit/state1.png')
            StatusDes = '正常'
            StatusColor = '#00D901'
            break
          case 2:
            icon = require('@/assets/cockpit/state2.png')
            StatusDes = '离线'
            StatusColor = '#818289'
            break
          case 3:
            icon = require('@/assets/cockpit/state3.png')
            StatusDes = '一般'
            StatusColor = '#EBEC34'
            break
          case 4:
            icon = require('@/assets/cockpit/state4.png')
            StatusDes = '紧急'
            StatusColor = '#F59B22'
            break
          default:
            icon = require('@/assets/cockpit/state5.png')
            StatusDes = '重要'
            StatusColor = '#D9001B'
        }

        // 配置marker
        const marker = new AMap.Marker({
          position: item.value,
          map: that.map,
          icon: new AMap.Icon({
            image: icon,
            imageSize: new AMap.Size(21, 28)
          }),
          // offset: new AMap.Pixel(-20, -62), //设置偏移量
          extData: {
            index
          }
        })

        const content =
						item.name + '<br/>' +
						'设备名称:' + item.devicesName + '<br/>' +
						'设备型号:' + item.devicesNumer + '<br/>' +
						'设备状态:' + '<span style=color:' + StatusColor + '>' + StatusDes + '</span>'
        marker.content = content
        marker.data = item

        marker.on('mouseover', function(e) {
          that.infoWindow.setContent(e.target.content)
          that.infoWindow.open(that.map, e.target.getPosition())
        })
        marker.on('mouseout', function(e) {
          that.infoWindow.close()
        })

        marker.on('click', this.markerClick)
      })

      that.infoWindow = new AMap.InfoWindow({
        offset: new AMap.Pixel(16, -10)
      })

      that.map.on('click', function() {
        that.infoWindow.close()
      })
    },

    markerClick(e) {
      const data = e.target.data
      this.$router.push({
        path: '/configuration',
        query: {
          singleDevice: data.devicesNumer
        }
      })
    }

  }
}
</script>
<style>
	.map {
		width: 100%;
		/* height: calc(100vh - 200px) */
		height: 100%;
	}

	.map .amap-info-content {
		background: rgba(0, 0, 0, .6);
		border-radius: 8px;
		box-shadow: none;
		border: 1px solid rgba(147, 235, 248, .8);
		padding: 15px 25px 15px 15px;
		color: white;
		font-size: 14px;
	}

	body,
	h1,
	h2,
	h3,
	h4,
	h5,
	h6,
	hr,
	p,
	blockquote,
	dl,
	dt,
	dd,
	ul,
	ol,
	li,
	pre,
	form,
	fieldset,
	legend,
	button,
	input,
	textarea,
	th,
	td,
	img {
		border: medium none;
		margin: 0;
		padding: 0;
		font-size: 100%;
	}
</style>

父组件的使用

//引入组件
import Map from '@/components/Map'

components: {
    Map
  },



//使用组件
<Map :marker-list="markerList"></Map>


//给地图穿的参数
this.markerList = [{
      name: '上海',
      devicesName: 'BL89545',
      devicesNumer: 'BL88X-8',
      devicesStatus: 1,
      value: [121.39585, 31.37661, 55] // 经纬+数据
    },
    {
      name: '北京',
      devicesName: 'BL89545',
      devicesNumer: 'BL88X-8',
      devicesStatus: 2,
      value: [116.40, 39.90, 110]
    },
    {
      name: '西藏',
      devicesName: 'BL89545',
      devicesNumer: 'BL88X-8',
      devicesStatus: 3,
      value: [85.08, 31.39, 110]
    },
    {
      name: '重庆',
      devicesName: 'BL89545',
      devicesNumer: 'BL88X-8',
      devicesStatus: 4,
      value: [106.55, 29.56, 32]
    },
    {
      name: '江西',
      devicesName: 'BL89545',
      devicesNumer: 'BL88X-8',
      devicesStatus: 5,
      value: [115.89, 28.67, 32]
    }
    ]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用和引用的内容,出现错误"AttributeError: module 'distutils' has no attribute 'version'"的原因是setuptools版本问题。可以按照以下解决方案进行处理: 1. 打开对应的环境。 2. 输入以下代码卸载setuptools:pip uninstall setuptools。 3. 安装较旧版本的setuptools:pip install setuptools==59.5.0。 这样就可以解决美团yolov6报错"AttributeError: module 'distutils' has no attribute 'version'"的问题了。123 #### 引用[.reference_title] - *1* [YOLOV7:AttributeError: module ‘distutils‘ has no attribute ‘version‘ 的解决方案](https://blog.csdn.net/xty123abc/article/details/125862656)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *2* [AttributeError: module ‘distutils‘ has no attribute ‘version‘解决跑pytorch代码报错](https://blog.csdn.net/qq_42076902/article/details/129261266)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [AttributeError: module ‘distutils‘ has no attribute ‘version](https://blog.csdn.net/hahhahahhaja/article/details/128003170)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值