【发布时间】: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