【问题标题】:Using Google Map in asp.net mvc Entity Framework在 asp.net mvc 实体框架中使用谷歌地图
【发布时间】:2013-12-01 07:59:49
【问题描述】:

我想将 Google Map 集成到 Asp.net mvc Entity 框架中。我想根据位置搜索条件将纬度和经度从控制器传递到视图,该部分正在工作,但我不知道如何将其传递给 Javascript 部分,即根据控制器的返回值动态地传递纬度和经度。请帮忙解决我的问题..

HomeController.cs

       LocationDbEntities dbentity = new LocationDbEntities();
        public ActionResult Index()
        {
            int id = 2;
            var query = dbentity.LocationDetails.First(c => c.Id == id);
            return View("Index", query);
        }

索引视图

@model MvcApplication2.Models.LocationDetail
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>@Model.Latitude </p>
<p>@Model.Longitude</p>

 <div id="map_canvas" style="width: 640px; height: 480px;">  
 </div>  

_Layout.cshtml

     <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>  
 <script type="text/javascript">
     $(document).ready(function () {
         initialize();
     });
     function initialize() {
         var mapOptions = {
             center: new google.maps.LatLng(17.897400, 77.519500),
             zoom: 10,
             mapTypeId: google.maps.MapTypeId.ROADMAP
         };
         var map = new google.maps.Map(document.getElementById("map_canvas"),
       mapOptions);
         // create a marker  
         var latlng = new google.maps.LatLng(6.9167, 79.8473);
         var marker = new google.maps.Marker({
             position: latlng,
             map: map,
             title: 'My Place'
         });
     }  
 </script>  

【问题讨论】:

    标签: c# asp.net asp.net-mvc google-maps asp.net-mvc-4


    【解决方案1】:

    您可以像这样在脚本标签内的模型属性中设置 JavaScript 变量。

    <script>
       var myLat = '@Model.Latitude';
       var myLong = '@Model.Longitude';
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 2020-05-26
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 2011-02-20
      相关资源
      最近更新 更多