asp.net - Using LINQ find nearby places from database -
we want receive list of nearby places database using linq in asp.net 2012 , feedback on our strategy.
my table , fake data:
placeid name latitude longitude 1 18.1 20.1 2 b 18.2 20.2 3 c 18.3 20.3
1) in our project client current location (latitude , longitude) taken input
2) @ server side ,depending upon client current location, need find nearby places database using linq
here's code sql earlier used , want use linq.
select name, latitude, longitude , ( 3959 * acos( cos( radians(?) )* cos( radians( latitude) ) * cos( radians( longitude ) - radians(?) ) + sin( radians(?) ) * sin( radians( latitude) ) ) ) distance table_name having distance < ? order distance limit 0 , 20
[but question how write such query in linq.]
my work on this:
while searching solution, came across code
var value1 = 57.2957795130823d; var value2 = 3958.75586574d; var searchwithin = 20; double latitude = conversionhelper.safeconverttodoublecultureind(latitude, 0), longitude = conversionhelper.safeconverttodoublecultureind(longitude, 0); var location = (from l in sdbml.places let temp = math.sin(convert.todouble(l.latitude) / value1) * math.sin(convert.todouble(latitude) / value1) + math.cos(convert.todouble(l.latitude) / value1) * math.cos(convert.todouble(latitude) / value1) * math.cos((convert.todouble(longitude) / value1) - (convert.todouble(l.longitude) / value1)) let calmiles = (value2 * math.acos(temp > 1 ? 1 : (temp < -1 ? -1 : temp))) (l.latitude > 0 && l.longitude > 0) orderby calmiles select new location { name = l.name }); return location .tolist();
but problem ,how reference conversionhelper or under namespace comes.
all advice appreciated.
so, if want calculate distance between 2 co-ordinates, why don't use dot net's geocoordinate
?
it goes
var firstcordinate = new geocoordinate(latitude1, longitude1); var secondcordinate = new geocoordinate(latitude2, longitude2); double distance = firstcordinate.getdistanceto(secondcordinate);
you can find inside namespace system.device.location
.
so save math.cos
, math.sin
, linq plain , simple. (probably foreach loop do)
so entire query can summarized as:
list<location> locations = new list<location>(); foreach(var place in sdbml.places) { //your logic compare various place's co-ordinates of //user's current co-ordinate }
Comments
Post a Comment