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>
虽然这个方法简单而且不用通过位置获取,但是经纬度有偏差。