-1

I am setting up a Map Screen in XAMARIN Mobile Phone application using C# for which I am trying to write some API's to get the nearest entity locations. I have some entities (marker locations) stored in MySQL Database with Latitude and Longitude. I want to show nearest marker locations according to user's current locations.

Is there any guide or source code help, I can get to create Web API for taking nearest Lats/Longs within a certain range of distance according to current location. All Lat/Longs are saved in MySQL Database and I am using C# for API's.

Thank You for the help if someone can provide.

Jugnu Khan
  • 43
  • 1
  • 6
  • 1
    Possible duplicate of [Calculating Distance between two Latitude and Longitude GeoCoordinates](https://stackoverflow.com/questions/6366408/calculating-distance-between-two-latitude-and-longitude-geocoordinates) – kara Aug 01 '19 at 07:35

1 Answers1

0

You can get distance between two points in(Mines, Kilometers, Nautical Miles) from below code.

 public double distance(double lat1, double lon1, double lat2, double lon2, char unit)
        {
            if ((lat1 == lat2) && (lon1 == lon2))
            {
                return 0;
            }
            else
            {
                double theta = lon1 - lon2;
                double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
                dist = Math.Acos(dist);
                dist = rad2deg(dist);
                dist = dist * 60 * 1.1515;
                if (unit == 'K')
                {
                    dist = dist * 1.609344;
                }
                else if (unit == 'N')
                {
                    dist = dist * 0.8684;
                }
                return (dist);
            }
        }
        private static double deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }

        private static double rad2deg(double rad)
        {
            return (rad / Math.PI * 180.0);
        }

Where unit is M, K or N.

Narendra Sharma
  • 434
  • 3
  • 13