-2

Suppose I have lat1=21.154533 & long1=79.045760 with accuracy of x meters, and another lat2=21.153184 & long2=79.045882. So can I find out the nearest position (lat,long) with respect to lat1 & long1 with the help of lat2 & long2 direction?

enter image description here

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Ashwin
  • 5
  • 1
  • 4
  • Check [this](https://stackoverflow.com/a/6671240/4471013) – Abdur Rahim Mar 13 '18 at 07:05
  • 1
    This is unclear. What are you trying to find? The middle point between 2 points? Why do you need the direction if you already know point 1 and 2? What does the accuracy have to do with the placement of 1 point? Define "nearest position" - Nearest to what? Nearest by 1 km? 1 meter? 1 centimeter? 1 millimeter? – MrUpsidown Mar 13 '18 at 08:09
  • i am trying to find nearest coordinates from lat1, long1 because my lat2 & long2 where not accurate every time its an dynamic value with less accuracy which depend upon a device. so because of this scenario i am trying to find out coordinates from lat1 long1! (hope these will help to figure out ) – Ashwin Mar 13 '18 at 08:34

1 Answers1

0

Finally I have found answer to my own question.

  • Calculate Bearing/Angle using Lat1,Long1 & Lat2,Long2
    function bearing($lat1, $long1, $lat2, $long2){
    $bearingradians = atan2(asin($long1-$long2)*cos($lat2),
    cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($long1-$long2)); 
    $bearingdegrees = abs(rad2deg($bearingradians));
    return $bearingdegrees;
    }
  • Calculate Distance between Lat1,Long1 & Lat2,Long2
function distance($lat1, $lon1, $lat2, $lon2) {
      $theta = $lon1 - $lon2;
      $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
      $dist = acos($dist);
      $dist = rad2deg($dist);
      $distanceMeters = $dist * 60 * 1000;
     return $distanceMeters; 
    }
  • Then Generate New Lat Long near to Lat1,Long2.
$earthRadius = 6371000;
$lat1 = deg2rad(21.156845);
$lon1 = deg2rad(79.053331);
$lat2 = deg2rad(21.156805);
$lon2 = deg2rad(79.057634);

//$bearing = $this->bearing($lat1,$lon1,$lat2,$lon2); //you can get value from above function
//$distance = $this->distance($lat1,$lon1,$lat2,$lon2);//you can get value from above function
$bearing = deg2rad(90);
$distance =100;
$Newlat = asin(sin($lat1) * cos($distance / $earthRadius) + cos($lat1) * sin($distance / $earthRadius) * cos($bearing));
$Newlon = $lon1 + atan2(sin($bearing) * sin($distance / $earthRadius) * cos($lat1), cos($distance / $earthRadius) - sin($lat1) * sin($Newlat));
echo 'distance: ' .$distance;
echo 'bearing: ' .$bearing;
echo 'LAT: ' . rad2deg($Newlat);
echo 'LNG: ' . rad2deg($Newlon);

The conclusion is if we get incorrect lat2,long2 values. But we can exact calculate lat,long near lat1 and lat2 with its accurate angle. This challenge we found while integrate beacons technology.

Ashwin
  • 5
  • 1
  • 4