vue

运用百度api实现定位城市

2019-07-25  本文已影响27人  一只正在成长的程序猿

1.在index.html中写

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>

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;
    })
  }

参考文档

上一篇 下一篇

猜你喜欢

热点阅读