public IActionResult ObtainAll(double latitude, double longitude) { CustomStatusCode code; var jwt = HttpRequest(); var all = _longitudeResource.LAndLSearchAll(jwt.Id); var resource = _mapper.Map <IEnumerable <LAndLSearchMapper> >(all); if (resource == null) { code = new CustomStatusCode { Status = 404, Message = $"获取所有经纬度为空" }; return(StatusCode(404, code)); } foreach (var s in resource) { s.Distance = LocationUtils.GetDistance(latitude, longitude, s.Latitude, s.Longitude); } resource = resource.OrderBy(x => x.Distance); code = new CustomStatusCode { Status = 200, Message = $"获取所有经纬度成功", Data = resource }; return(StatusCode(200, code)); }
public IActionResult SearchChildrenLalApi(int sonId, double latitude, double longitude) { CustomStatusCode code; var log = _parentResource.SearchChildrenLal(sonId); if (log == null) { _logger.LogInformation($"用户查询 {sonId} 为空"); code = new CustomStatusCode { Status = "404", Message = $"用户查询 {sonId} 为空" }; return(StatusCode(404, code)); } var resource = _mapper.Map <IEnumerable <LAndLSearchMapper> >(log); foreach (var s in resource) { s.Distance = LocationUtils.GetDistance(latitude, longitude, s.Latitude, s.Longitude); } _logger.LogInformation($"用户查询 {sonId} 成功"); code = new CustomStatusCode { Status = "200", Message = $"用户查询 {sonId} 成功", Data = resource }; return(StatusCode(200, code)); }
public void GetDistance() { // Values used for comparison comes from lines plotted into Google Maps. Google uses a varying number // decimals depending on the distance, so we loose a bit precision when rounding. But the results of this // unit test still shows that the method works (eg. the test will fail if using the mean radius rather than // the equatorial radius). var samples = new[] { new { From = new EssentialsLocation(55.6946159, 10.0366974), To = new EssentialsLocation(55.6477614, 10.1589203), Expected = "9.28", Decimals = 2 }, new { From = new EssentialsLocation(54.8671242, 7.7124023), To = new EssentialsLocation(56.8159142, 12.2113037), Expected = "355", Decimals = 0 }, new { From = new EssentialsLocation(49.2104204, -6.1083984), To = new EssentialsLocation(59.578851, 22.9833984), Expected = "2184", Decimals = 0 }, new { From = new EssentialsLocation(12.3829283, -71.3671875), To = new EssentialsLocation(71.2443555, 25.4882813), Expected = "8958", Decimals = 0 } }; foreach (var sample in samples) { string format = "{0:0." + ("".PadLeft(sample.Decimals, '0')) + "}"; Assert.AreEqual(sample.Expected, String.Format(CultureInfo.InvariantCulture, format, LocationHelper.GetDistance(sample.From, sample.To) / 1000)); Assert.AreEqual(sample.Expected, String.Format(CultureInfo.InvariantCulture, format, LocationUtils.GetDistance(sample.From, sample.To) / 1000)); } }
/// <summary> /// Calculates the distance in meters between two GPS locations. /// </summary> /// <param name="loc1">The first location.</param> /// <param name="loc2">The second location.</param> public static double GetDistance(this ILocation loc1, ILocation loc2) { return(LocationUtils.GetDistance(loc1, loc2)); }