第一步:
在index.html中引入,放在body上面:

<script type='text/javascript' src='https://webapi.amap.com/maps?v=1.4.13&key=你自己的key&plugin=AMap.CitySearch'></script>

第二步在vue.config.js中:

configureWebpack: {
    externals: {
      'AMap': 'AMap'
    }
}

第三步使用的页面中引入并使用:
import AMap from 'AMap'

const citysearch = new AMap.CitySearch()
      citysearch.getLocalCity((status, result) => {
        if (status === 'complete' && result.info === 'OK') {
          if (result && result.city && result.bounds) {
            console.log(result)
          }
        } else {
          console.log(result.info)
        }
      })

这样就可以获取所在城市的定位了
image.png


静静不吃姜
20 声望0 粉丝