「sourcePoint」の緯度と経度を動的に設定するにはどうすればよいですか?
データベースにクエリを実行して必要な情報 (緯度と経度) を取得できますが、これらの値を使用して sourcePoint を動的に設定する方法がわかりません。
これは、C#、MVC 4、Entity Framework 5、および Linq での私の最初のショットなので、これについての助けをいただければ幸いです。事前にご協力いただきありがとうございます:)
public ActionResult Index(string location = "melbourne-vic-3000")
{
////get the latitude & longitude of the current location
//var latlong =
// (from u in db.Locations
// where u.Url.Equals(location)
// select u).Take(1);
//set the latitude & longitude of the sourcePoint
var sourcePoint = GeoUtils.CreatePoint(-37.815206, 144.963937); //***NEED TO GET THE LATITUDE & LONITUDE FROM THE DATABASE (see query above)***
//work out the distance each business is from the sourcePoint
var model =
from x in db.Businesses
from y in db.Locations
where location == y.Url
select new BusinessDistance
{
BusinessName = x.BusinessName,
Address = x.Address,
Distance = Math.Round((x.GeoLocation.Distance(sourcePoint).Value / 1000), 2)
};
return View(model.ToList());
}
---- 作業コード ----
public ActionResult Index(string location = null)
{
var sourcePoint = GeoUtils.CreatePoint(-37.815206, 144.963937); //Set the default latitude & longitude to Melbourne
//get the latitude & longitude of the current location from the database
var latlong =
(from u in db.Locations
where u.Url.Equals(location)
select u).FirstOrDefault();
if (latlong != null)
{
//dynamically set the latitude & longitude of the sourcePoint using the latitude & longitude we got from the database
sourcePoint = GeoUtils.CreatePoint((float)Convert.ToDouble(latlong.Latitude), (float)Convert.ToDouble(latlong.Longitude));
}
//work out the distance each business is from the sourcePoint
var model =
from x in db.Businesses
from y in db.Locations
where location == y.Url
select new BusinessDistance
{
LocationSuburb = y.Suburb,
BusinessName = x.BusinessName,
Address = x.Address,
Distance = Math.Round((x.GeoLocation.Distance(sourcePoint).Value / 1000), 2)
};
if (model != null)
{
return View(model.ToList());
}
else
{
//display the error page
return View(); //TO DO...
}
}