2017-10-21 130 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) });