vue --- 项目中是用高德地图API

1 篇文章 0 订阅

4

4.1.首先在高德地图开放平台注册成为开发者,申请自己的key值

https://lbs.amap.com/api/javascript-api/guide/abc/prepare

2.在index.html页面内引入高德地图

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.13&key=自己的key值"></script>

3.新建地图组件(根据项目需求),给地图一个容器

<template>
  <div class="mapInit">
    <div id="container"></div>
  </div>
</template>

 4.把地图赋予一个变量,确保每次使用的是同一个地图,然后根据需求添加功能,下面包括了自定义多个marker,marker名,marker事件,信息弹窗等

<script>
import axios from 'axios';
import store from '../store/index.js'
import {mapState} from 'vuex';
export default {
  name: 'mapInit',
  data () {
    return {
      Map:'',
    }
  },
  filters:{},
  watch:{},
  computed:{
    ...mapState({
      active: state => state.vux.active
    }),
    // getActive(){
    //   return  store.state.active
    // }
  },
  methods:{
    // 创建高德地图
    createdMap(){
      this.Map = new AMap.Map('container', {
          mapStyle:'amap://styles/fresh',
          viewMode:'3D',//使用3D视图
          pitch:30, //视野倾斜角度
      })
      //设置地图缩放级别和中心点经纬度
      this.Map.setZoomAndCenter(18, [121.559868, 31.18244]);
    },
    // 创建地图撒点,点击标注的点触发事件
    mapSprinkle(){
      let _this = this;
      // 创建 AMap.Icon 实例:
      var icon = new AMap.Icon({
          size: new AMap.Size(50, 50),    // 图标尺寸
          image: require('../assets/images/pic.png'),  // Icon的图像
          imageOffset: new AMap.Pixel(10, 10),  // 图像相对展示区域的偏移量,适于雪碧图等
          // imageSize: new AMap.Size(40, 50),   // 根据所设置的大小拉伸或压缩图片
          name: '上海'
      });
      let datad = [{address:[121.559868, 31.18244],style:'烟感',status:1},{address:[121.559811, 31.18224],style:'安防',status:0}];
      datad.map(function(item,index){
        // 创建一个 Marker 实例:
        var marker = new AMap.Marker({
          position: item.address,   // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
          icon: icon,
          // offset: new AMap.Pixel(-10, -10),
          anchor:"center",
        });
        // 将创建的点标记添加到已有的地图实例:
        _this.Map.add(marker);  //可以是一个对象,代表创建多个marker

        // marker.setMap(_this.Map);
        // marker.setTitle("设置位置");
        marker.setLabel({
		        // offset: new AMap.Pixel(15, 15),
		        content: "定位名称"
		    });

        // 点击标注的点触发事件
        // var clickHandler = function(e) { alert('您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图!') }
        // marker.on('click', clickHandler);
        AMap.event.addListener(marker,'click',function(e){
          let targetNode = e.target.He.icon.He
          console.log(targetNode.image,targetNode.name);
          // console.log(e.target.getPosition())   // 获取点击marker的经纬度
          _this.infor(e,item.style,item.status)
        })
      })
    },
    //在点击marker的经纬度打开信息窗体
    infor(e,style,status){
      if(status==0){
        style = '<p style="color:blue;">'+style+'</p>'
      }else{
        style = '<p style="color:red;">'+style+'</p>'
      }
      let infoWindow = new AMap.InfoWindow({
        offset: new AMap.Pixel(-15, -28),
        content:'<div style="width:210px; height:60px; line-height:20px; overflow:auto;">' +style+ '</div>',
        // content: '这是信息窗体!这是信息窗体!'
      });
      infoWindow.open(this.Map, e.target.getPosition());
    }
  },
  components:{},
  created (){},
  mounted(){
    this.createdMap()
    this.mapSprinkle()
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.mapInit{
    width: 100%;
    height: 100%;
  #container {
    width: 100%;
    height: 100%;
  }
}
</style>

5.把组件添加到页面内,引入组建 import mapInit from '../components/mapInit',注册组件 components:{ mapInit },使用组件 <mapInit></mapInit>

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中使用高德SDK可以通过vue-amap插件来实现。以下是一些基本步骤: 1. 首先,通过npm或yarn安装vue-amap插件。可以使用以下命令进行安装: ```bash npm install vue-amap ``` 2. 在main.js文件中引入vue-amap插件: ```javascript import VueAMap from 'vue-amap'; Vue.use(VueAMap); ``` 3. 在Vue组件中使用vue-amap插件。可以在组件的mounted钩子函数中进行初始化和配置。 ```javascript mounted() { // 初始化vue-amap this.initAMapAPI(); }, methods: { initAMapAPI() { // 配置vue-amap VueAMap.initAMapApiLoader({ key: 'your-amap-api-key', plugin: ['AMap.Geolocation'] }); // 加载插件模块 VueAMap .then(() => { // 创建地图对象 const map = new VueAMap.Map('map-container'); // 添加地图控件等操作 // ... // 获取当前位置 this.getCurrentLocation(map); }) .catch(error => { console.log('地图加载失败:', error); }); }, getCurrentLocation(map) { // 使用高德SDK中的定位功能获取当前位置 AMap.plugin('AMap.Geolocation', () => { const geolocation = new VueAMap.Geolocation(); map.addControl(geolocation); geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 获取到当前位置信息 console.log('当前位置:', result); } else { // 获取失败 console.log('获取位置失败'); } }); }); } } ``` 通过以上步骤,你就可以在Vue中使用高德SDK了。需要注意的是,在使用过程中要替换'your-amap-api-key'为你自己的高德API密钥。并且根据需求,你还可以添加更多高德SDK的功能和组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值