(MVC)从MapView的控制器获取纬度、经度?c#
嗨,伙计们,我有这样的模特(职位): 嗨,伙计们,我有一个这样的控制器(LocationController): 我有这样的观点(概述):(MVC)从MapView的控制器获取纬度、经度?c#,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,嗨,伙计们,我有这样的模特(职位): 嗨,伙计们,我有一个这样的控制器(LocationController): 我有这样的观点(概述): 函数GetMap(){} 函数GetMap(){ var latitude=@ViewBag.latitude; var longitude=@ViewBag.longitude; //设置地图和视图选项,将地图样式设置为“道路”和“视图” //删除用户更改地图样式的功能 var映射选项= { 凭证:“Al64oUurZOV-AyLUdQI0i0BSPC7
函数GetMap(){}
函数GetMap(){
var latitude=@ViewBag.latitude;
var longitude=@ViewBag.longitude;
//设置地图和视图选项,将地图样式设置为“道路”和“视图”
//删除用户更改地图样式的功能
var映射选项=
{
凭证:“Al64oUurZOV-AyLUdQI0i0BSPC76kcJc4M2rmA9rSi8VtKhu0GH-qBjVhu4AlzvE”,
高度:400,宽度:960,mapTypeId:Microsoft.Maps.mapTypeId.road,
//编辑地图的视图
showMapTypeSelector:true,
enableSearchLogo:false,
enableClickableLogo:false,
showDashboard:正确
};
//初始化映射
var map=new Microsoft.Maps.map(document.getElementById(“mapDiv”)、mapOptions);
//带标记的硬编码位置
var pushpin=新的Microsoft.Maps.pushpin(新的Microsoft.Maps.Location(纬度、经度),null);
地图。实体。推(图钉);
Microsoft.Maps.Events.addHandler(图钉,“mouseup”,ZoomIn);
//用于缩放到标记的功能
函数ZoomIn(e){}
}
概览只是一张带有硬编码位置的Bing地图。
如何从我的模型中获得概览中的正确位置(纬度、长度)。我从控制器传递数据的方法如下
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>
函数GetMap(){}
函数GetMap(){
var latitude=@ViewBag.latitude;
var longitude=@ViewBag.longitude;
//设置地图和视图选项,将地图样式设置为“道路”和“视图”
//删除用户更改地图样式的功能
var映射选项=
{
凭证:“Al64oUurZOV-AyLUdQI0i0BSPC76kcJc4M2rmA9rSi8VtKhu0GH-qBjVhu4AlzvE”,
高度:400,宽度960,mapTypeId:Microsoft.Maps.mapTypeId.road,
//编辑地图的视图
showMapTypeSelector:true,
enableSearchLogo:false,
enableClickableLogo:false,
showDashboard:正确
};
//初始化映射
var map=new Microsoft.Maps.map(document.getElementById(“mapDiv”)、mapOptions);
//带标记的硬编码位置
var pushpin=新的Microsoft.Maps.pushpin(新的Microsoft.Maps.Location(纬度、经度),null);
地图。实体。推(图钉);
Microsoft.Maps.Events.addHandler(图钉,“mouseup”,ZoomIn);
//用于缩放到标记的功能
函数ZoomIn(e){}
}
使用ViewBag或ViewDataPass将您的模型传递给视图,然后将值分配给javascript变量以在视图中使用script@Anand你是什么意思?@StephenMuecke你能给我举个例子吗?你是什么意思?使用MVCIn控制器更新im-返回视图(位置)
在视图中-`@model Position`和新建Microsoft.Maps.Location(@model.latitude,@model longitude)
(注意,您已经有了一个模型,所以使用ViewBag
)好主意。我现在有一个问题。此lat和long是lat和long的列表。我怎么能说lat和long,他们应该只选择列表中最后一个检索的位置?我希望你知道我在说什么mean@LukasReiner,我已经修改了我的答案,使用最后检索到的位置。这可能会起作用,但我担心的是,如果纬度是双精度和位置。纬度是双精度的列表,这将出错。另外,如果您说ViewBag.Latitude=Latitude[Latitude.Count-1],这将在空序列上出错。我认为最好像这样将值直接弹出到viewbag中:viewbag.latitude=position.latitude,然后如果需要最后一个值,则说viewbag.latitude=position.latitude.LastOrDefault();如果纬度是双精度的,位置是一个列表,那么它们是不兼容的,你将无法编译你的项目。你真的需要变量纬度和经度吗?@D.Mac,如果变量纬度和经度没有用处,我们可以像你说的那样直接将值分配给view bag。在将列表传递给viewbag之前,用户可以对列表纬度和经度执行一些操作,而无需修改位置列表中的原始值
<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();
}
<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>