2017-10-21 129 views
0

这是计算距离从当前位置到另一个列表中的距离的JSON结果,但我有一个问题,我无法将距离单位(度)转换为从dbgeography.location.distance到km的问题这是我的代码:在dbgeography中如何返回距离km?

string CurrentLocation = String.Format("POINT(" + CurrentLat + " " + CurrentLng + ")", 4326); 
       DbGeography point = DbGeography.PointFromText(CurrentLocation, DbGeography.DefaultCoordinateSystemId); 

       var places = (from u in context.schoolinfo 

           orderby u.Location.Distance(point.Buffer(dist)) 

           select u).Take(10).Select(x => new schoollinfo() { Name = x.name, Lat = x.Location.Latitude, Lng = x.Location.Longitude, Distance = x.Location.Distance(point.Buffer(dist)) }); ; 

       var nearschools = places.ToList(); 
       return Json(nearschools, JsonRequestBehavior.AllowGet); 

Print screen of result

+0

是否https://earthscience.stackexchange.com /曲estions/7350/convert-grid-resolution-from-degrees-to-kilocity或https://stackoverflow.com/questions/13934169/system-data-spatial-dbgeography-distance-units help? – mjwills

+0

@mjwills第一个链接它interessant,但它仍然很难.. –

回答

0

为calaculate远处看的角度(经纬度)不点(经纬度),这样的代码会因为这改变:

 select u).Take(10).Select(x => new schoollinfo() { Name = x.name, Lat = x.Location.Latitude, Lng = x.Location.Longitude, Distance = DbGeography.FromText("POINT(" + x.Location.Longitude + " " + x.Location.Latitude + ")", 4326).Distance(point) });