title: Calculate the radius around a lat/lon GPS point in PHP
body: <p>I'm trying to make a PHP script that can do something when a GPS device (tracker) enters a "geofence" with a predefined position. Most of the help I have found uses the haversine formula, but most try to get a nearest location from a point. I pretty much need the opposite.<br>
  The device and "fence location" coordinates are in a MySQL database, I am able to pull them sucessfully. Now I need to set the radius around the fence location center, which should be 10 meters in any direction from that point.<br>
  Whenever the device enters within this 10m radius, PHP does something.<br>
  So point A(lat,lon) has a 10m radius around it, device B is sending its lat,lon into the SQL database and whenever point A and B are within 10m of each other, something happens.
  Where do I start? Is the haversine of any use in my situation?
  I think PHP could caluculate the distance difference of A and B an whenever that distance is under 10m, PHP does something.</p>
  
  <p>I think this could do the job.</p>
  
  <pre><code>function haversineGreatCircleDistance(
    $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
  {
    // convert from degrees to radians
    $latFrom = deg2rad($latitudeFrom);
    $lonFrom = deg2rad($longitudeFrom);
    $latTo = deg2rad($latitudeTo);
    $lonTo = deg2rad($longitudeTo);
  
    $latDelta = $latTo - $latFrom;
    $lonDelta = $lonTo - $lonFrom;
  
    $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
      cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
    return $angle * $earthRadius;
  }
  </code></pre>