使用腾讯地图直接获取经纬度

1.注册成为开发者申请api key

注册成功后填写密钥信息打开控制台获取密钥

腾讯位置服务 - 立足生态,连接未来

2.使用axios去获取经纬度

在main.js 中添加一下代码

import axios from 'axios'
axios.defaults.baseURL='#'
Vue.prototype.$http = axios

 再需要获取经纬度的页面

 <script>
 export default {
    name: 'Home',
    components: {
      HelloWorld
    },
    created() { 
      this.getlocation()
    },
    methods: {
    //这边使用了async,await去简化Promise并解构获取到的数据
    //其中axios中的 https://apis.map.qq.com/ws/location/v1/ip 是在下图中获取这里的key就是之前申请的
      async getlocation() {
        const {
          data: res
        } = await this.$http.get('https://apis.map.qq.com/ws/location/v1/ip', {
          params: {
            key: '你的密钥'
          }
        })
        console.log(res);
      }
    }
  }
</script>

虽然这个方法简单而且不用通过位置获取,但是经纬度有偏差。

猜你喜欢

转载自blog.csdn.net/weixin_55953988/article/details/124735470