import AMapLoader from "@amap/amap-jsapi-loader";
import {useUserStore} from "@/store/modules/user";



export const getLocation = () => {
  const user = useUserStore()


  AMapLoader.load({
    key: 'cd9769b4feec118fe176763bd10168a0',
    version: "2.0",
    AMapUI: {
      version: "1.1",
    },
    Loca: {
      version: "2.0.0"
    },
  }).then((AMap) => {
    const map = new AMap.Map("container", {
      zoom: 15,
      features: ['bg', 'point', 'road', 'building'],
      viewMode: '2D',  //设置地图模式
      // center: [116.47394,39.904211],
      resizeEnable: true
    })

    AMap.plugin(['AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Geolocation', 'AMap.CitySearch'], () => {
      const geolocation = new AMap.Geolocation({
        enableHighAccuracy: true, // 是否使用高精度定位，默认：true
        timeout: 10000,          // 设置定位超时时间，默认：无穷大
        showButton: true,        //显示定位按钮，默认：true
        needAddress: true,       //是否需要将定位结果进行逆地理编码操作
        convert: true,       //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
        position: 'RB',
        offset: [10, 20],
      })
      map.addControl(geolocation)
      geolocation.getCurrentPosition()
      // 获取用户当前的精确位置信息
      geolocation.getCurrentPosition((status, result) => {
        // myCity.value = result.addressComponent.province
        user.setLocationMap(result)
      })
    })
  })
}