本文介绍了与经度和纬度查找最接近的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,我需要得到附近的位置,
我的web服务将获得2个参数(十进制经度,数字纬度)



我所在的位置与经度和纬度领域保存在数据库中的表,



我要检索最近的位置。


$ b ?$ b

谁能帮



下面是我的代码:

 从升的位置VAR位置= 

选择L

下面对于此次的详细信息:
I有个2场(十进制(18,2)NULL)1纬度,一个数据库表内的2经度,



和我有一个方法

 公开名单<地点和GT;的getLocation(十进制?龙,小数?LAT)
{
无功禄=由升的位置
////现在这里是如何得到最近的位置?如何查询?
////我自己也尝试Math.Abs​​(l.Lat - LAT)及其可空小数总是因此我已经seted小数可空或转换为可空
////也给错误我试图在那里(l.lat - 纬度)*(l.lon - 长),这也提供有关小数不能转换为BOOL
返回Loc.ToList错误();
}


解决方案

下面是解决方案。



  VAR constValue = 57.2957795130823D 

VAR constValue2 = 3958.75586574D;

VAR searchWithin = 20;

双纬度= ConversionHelper.SafeConvertToDoubleCultureInd(纬度,0),
经度= ConversionHelper.SafeConvertToDoubleCultureInd(经度,0);
VAR LOC =(从升的DB.locations
设临时= Math.Sin(Convert.ToDouble(l.Latitude)/ constValue)* Math.Sin(Convert.ToDouble(纬度)/ constValue) +
Math.Cos(Convert.ToDouble(l.Latitude)/ constValue)*
Math.Cos(Convert.ToDouble(纬度)/ constValue)*
Math.Cos((转换。 ToDouble(经度)/ constValue) - (Convert.ToDouble(l.Longitude)/ constValue))
让calMiles =(constValue2 * Math.Acos(温度→1 1:?(温度&下; -1 - 1:TEMP)))
,其中(l.Latitude大于0和放大器;&安培; l.Longitude大于0)
排序依据calMiles

选择新位置
{
名称= l.name
});
返回LOC .ToList();


I am working on a application where I need to get nearby location,my web service will receive 2 parameters (decimal longitude, decimal latitude )

I have a table where the locations are saved in database with longitude and latitude fields,

I want to retrieve the nearest locations.

Can anyone help?

Here is my code:

 var locations = from l in locations

     select l

Here are further details about this :i have a 2 fields (decimal(18, 2) null) 1 latitude, 2 longitude inside a database table,

and i have a method

public List<Locations>  GetLocation(decimal? Long, decimal? lat)
{
var Loc = from l in Locations
  //// now here is how to get nearest location ? how to query?
  //// i have also tried Math.Abs(l.Lat - lat) its giving error about nullable decimal always hence i have seted decimal to nullable or converted to nullable
 //// also i have tried where (l.lat - Lat) * (l.lon - Long)  this is also giving error about can not convert decimal to bool
return Loc.ToList();
}
解决方案

Here is Solution

var constValue = 57.2957795130823D

var constValue2 = 3958.75586574D;

var searchWithin = 20;

double latitude = ConversionHelper.SafeConvertToDoubleCultureInd(Latitude, 0),
                    longitude = ConversionHelper.SafeConvertToDoubleCultureInd(Longitude, 0);
var loc = (from l in DB.locations
let temp = Math.Sin(Convert.ToDouble(l.Latitude) / constValue) *  Math.Sin(Convert.ToDouble(latitude) / constValue) +
                                 Math.Cos(Convert.ToDouble(l.Latitude) / constValue) *
                                 Math.Cos(Convert.ToDouble(latitude) / constValue) *
                                 Math.Cos((Convert.ToDouble(longitude) / constValue) - (Convert.ToDouble(l.Longitude) / constValue))
                             let calMiles = (constValue2 * Math.Acos(temp > 1 ? 1 : (temp < -1 ? -1 : temp)))
                             where (l.Latitude > 0 && l.Longitude > 0)
                             orderby calMiles

select new location
  {
     Name = l.name
  });
  return loc .ToList();

这篇关于与经度和纬度查找最接近的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 22:27