(MVC)从控制器获取MapView的纬度,经度? C#

时间:2015-04-02 08:27:30

标签: c# asp.net-mvc model-view-controller

大家好我有这样的模特(职位):

namespace moBelegDAL.Models
{
    public class Position : Entity
    {
        public int PositionId { get; set; }
        public string EmployeeName { get; set; }
        public string EmployeeNumber { get; set; }
        public int CompanyId { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Accuracy { get; set; }
        public double Altitude { get; set; }
        public bool AltitudeValid { get; set; }
        public double Heading { get; set; }
        public bool HeadingValid { get; set; }
        public double Speed { get; set; }
        public bool SpeedValid { get; set; }
        public double NumSatellites { get; set; }
        public bool NumSatellitesValid { get; set; }
        public DateTime Timestamp { get; set; }
    }
}

大家好我有这样的控制器(LocationController):

namespace moBelegGUI.Controllers
{
    [Authorize]
    public class LocationController : BootstrapBaseController
    {
        public double latitude;
        public double longitude;



        public ActionResult OverView(Position position)
        {
            latitude = position.Latitude;
            longitude = position.Longitude;
            ViewBag.Latitude = latitude;
            ViewBag.Longitude = longitude;
            return View();
        }
    }
}

我有一个像这样的视图(OverView):

<body onload="GetMap();">

    <script type="text/javascript"> function GetMap() { }</script>
    <script type="text/javascript">

    function GetMap() {

        var latitude = @ViewBag.Latitude;
        var longitude = @ViewBag.Longitude;


        // Set the map and view options, setting the map style to Road and
        // removing the user's ability to change the map style
        var mapOptions =
            {
                credentials: "Al64oUurZOV-AyLUdQI0i0BSPC76kcJc4M2rmA9rSi8VtKhu0GH-qBjVhu4AlzvE",
                height: 400, width: 960, mapTypeId: Microsoft.Maps.MapTypeId.road,

                //Edit the Views of the Map
                showMapTypeSelector: true,
                enableSearchLogo: false,
                enableClickableLogo: false,
                showDashboard: true
            };

        // Initialize the map
        var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), mapOptions);

        //Hardcode Location with Marker
        var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(latitude, longitude), null);
        map.entities.push(pushpin);
        Microsoft.Maps.Events.addHandler(pushpin, "mouseup", ZoomIn);

        //Function for zoom to the Marker
        function ZoomIn(e) { }


    }

    </script>
    <asp:Literal ID="Literal1" runat="server">
    </asp:Literal>

</body>

概述仅是具有硬编码位置的Bing地图。 如何在我的模型概述中获得正确的位置(lat,long)。

1 个答案:

答案 0 :(得分:0)

我从控制器传递数据的方法就是这样的

 public ActionResult OverView(Position position)
        {
             latitude = position.Latitude;
             longitude = position.Longitude;

             //Passing the latitude and longitude values of last element in latitude and longitude lists
             ViewBag.Latitude = latitude;
             ViewBag.Longitude = longitude;
             return View();
        }

JavaScript就像:

<body onload="GetMap();">

    <script type="text/javascript"> function GetMap() { }</script>
    <script type="text/javascript">

    function GetMap() {

        var latitude = @ViewBag.Latitude;
        var longitude = @ViewBag.Longitude;

        // Set the map and view options, setting the map style to Road and
        // removing the user's ability to change the map style
        var mapOptions =
            {
                credentials: "Al64oUurZOV-AyLUdQI0i0BSPC76kcJc4M2rmA9rSi8VtKhu0GH-qBjVhu4AlzvE",
                height: 400, width: 960, mapTypeId: Microsoft.Maps.MapTypeId.road,

                //Edit the Views of the Map
                showMapTypeSelector: true,
                enableSearchLogo: false,
                enableClickableLogo: false,
                showDashboard: true
            };

        // Initialize the map
        var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), mapOptions);

        //Hardcode Location with Marker
        var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(latitude, longitude), null);
        map.entities.push(pushpin);
        Microsoft.Maps.Events.addHandler(pushpin, "mouseup", ZoomIn);

        //Function for zoom to the Marker
        function ZoomIn(e) { }


    }

    </script>
    <asp:Literal ID="Literal1" runat="server">
    </asp:Literal>

</body>