簡體   English   中英

(MVC)從控制器獲取MapView的緯度,經度? C#

[英](MVC) Get Latitude, Longitude from Controller for the MapView? c#

大家好,我有一個這樣的模型(位置):

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>

概述只是帶有硬編碼位置的必應地圖。 如何從模型中獲得概述中的正確位置(經緯度)。

我從控制器傳遞數據的方法是這樣的

 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM