html 引入高德地图
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.10&key=xxxxxxxxx"></script>
getCity.js 文件
// 定位城市
var position = new Promise(function (resolve, reject) {
// 加载地图,调用浏览器定位服务
/* eslint-disable */
AMap.plugin('AMap.CitySearch', function () {
var citySearch = new AMap.CitySearch()
citySearch.getLocalCity(function (status, result) {
// 查询成功,result即为当前所在城市信息
if (status === 'complete' && result.info === 'OK') {
resolve(result.city)
} else {
console.log('定位城市失败')
reject('定位失败')
}
})
})
})
const getCity = () => {
return position
}
export {getCity}