vue使用高德地图根据城市名定位到地图上,并且点击出现消息弹窗体

vue使用高德地图根据城市名定位到地图上,并且点击出现消息弹窗体

效果图:
在这里插入图片描述
因为我们要根据城市名进行定位,所以要在引入的map插件后面加上
&plugin=AMap.DistrictSearch行政区搜索:

第一步:在index.html页面上引入下载的文件,这里的key需要你自己去高德地图上注册获取

 <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=**这里的key去高德注册**&plugin=AMap.DistrictSearch"></script> 

第二部:父组件中的代码

//引入map子组件
<Map :map-value="mapValue" v-if="mapValue.length>0"/>

<script>
import Map from '@/components/map_com'
export default {
	components: {
    Map,
 },
 data(){
  return{
    mapValue:[
      {
      	provinceName: "安徽省",
      	totalDeptNum:3,
      	workingDeptNum:1,
      	stopDeptNum:1,
      	finishDeptNum :2
      },
     {
      	provinceName: "北京",
      	totalDeptNum:3,
      	workingDeptNum:1,
      	stopDeptNum:1,
      	finishDeptNum :2
      },
    ]
  }
}

</script>


第三部:子组件代码

<template>
  <div id="app">
    <!-- <router-view /> -->
    <!-- <ExportWord></ExportWord> -->
    <div id="container" />
  </div>
</template>

<script>
// import AMap from 'vue-amap';
export default {
  name: 'App',
  components: {
    // FromCom

  },
  props: ['mapValue'],
  data() {
    return {
      map: null,
    }
  },
  mounted() {
    const _t = this
    this.map = new AMap.Map('container', {
      // 地图的缩放级别
      zoom: 5, // 级别
      center: [105, 35.319524], // 中心点坐标
      viewMode: '3D', // 使用3D视图
      mapStyle: 'amap://styles/6e156fccbae213b2cd402958ff4ff01b' // 设置地图的显示样式
    })

    // 地图操作工具条插件。可支持方向导航、位置定位、视野级别缩放、视野级别选择等操作。
    this.map.plugin(['AMap.ToolBar', 'AMap.Scale'], function() {
      // 加载工具条
      // var tool = new AMap.ToolBar({
      //   position:'RB'
      // });
      // _t.map.addControl(tool);
      // var scale = new AMap.Scale();
      // _t.map.addControl(scale);
    })

    this.mapAdd(this.mapValue)
  },
  methods: {
    // 添加标点
    mapAdd(markerList) {
      const _t = this

       var opts = {
            subdistrict: 0,   //获取边界不需要返回下级行政区
            extensions: 'all',  //返回行政区边界坐标组等具体信息
            // level: 'city'  //查询行政级别为 市
        };
        var district = null
        //加载行政区划插件
        if(!district){
            //实例化DistrictSearch
          district = new AMap.DistrictSearch(opts);
        }
        //行政区查询
        let cityCenter = ''
        console.log(markerList)
        markerList.forEach((item,index)=>{
          district.search(item.provinceName, function(status, result) {
            cityCenter = [result.districtList[0].center.lng,result.districtList[0].center.lat]
            var marker = new AMap.Marker({
            position: cityCenter,
            offset: new AMap.Pixel(-12,-12),
            zIndex: 101,
            map: _t.map,
            content:`<div class='info' >${item.totalDeptNum}</div>`,
            anchor: 'top-right',
            // 数据传递
            extData: item
          });
          _t.map.add(marker)
          
           // console.log(marker.getExtData())(marker,element)
          marker.on('click', _t.onMarkerClick)// 绑定click事件
          })
        })     
    },
    onMarkerClick(e) {
      const markerValue = e.target.getExtData()
      
      // console.log(e.originEvent.path)
      // console.log(JSON.parse(e.originEvent.path[0].dataset.mapvalue))
      const _t = this
      // 信息窗体的内容
      var content = `
            ${markerValue.provinceName}
            <hr>
            项目总数:<span class='zong'>${markerValue.totalDeptNum ?? 0}</span><br>
            在建项目总数:<span class='zaiZhi'>${markerValue.workingDeptNum ?? 0}</span><br>
            停工项目总数:<span class='zaiChang'>${markerValue.stopDeptNum ?? 0}</span><br>
            竣工项目总数:<span class='zaiChang'>${markerValue.finishDeptNum ?? 0}</span><br>
        `
        
      var infoWindow = new AMap.InfoWindow({ // 创建信息窗体
        content: content, // 信息窗体的内容可以是任意html片段
        // 信息弹窗相对位置
        offset: new AMap.Pixel(0, -45)
      })
      infoWindow.open(_t.map, e.target.getPosition())// 打开信息窗体
      // e.target就是被点击的Marker
    }
  }
}
</script>

<style lang="scss">
#container {width:100%; height: 100%; }
.amap-marker-label{
  background: transparent;
  border: none;
  color: white;
  font-size: 18px;
}
.info::before,.amap-marker-label::after,.amap-marker-label::before{
  content:' ';
  position: absolute;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: rgb(55, 148, 255);
  z-index: -1;
}
.amap-marker-label::before{
  width: 60px;
  height: 60px;
  background-color: rgba(55, 148, 255,0.5);
}
.info::before{
  width: 60px;
  height: 60px;
  background-color: rgba(55, 148, 255,0.5);
  animation: loading 2s ease-in-out infinite;
}

@keyframes loading{
	0%{
		/* transform: scaleY(0.1); */
    width: 10px;
    height: 10px;
		background: rgba(55, 148, 255,0.5);
	}50%{
		/* transform: scaleY(1); */
		background: rgba(135, 191, 255, 0.5);
	}100%{
		/* transform: scaleY(0.1); */
    width: 70px;
    height: 70px;
		background: transparent;/* 背景透明。实际上background默认的颜色就是透明的属性 */
	}
}

.amap-info-content.amap-info-outer{
  background-color: rgba(55, 148, 255, 0.836);
  color: white;
  padding: 10px 20px;

}
.bottom-center .amap-info-sharp{
  border-top: 8px solid rgba(55, 148, 255, 0.836);
}
.zong{
  color: chartreuse;
}
.zaiZhi{
  color: rgb(255, 230, 0);
}
.zaiChang{
  color: rgb(255, 115, 0);
}
</style>

注意一点:从父组件传值到子组件的时候要加上判断v-if:"{{mapValue.length>0}}",不然会出现数据没加载完成子组件就渲染好,导致数据传过去的是空

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值