运用百度api实现定位城市

1.在index.html中写


2.在lib里面封装一个getLocation.js

let getCurrentCityName = function() {
    return new Promise(function(resolve, reject) {
        let myCity = new BMap.LocalCity()
        myCity.get(function(result) {
            resolve(result.name)
        })
    })
}

export default getCurrentCityName

3.在你需要定位的文件里面写

  mounted(){
    getCurrentCityName().then((city)=>{
      this.locations = city;
    })
  }

参考文档

你可能感兴趣的:(运用百度api实现定位城市)