Example #1
0
        public static double Get(double fromLat, double fromLon, double toLat, double toLon)
        {
            var from = new System.Device.Location.GeoCoordinate(fromLat, fromLon);
            var to   = new System.Device.Location.GeoCoordinate(toLat, toLon);

            return(from.GetDistanceTo(to));
        }
Example #2
0
        public double Distance(Stop other)
        {
            var sCoord = new System.Device.Location.GeoCoordinate(latitude, longitude);
            var eCoord = new System.Device.Location.GeoCoordinate(other.latitude, other.longitude);

            return(sCoord.GetDistanceTo(eCoord));
        }
        public double GetDistanceTo(GeoCoordinate end2Coord)
        {
            var coord1 = new System.Device.Location.GeoCoordinate(this.Latitude, this.Longitude);
            var coord2 = new System.Device.Location.GeoCoordinate(end2Coord.Latitude, end2Coord.Longitude);

            return(coord1.GetDistanceTo(coord2));
        }
        public GeoDistance DistanceBetweenImperial()
        {
            try
            {
                string decimalFormat = ServiceConfiguration["DecimalFormat"].ToString();

                System.Device.Location.GeoCoordinate primaryCoords   = new System.Device.Location.GeoCoordinate(this.PrimaryLatitude, this.PrimaryLongitutde);
                System.Device.Location.GeoCoordinate secondaryCoords = new System.Device.Location.GeoCoordinate(this.SecondaryLatitude, this.SecondaryLongitutde);

                double distanceMetres = primaryCoords.GetDistanceTo(secondaryCoords);

                this.DistanceYards = distanceMetres * 1.09361;
                this.DistanceMiles = this.DistanceYards / 1760;

                this.DistanceYardsDisplay = this.DistanceYards.ToString(decimalFormat);
                this.DistanceMilesDisplay = this.DistanceMiles.ToString(decimalFormat);

                this.ResultStatus = "OK";

                return(this);
            }
            catch (Exception ex)
            {
                this.ResultStatus  = "Exception";
                this.ResultMessage = ex.GetBaseException().Message;
                return(this);
            }
        }
Example #5
0
        /// <summary>
        /// Obtain the distance using as reference origin and destiny airports
        /// </summary>
        /// <param name="originAirport">Origin airport</param>
        /// <param name="destinyAirport">Destiny airport</param>
        /// <returns></returns>
        public static double CalculateDistance(Airport originAirport, Airport destinyAirport)
        {
            // set the Geocoordinate related to origin airport
            var destinyCoord = new System.Device.Location.GeoCoordinate(originAirport.latitude, originAirport.Longitude);

            // set the Geocoordinate related to destiny airport
            var originCoord = new System.Device.Location.GeoCoordinate(destinyAirport.latitude, destinyAirport.Longitude);

            // return the distance in kms
            return(originCoord.GetDistanceTo(destinyCoord) / 1000);
        }
        public double GetDistanceTo(double?i_LatitudeOfUser, double?i_LongitudeOfUser, double?i_LatitudeOfMatch, double?i_LongitudeOfMatch)
        {
            if (i_LatitudeOfUser == null || i_LongitudeOfUser == null || i_LatitudeOfMatch == null || i_LongitudeOfMatch == null)
            {
                throw new ArgumentNullException("While calculating distance, one of the inserted parameters is null.");
            }

            m_CoordinatesOfUser  = new System.Device.Location.GeoCoordinate((double)i_LatitudeOfUser, (double)i_LongitudeOfUser);
            m_CoordinatesOfMatch = new System.Device.Location.GeoCoordinate((double)i_LatitudeOfMatch, (double)i_LongitudeOfMatch);
            Distance             = m_CoordinatesOfUser.GetDistanceTo(m_CoordinatesOfMatch);

            return(Distance);
        }
Example #7
0
 public double RunReals(double[] values)
 {
     if (values.Length >= 4)
     {
         var gc       = new System.Device.Location.GeoCoordinate(values[0], values[1]);
         var distance = gc.GetDistanceTo(new System.Device.Location.GeoCoordinate(values[2], values[3]));
         if (values.Length >= 5)
         {
             if (distance <= values[4])
             {
                 return(1.0);
             }
             return(0.0);
         }
         return(distance);
     }
     return(0.0);
 }
Example #8
0
        public ActionResult Index()
        {
            var summaries = api.GetSummary();
            var model     = new Models.SummaryIndex();

            var list = summaries.Select(x => new Models.Summary()
            {
                Id           = x.Id,
                RouteId      = x.RouteId,
                Vehicle      = x.Vehicle,
                EnterCost    = x.EnterCost,
                DistanceCost = x.DistanceCost,
                DepName      = x.DepName,
                DepLatitude  = x.DepLatitude,
                DepLongitude = x.DepLongitude,
                DesName      = x.DesName,
                DesLatitude  = x.DesLatitude,
                DesLongitude = x.DesLongitude
            }).ToList();

            var gps_src = new System.Device.Location.GeoCoordinate(0, 0);
            var gps_dst = new System.Device.Location.GeoCoordinate(0, 0);

            foreach (var summary in list)
            {
                gps_src.Latitude  = summary.DepLatitude ?? 0;
                gps_src.Longitude = summary.DepLongitude ?? 0;
                gps_dst.Latitude  = summary.DesLatitude ?? 0;
                gps_dst.Longitude = summary.DesLongitude ?? 0;

                summary.Distance = Math.Round(gps_src.GetDistanceTo(gps_dst) / 1000, 2); // in km
                summary.Cost     = Math.Round((summary.EnterCost ?? 0) + (summary.Distance ?? 0) * (summary.DistanceCost ?? 0), 2);
            }

            model.summaries = list;

            return(View(model));
        }
        public GeoDistance WithinRangeMetric()
        {
            try
            {
                string decimalFormat = ServiceConfiguration["DecimalFormat"].ToString();

                System.Device.Location.GeoCoordinate primaryCoords   = new System.Device.Location.GeoCoordinate(this.PrimaryLatitude, this.PrimaryLongitutde);
                System.Device.Location.GeoCoordinate secondaryCoords = new System.Device.Location.GeoCoordinate(this.SecondaryLatitude, this.SecondaryLongitutde);

                double distanceMetres = primaryCoords.GetDistanceTo(secondaryCoords);

                this.DistanceMetres     = distanceMetres;
                this.DistanceKilometres = distanceMetres / 1000;

                this.DistanceMetresDisplay     = this.DistanceMetres.ToString(decimalFormat);
                this.DistanceKilometresDisplay = this.DistanceKilometres.ToString(decimalFormat);

                if (distanceMetres <= this.RangeMetres)
                {
                    this.WithinRange = true;
                }
                else
                {
                    this.WithinRange = false;
                }

                this.ResultStatus = "OK";

                return(this);
            }
            catch (Exception ex)
            {
                this.ResultStatus  = "Exception";
                this.ResultMessage = ex.GetBaseException().Message;
                return(this);
            }
        }
Example #10
0
 public double GetDistance(double xLa, double xLong, double yLa, double yLong)
 {
     System.Device.Location.GeoCoordinate cordx = new System.Device.Location.GeoCoordinate(xLa, xLong);
     System.Device.Location.GeoCoordinate cordy = new System.Device.Location.GeoCoordinate(yLa, yLong);
     return(cordx.GetDistanceTo(cordy));
 }
Example #11
0
        //public IHttpActionResult finddoctor(long specialty_id, string appointment_type, double lat, double longi)
        public IHttpActionResult Getfinddoctor(double lat, double longi, long specialty_id = 0, long appointment_type = 0)
        {
            //var doc1 = Newtonsoft.Json.Linq.JObject.Parse(@"{
            //            'Title': 'Alpha',
            //            'data': [{
            //                'Id': 'Fox 2',
            //                'Field': 'King6',
            //                'Value': 'Alpha',
            //                'Description': 'Tango'
            //            }]
            //        }");

            //var doc2 = Newtonsoft.Json.Linq.JObject.Parse(@"{
            //          'Title': 'Bravo',
            //          'data':[{
            //                'Id':'Kilo',
            //                'Field':'Echo',
            //                'Value':'Romeo',
            //                'Description':'Jester'
            //           }]
            //}");

            //var final = JsonConvert.SerializeObject(new
            //{
            //    Title = doc1["Title"],
            //    data = doc1["data"].Union(doc2["data"])
            //},
            // Newtonsoft.Json.Formatting.Indented);

            //double ref_lat = 39.780319;
            //double re_longi = -89.681066;
            double lat2   = 42.293;
            double longi2 = -83.715363;

            System.Device.Location.GeoCoordinate c1 = new System.Device.Location.GeoCoordinate(lat, longi);

            //var doc = from a in dbEntity.hs_DOCTOR select a;
            var doc      = dbEntity.hs_DOCTOR.Where(a => a.ref_zip.city_lat == lat && a.ref_zip.city_lon == longi);
            var doc_spec = dbEntity.con_DOCTOR_ref_specialty.Where(a => a.rel_ref_specialty_id == specialty_id);



            List <proxi_doc> prox = new List <proxi_doc>();

            //foreach (var n in doc_spec)
            //{
            //    //double d = FindDistance(lat2, longi2, lat, longi);


            //    string c = n.DOCTOR.ref_zip.city_name;
            //    string s = n.DOCTOR.ref_zip.city_state;


            //    if (inKM < 30)
            //    {
            //        addr.Add(new spec_address {
            //            address = n.DOCTOR.addr_address1,
            //            city = n.DOCTOR.ref_zip.city_name,
            //            county = n.DOCTOR.ref_zip.city_county,
            //            state = n.DOCTOR.ref_zip.city_state,
            //            lat = n.DOCTOR.ref_zip.city_lat,
            //            longi = n.DOCTOR.ref_zip.city_lon
            //        });

            //        prox.Add(new proxi_doc {
            //            id = n.DOCTOR.id,
            //            firstname = n.DOCTOR.name_first,
            //            lastname = n.DOCTOR.name_last,
            //            address = addr
            //        });
            //    }
            //}

            foreach (var d in doc)
            {
                List <spec_address> addr = new List <spec_address>();

                double z_lat  = d.ref_zip == null?0:d.ref_zip.city_lat;
                double z_long = d.ref_zip == null ? 0 : d.ref_zip.city_lon;
                System.Device.Location.GeoCoordinate c2 = new System.Device.Location.GeoCoordinate(z_lat, z_long);

                double inKM = c1.GetDistanceTo(c2) / 1000;
                if (inKM < 30)
                {
                    //addr.Add(new spec_address
                    //{
                    //    address = d.addr_address1,
                    //    city = d.ref_zip.city_name,
                    //    county = d.ref_zip.city_county,
                    //    state = d.ref_zip.city_state,
                    //    lat = d.ref_zip.city_lat,
                    //    longi = d.ref_zip.city_lon
                    //});

                    prox.Add(new proxi_doc
                    {
                        id        = d.id,
                        firstname = d.name_first,
                        lastname  = d.name_last,
                        address   = addr
                    });
                }
            }


            return(Json(new { data = prox }));


            //double d = FindDistance(lat, longi, ref_lat, re_longi);
            //1866.8278770706186


            //  return Json(new { d });
        }
        public GeoDistance DistanceBetweenMetric()
        {
            try
            {
                string decimalFormat = ServiceConfiguration["DecimalFormat"].ToString();

                System.Device.Location.GeoCoordinate primaryCoords = new System.Device.Location.GeoCoordinate(this.PrimaryLatitude, this.PrimaryLongitutde);
                System.Device.Location.GeoCoordinate secondaryCoords = new System.Device.Location.GeoCoordinate(this.SecondaryLatitude, this.SecondaryLongitutde);

                double distanceMetres = primaryCoords.GetDistanceTo(secondaryCoords);

                this.DistanceMetres = distanceMetres;
                this.DistanceKilometres = distanceMetres / 1000;

                this.DistanceMetresDisplay = this.DistanceMetres.ToString(decimalFormat);
                this.DistanceKilometresDisplay = this.DistanceKilometres.ToString(decimalFormat);

                this.ResultStatus = "OK";

                return this;
            }
            catch (Exception ex)
            {
                this.ResultStatus = "Exception";
                this.ResultMessage = ex.GetBaseException().Message;
                return this;
            }
        }
        public GeoDistance WithinRangeImperial()
        {
            try
            {
                string decimalFormat = ServiceConfiguration["DecimalFormat"].ToString();

                System.Device.Location.GeoCoordinate primaryCoords = new System.Device.Location.GeoCoordinate(this.PrimaryLatitude, this.PrimaryLongitutde);
                System.Device.Location.GeoCoordinate secondaryCoords = new System.Device.Location.GeoCoordinate(this.SecondaryLatitude, this.SecondaryLongitutde);

                double distanceMetres = primaryCoords.GetDistanceTo(secondaryCoords);

                this.DistanceYards = distanceMetres * 1.09361;
                this.DistanceMiles = this.DistanceYards / 1760;

                this.DistanceYardsDisplay = this.DistanceYards.ToString(decimalFormat);
                this.DistanceMilesDisplay = this.DistanceMiles.ToString(decimalFormat);

                if (this.DistanceYards <= this.RangeYards)
                {
                    this.WithinRange = true;
                }
                else
                {
                    this.WithinRange = false;
                }
                this.ResultStatus = "OK";

                return this;
            }
            catch (Exception ex)
            {
                this.ResultStatus = "Exception";
                this.ResultMessage = ex.GetBaseException().Message;
                return this;
            }
        }
 /// <summary>
 /// Returns the distance between two given GeoCoordinates in kilometers using the "Haversine" formula.
 /// </summary>
 /// <param name="pos1"></param>
 /// <param name="pos2"></param>
 /// <returns></returns>
 public static double GetDistanceInKM(System.Device.Location.GeoCoordinate pos1, System.Device.Location.GeoCoordinate pos2)
 {
     return(pos1.GetDistanceTo(pos2) / 1000);
 }
Example #15
0
        public IHttpActionResult Open(string email, string token, double latitude, double longitude)
        {
            cnx = new doorAccessEntities();

            try
            {
                // check that email matches the format
                if (!rgx.IsMatch(email))
                {
                    return(Ok(new { response = "Bad email", number = 101, detail = "The email you provided does not meet our validation criteria", profile = "" }));
                }

                var x = (from u in cnx.Users
                         where u.email.Equals(email)
                         select u
                         ).FirstOrDefault();

                // Makes sure the user is found in the database
                if (x == null)
                {
                    return(Ok(new { response = "User not found", number = 104, detail = "The user was not found in our records", profile = "" }));
                }

                // Makes sure the token is not empty (another device is not linked to the user's records already)
                if (string.IsNullOrEmpty(x.token))
                {
                    return(Ok(new { response = "User has no Token", number = 106, detail = "User is not registered", profile = "" }));
                }

                // Initialize the access to the
                Models.Access a = new Access();
                a.access_date = DateTime.Now.Date;
                a.access_time = DateTime.Now.TimeOfDay;
                a.user_id     = x.Id;
                a.served      = false;

                // Makes sure the user found is active
                if (!x.is_active)
                {
                    a.status      = 104;
                    a.descripcion = "Inactive User";
                    cnx.Accesses.Add(a);
                    cnx.SaveChanges();
                    return(Ok(new { response = "Inactive User", number = 105, detail = "The user is unable to perform this action", profile = "" }));
                }

                // Makes sure the user's token match
                if (!x.token.Equals(token))
                {
                    a.status      = 107;
                    a.descripcion = "Bad token";
                    cnx.Accesses.Add(a);
                    cnx.SaveChanges();
                    return(Ok(new { response = "Bad token", number = 107, detail = "This user's device is not registered", profile = "" }));
                }
                // Initialize the GeoCoordinates objects (device and door) appConfig[doorLatitude], appConfig[doorLongitude]
                System.Device.Location.GeoCoordinate doorCoordinates   = new System.Device.Location.GeoCoordinate(doorLatitude, doorLongitude);
                System.Device.Location.GeoCoordinate deviceCoordinates = new System.Device.Location.GeoCoordinate(latitude, longitude);

                // If distance between door and device is greater than appConfig[deviceMaxDistance] meters the device is out of range and is not able to open the door
                if (doorCoordinates.GetDistanceTo(deviceCoordinates) > deviceMaxDistance)
                {
                    a.status      = 108;
                    a.descripcion = "Device not in range (" + latitude + ", " + longitude + ")";
                    cnx.Accesses.Add(a);
                    cnx.SaveChanges();
                    return(Ok(new { response = "Device not in range", number = 108, detail = "The device is out of the permit range to perform this action", profile = x.ProfileType.name }));
                }

                a.status      = 0;
                a.descripcion = "Success (" + latitude + ", " + longitude + ")";

                //Tries to open de door using the Uri for the door set in web.config
                string        doorUri  = System.Configuration.ConfigurationManager.AppSettings["doorUri"];
                Task <string> request  = GetAsync(doorUri);
                string        response = request.Result;

                if (response == "{}")
                {
                    return(Ok(new { response = "Success", number = 0, detail = "The door is open", profile = x.ProfileType.name }));

                    a.served = true;
                }
                else
                {
                    return(Ok(new { response = "Door Connection Error", number = 0, detail = "The door is open", profile = x.ProfileType.name }));

                    a.served = false;
                }
                cnx.Accesses.Add(a);
                cnx.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.BadRequest, ex.Message));
            }
            finally
            {
                cnx.Dispose();
            }
        }