vue-获取当前位置

时间:2024-11-22 09:03:49
  • <script>
  • import {MP} from '../../../public/';
  • export default {
  • data() {
  • return {
  • ak:'********************',//这里是密钥,秘钥去百度地图申请
  • city: '成都市'
  • }
  • },
  • created() {
  • this.getCity()
  • },
  • methods:{
  • getCity() {
  • this.$nextTick(()=>{
  • MP(this.ak).then(BMap=> {
  • //在此调用api
  • var geolocation = new BMap.Geolocation();
  • geolocation.getCurrentPosition(a=>{
  • this.city = a.address.city;
  • })
  • })
  • })
  • }
  • }
  • }
  • </script>