/**
 * 使用百度地图api 获取当前用户的位置
 * 返回 {
 *   address : '地理位置中文',
 *   lng: '纬度',
 *   lat: '经度',
 * }
 * */
export default function getLocation(callBack = () => {}){
    if(typeof BMap == 'undefined'){
      console.log('请先加载百度api js 文件: http://api.map.baidu.com/api?v=2.0&ak=qWnwdKnruRGnojAtsZ7Cfb2g');
      return false;
    }
    const map = new BMap.Map('baiduMap');
    const point = new BMap.Point(107.303748,34.346725);
    map.centerAndZoom(point, 12);
    const geolocation = new BMap.Geolocation();
    geolocation.enableSDKLocation();
    geolocation.getCurrentPosition(function(r){
      if(this.getStatus() === 0) {
        let address = getAddress(r.point, callBack);
        let mk = new BMap.Marker(r.point);
        map.addOverlay(mk);
        map.panTo(r.point);
        return address;
      }else{
        console.log('发生错误!');

        return false;
      }
    },{enableHighAccuracy: true});

}

export function getAddress(point, callBack=()=>{}){
  const lng = point.lng; // 经度
  const lat = point.lat; // 纬度
  let gc = new BMap.Geocoder();
  gc.getLocation(point,function(res){
    let  formValues = {};
    // 更改状态
    formValues.address = res.address;
    formValues.lng = lng;
    formValues.lat = lat;
    callBack(formValues);
  });
}
