package com.cpm.lorealbaautomation.utils;

import android.location.Location;

public class DistanceUtils {
    /// /using location api for distance calculation
    public static int calculateDistanceInMeters(double startLat, double startLng, double endLat, double endLng) {
        float[] results = new float[1];
        Location.distanceBetween(startLat, startLng, endLat, endLng, results);
        return (int) results[0]; // Distance in meters
    }

    // Radius of the Earth in meters
    private static final double R = 6371000;
    /// using pure java method for distance calculation
    public static double haversine(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        lat1 = Math.toRadians(lat1);
        lat2 = Math.toRadians(lat2);
        double a = Math.pow(Math.sin(dLat / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
                * Math.pow(Math.sin(dLon / 2), 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        return R * c; // Distance in meters
    }
}
