daniel-s
6/7/2014 - 10:23 AM

Calculate distance between two GPS points.

Calculate distance between two GPS points.

/**
	 * Calculate distance between two gps points based in Haversine formula: http://en.wikipedia.org/wiki/Haversine_formula
	 * 
	 * @param latitude1 GPS point 1 Latitude
	 * @param longitude1 GPS point 1 Longitude
	 * @param latitude2 GPS point 2 Latitude
	 * @param longitude2 GPS point 1 Longitude
	 * @return Distance in kilometers (km)
	 */
	static double haversineDistance(float latitude1, float longitude1, float latitude2, float longitude2)
	{
		double h = (1 - Math.cos(latitude1 * Math.PI / 180 - latitude2 * Math.PI / 180)) / 2 + Math.cos(latitude2 * Math.PI / 180) * Math.cos(latitude1 * Math.PI / 180) * (1 - Math.cos(longitude1 * Math.PI / 180 - longitude2 * Math.PI / 180)) / 2;
		
		h += 2 * 6367.45 * Math.asin(Math.sqrt(h));
		
		return h;
	}