package com.game;

import com.taurus.core.util.StringUtil;

/**
 * @author dong.teng
 */
public class GPSUtil {
	// 地球半径
	private static final double EARTH_RADIUS = 6378137;

	private static double radian(double lngLat) {
		return lngLat * Math.PI / 180.0;
	}

	/**
	 * 根据两点间经纬度坐标,计算直线距离 单位 ：米
	 */
	public static double getDistance(String gps1, String gps2) {
		double distance = 0;
		if (isOpenGPS(gps1) && isOpenGPS(gps2)) {
			String[] gps1Arr = gps1.split(",");
			String[] gps2Arr = gps2.split(",");
			Double lat1 = Double.parseDouble(gps1Arr[1]);
			Double lng1 = Double.parseDouble(gps1Arr[0]);
			Double lat2 = Double.parseDouble(gps2Arr[1]);
			Double lng2 = Double.parseDouble(gps2Arr[0]);
			double radLat1 = radian(lat1);
			double radLat2 = radian(lat2);
			double a = radLat1 - radLat2;
			double b = radian(lng1) - radian(lng2);
			distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
			distance *= EARTH_RADIUS;
			distance = Math.round(distance * 10000) / 10000;
		}
		return distance;
	}

	/**
	 * 两位置是否过近
	 * 
	 * @param gps1
	 * @param gps2
	 * @param measure
	 *            衡量过近米
	 * @return
	 */
	public static boolean isDistanceNear(String gps1, String gps2, int measure) {
		return getDistance(gps1, gps2) <= measure;
	}

	/**
	 * 是否打开gps
	 */
	public static boolean isOpenGPS(String gps) {
		return StringUtil.isNotEmpty(gps) && gps.contains(",");
	}

	public static void main(String[] args) {
		//System.out.println(isDistanceNear("109.19300079346,27.710248947144", "100.23030090332,25.59494972229",50));
	}

}
