SoFunction
Updated on 2025-02-28

Positioning the geographical location PHP determines whether the latitude and longitude of employees check-in are within the clock-in

The specific code is as follows:

 /* Calculate the distance between two sets of latitude and longitude coordinates
   * @param $lat1 latitude 1
   * @param $lng1 Longitude 1
   * @param $lat2 latitude 2
   * @param $lng2 Longitude 2
   * @param int $len_type Return value type (1-m 2-km)
   * @param int $decimal Reserve the number of decimals
   * @return float
   */
  public function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
  {
    $radLat1 = $lat1 * 3.1415926 / 180.0;
    $radLat2 = $lat2 * 3.1415926 / 180.0;
    $a = $radLat1 - $radLat2;
    $b = ($lng1 * 3.1415926 / 180.0) - ($lng2 * 3.1415926 / 180.0);
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * 6378.137;
    $s = round($s * 1000);
    if ($len_type > 1) {
      $s /= 1000;
    }
    return round($s, $decimal);
  }

ps: Let's see if the check-in range is within the specified range based on the latitude and longitude

/**
   * Convert angle to radians
   * @param d angle
   * @return Arc
   */
 private static double rad(double d) {
     return d *  / 180.0;
 }
 
 /**
   * First obtain the distance (unit: meters) through the latitude and longitude, and then determine whether a point is in the circular area (compared according to the given radius)
   * @param n1=>app
    * @param n2=>Repository
   * @param radius
   * @return
   */
 public static boolean isInCircle(ZJPoint n1 ,ZJPoint n2,String radius){
  final double EARTH_RADIUS = 6378.137;/////The radius of the earth (km)  double radLat1 = rad(()!=null ? ().doubleValue():0);
   double radLat2 = rad(()!=null ? ().doubleValue():0);
   double radLon1 = rad(()!=null ? ().doubleValue():0);
   double radLon2 = rad(()!=null ? ().doubleValue():0);
   //The difference between two points   double jdDistance = radLat1 - radLat2;
   double wdDistance = radLon1 - radLon2;
   double distance = 2 * ((((jdDistance / 2), 2) +
         (radLat1) * (radLat2) * ((wdDistance / 2), 2)));
   distance = distance * EARTH_RADIUS;
   distance = (distance * 10000d) / 10000d;
   distance = distance*1000;//Convert the calculated distance kilometers into meters   double r = (radius);
   //Distinguish whether a point is in a circular area   if (distance > r) {
       return false;
    }
  return true;
 }

Summarize

The above is the location and location that the editor introduced to you. PHP determines whether the latitude and longitude of employees check-in and check-in is within the check-in. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!