//防抖debounce代码：
export function debounce(fn,delay=500) {
  var timeout = null; // 创建一个标记用来存放定时器的返回值
  return function (e) {
      // 每当用户输入的时候把前一个 setTimeout clear 掉
      clearTimeout(timeout); 
      // 然后又创建一个新的 setTimeout, 这样就能保证interval 间隔内如果时间持续触发，就不会执行 fn 函数
      timeout = setTimeout(() => {
          fn.apply(this, arguments);
      }, delay);
  };
}

// 返回两个经纬度之间的距离 单位米
const earthDistance = function (location1, location2) {
	    const lat1 = parseFloat(location1.lat);
	    const lng1 = parseFloat(location1.lng);
	    const lat2 = parseFloat(location2.lat);
	    const lng2 = parseFloat(location2.lng);
	
	    const EARTH_RADIUS = 6378137.0; //单位M
	    const PI = Math.PI;
	
	    function getRad(d) {
	        return d * PI / 180.0;
	    }
	
	    let f = getRad((lat1 + lat2) / 2);
	    let g = getRad((lat1 - lat2) / 2);
	    let l = getRad((lng1 - lng2) / 2);
	
	    let sg = Math.sin(g);
	    let sl = Math.sin(l);
	    let sf = Math.sin(f);
	
	    let s, c, w, r, d, h1, h2;
	    let a = EARTH_RADIUS;
	    let fl = 1 / 298.257;
	
	    sg = sg * sg;
	    sl = sl * sl;
	    sf = sf * sf;
	
	    s = sg * (1 - sl) + (1 - sf) * sl;
	    c = (1 - sg) * (1 - sl) + sf * sl;
	
	    w = Math.atan(Math.sqrt(s / c));
	    r = Math.sqrt(s * c) / w;
	    d = 2 * w * a;
	    h1 = (3 * r - 1) / 2 / c;
	    h2 = (3 * r + 1) / 2 / s;
	
	    return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
	};


//rpx转换px  px=rpx * (屏幕宽度 / 750)
export const rpxTopx = function (rpx) {
	let width = uni.getWindowInfo().screenWidth
	let px = rpx *( width / 750 );
	return px
}

/**
 * 腾讯经纬度转百度地图经纬度
 * @param {Object} lng
 * @param {Object} lat
 */
export function qqMapTransBMap(lng, lat) {
	let x_pi = (3.14159265358979324 * 3000.0) / 180.0
	let x = lng
	let y = lat
	let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi)
	let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi)
	let lngs = z * Math.cos(theta) + 0.0065
	let lats = z * Math.sin(theta) + 0.006
	return {
		lng: lngs,
		lat: lats,
	}
}
/**
 * 百度地图经纬度转腾讯地图经纬度
 * @param {Object} lng
 * @param {Object} lat
 */
export function bMapTransQQMap(lng, lat) {
	let x_pi = (3.14159265358979324 * 3000.0) / 180.0
	let x = lng - 0.0065
	let y = lat - 0.006
	let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
	let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
	let lngs = z * Math.cos(theta)
	let lats = z * Math.sin(theta)
	return {
		lng: lngs,
		lat: lats,
	}
}

/**
 *  将百度坐标(DB-09)转为GCJ-02(火星坐标):
 */
export function transformFromBaiduToGCJ(latitude, longitude) {
  var xPi = 3.14159265358979323846264338327950288 * 3000.0 / 180.0;

  var x = longitude - 0.0065;
  var y = latitude - 0.006;
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi);
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi);
  var a_latitude = z * Math.sin(theta);
  var a_longitude = z * Math.cos(theta);

  return { latitude: a_latitude, longitude: a_longitude };
}

/**
 *  判断经纬度是否超出中国境内
 */
function isLocationOutOfChina(latitude, longitude) {
  if (longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271)
    return true;
  return false;
}

function transformLatWithXY(x, y) {
  var pi = 3.14159265358979324;
  var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  return lat;
}

function transformLonWithXY(x, y) {
  var pi = 3.14159265358979324;
  var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  return lon;
}


/**
 * 谷歌地图经纬度转gcj02经纬度
 * @param {Object} lng
 * @param {Object} lat
 */

export function transformFromWGSToGCJ(latitude, longitude) {
  var lat = "";
  var lon = "";
  var ee = 0.00669342162296594323;
  var a = 6378245.0;
  var pi = 3.14159265358979324;

  if (isLocationOutOfChina(latitude, longitude)) {
    lat = latitude;
    lon = longitude;
  }
  else {
    var adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0);
    var adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0);
    var radLat = latitude / 180.0 * pi;
    var magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    var sqrtMagic = Math.sqrt(magic);
    adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
    latitude = latitude + adjustLat;
    longitude = longitude + adjustLon;
  }
  return { latitude: latitude, longitude: longitude };

}