0

如何动态设置“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...

        }


    }
4

1 回答 1

1

试试这个:

//get the latitude & longitude of the current location
    var latlong =
        (from u in db.Locations
         where u.Url.Equals(location)
         select u).FirstOrDefault();

    // declarre sourcePoint here
    if(latlong != null)
    {
       //set the latitude & longitude of the sourcePoint
       sourcePoint = GeoUtils.CreatePoint(latlong.Latitude, latlong.Longitude);
    }
于 2013-02-13T13:42:34.990 回答