Tuesday, June 25, 2019

Two location within radius

Some time we need to identify one location latitude and longitude with the radius of other  location. How to check is the location in the radius of other location?



I will show you how we can do with help of Google map Api.
    class Coordinate
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }
    class Program
    {
        public static void Main()
        {
            //Get center location lat and lng
            Coordinate centerLatLong = GetLatLongForAddress("Delhi");
            //Get any location lat and lng
            Coordinate locationLatLong = GetLatLongForAddress("Noida");
            //Radius: Radius in meter
            int radius = 50000;
            //Check wheater  locations lat and long within  radius of a given  point.
            bool check = isInArea(centerLatLong, locationLatLong, radius);
            Console.Write(check);
         
            Console.ReadLine();
        }
        public static Coordinate GetLatLongForAddress(string location)
        {
            string result = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=true", location);
            Coordinate coordinate = new Coordinate();
            byte[] data;
            using (WebClient webClient = new WebClient())
                data = webClient.DownloadData(result);
            string str = Encoding.GetEncoding("Windows-1252").GetString(data);
            XDocument doc = XDocument.Parse(str);
            string status = doc.Element("GeocodeResponse").Element("status").Value;
            //no errors occurred; the address was successfully parsed and at least one geocode was returned.
            if (status.Equals("OK"))
            {
                var point = doc.Element("GeocodeResponse").Element("result").Element("geometry").Element("location");
                string lat = point.Element("lat").Value;
                string lng = point.Element("lng").Value;
                coordinate.Latitude = Convert.ToDouble(lat);
                coordinate.Longitude = Convert.ToDouble(lng);
            }
            else if (status.Equals("ZERO_RESULTS"))
            {
                //Geocode was successful but returned no results
                throw new ApplicationException("No maps found for this address");
            }
            else if (status.Equals("REQUEST_DENIED"))
            {
                //Request was denied
                throw new ApplicationException("Request Denied");
            }
            else if (status.Equals("INVALID_REQUEST"))
            {
                //Address is missing
                throw new ApplicationException("Address not found");
            }
            else if (status.Equals("UNKNOWN_ERROR"))
            {
                //The request could not be processed due to a server error
                throw new ApplicationException("Unknown Error. Try agian.");
            }
            return coordinate;
        }
        public static bool isInArea(Coordinate centerLatLong, Coordinate locationLatLong, int radius)
        {
            var R = 6371;
            var dLat = (centerLatLong.Latitude - locationLatLong.Latitude) * Math.PI / 180;
            var dLon = (centerLatLong.Longitude - locationLatLong.Longitude) * Math.PI / 180;
            var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(locationLatLong.Latitude * Math.PI / 180) * Math.Cos(centerLatLong.Latitude * Math.PI / 180) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            var d = R * c;
            return (d * 1000 <= radius);
        }
    }

1 comment: