Vue通过经纬度显示点位地图

该文介绍了如何在Vue应用中集成高德地图,包括通过npm安装资源,配置地图参数,如版本、API密钥和插件,以及在Vue组件中显示地图、点击事件处理和地图信息的显示。此外,还展示了如何根据经纬度信息展示地图并自定义地图样式。

效果图

1.首先运行npm install --save @amap/amap-vue
下载资源 在main.js里添加配置

// 高德地图
import AmapVue from '@amap/amap-vue';

AmapVue.config.version = '2.0'; // 默认2.0,这里可以不修改
AmapVue.config.key = 'e1b6e4516f3e515c017ea2e7356f7eab';
AmapVue.config.plugins = [
  'AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType',
  'AMap.PolyEditor', 'AMap.CircleEditor', 'AMap.DistrictSearch', 'AMap.CircleMarker', 'AMap.Polyline'
];
Vue.use(AmapVue);
window._AMapSecurityConfig = {
  securityJsCode:'11a114159073d6bd17eddddbadce94d9',
};

2.在vue页面添加代码

<template>
	<div>
        <table>
        	<af-table-column label="所在地址" align="center" prop="szdz" >
				<template slot-scope="scope">
			      <el-link type="primary" 		      @click="clickMapInfo(scope.row.jd,scope.row.wd)" :underline="false">
			       <span>{{scope.row.szdz}}</span>
			      </el-link>
			    </template>
			</af-table-column>  
    	</table>
        
        <el-dialog title="查看工地地址" :visible.sync="showMapInfo" width="65%" append-to-body>
			<look-map-info :timeStamp="Date.parse(new Date())" :mapData="mapInfo" />
		</el-dialog>
    </div>
</template>

<script>
	import lookMapInfo from "@/components/AMap/lookMapInfo.vue";
    
    export default {
        components: {
			lookMapInfo
		},
        data() {
            return {
                // 经纬度信息
				mapInfo: "",
				// 是否查看信息地图
				showMapInfo: false,
            }
        }
        methods: {
            // 点击查看地图位置
			clickMapInfo(jd,wd) {
				if (!jd) {
					this.$message.error("请维护地址信息");
					return;
				}
				this.mapInfo = jd + "," + wd;
				this.showMapInfo = true;
			},
        }
    }
</script>

3.添加地图页面

<template>
  <div class="coord-picker">
    <div class="map-container">
      <amap
        cache-key="coord-picker-map"
        mmap-style="amap://styles/whitesmoke"
        async
        :center.sync="center"
        :zoom.sync="zoom"
        is-hotspot
      >
        <amap-marker v-if="position" :position.sync="position" draggable />
      </amap>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    timeStamp: {
      type: Number,
    },
    mapData: {
      type: String,
    },
  },
  data() {
    return {
      center: null,
      zoom: 10,
      position: null,
    };
  },
  watch: {
    timeStamp() {
      this.center = [115.479646, 35.234309];
      this.zoom = 10;
      this.position = null;
	  this.initMap();
    },
    mapData() {
      this.initMap();
    },
  },
  created() {
    this.initMap();
  },
  methods: {
    // 搜索输入地区信息
    async initMap() {
      if (!this.mapData) {
        return;
      }
      let mapPoi = this.mapData.split(",");
      if (mapPoi.length < 2) {
        this.center = [115.479646, 35.234309];
        this.zoom = 10;
        return;
      }
      this.center = mapPoi;
      this.zoom = 14;
      this.position = mapPoi;
    },
  },
};
</script>

<style lang="scss" scoped>
.map-container {
  width: 100%;
  height: 500px;
}
</style>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半度纳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值