import Taro from "@tarojs/taro";

// 腾讯地图接口 - 个人开发者
const QMAP_WEBSERVICE_KEY = 'HTPBZ-HHJA7-XD2XD-PRS37-H3HVJ-U5BAA';

// https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
// location 为 gcj02 编码
export function geocoder(location, getPoi = 0) {
  if (!location || location.length < 3) {
    return Promise.reject(new Error('待解析地址为空'));
  }

  return new Promise((resolve, reject) => {
    const url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${location}&key=${QMAP_WEBSERVICE_KEY}&get_poi=${getPoi}`

    Taro.request({ url }).then(({ data }) => {
      const { status, message, result } = data || {};
      if (status != 0) {
        console.error(data);
        reject(new Error(message));
      } else {
        resolve(result);
      }
    }).catch((err) => {
      console.error(err);
      reject(new Error(err.errMsg));
    });
  })
}
