美文网首页vue
运用百度api实现定位城市

运用百度api实现定位城市

作者: 一只正在成长的程序猿 | 来源:发表于2019-07-25 19:05 被阅读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;
    })
  }

参考文档

相关文章

网友评论

    本文标题:运用百度api实现定位城市

    本文链接:https://www.haomeiwen.com/subject/gajkrctx.html