使用mvc模型在Google Maps API v3
Using mvc model to display markers on Google Maps API v3
我正在尝试发送一个IEnumerable模型,其中包括我想在我的网站上显示的谷歌地图上显示的纬度和经度值。
以下是我在视图中用于显示地图的代码:
@model IEnumerable<ProjectName.Models.modelname>
@{
ViewBag.Title = "Map View";
}
<script src="http://maps.google.com/maps/api/js?key=My-API-Key" type="text/javascript"></script>
<style>
#map_canvas img {
max-width: none;
}
</style>
<style>
.infoDiv {
height: 200px;
width: 300px;
-webkit-user-select: none;
background-color: white;
}
</style>
<h2>Map</h2> <a class="btn btn-default" href="/Tags/Create">Create Tag »</a>
<hr />
<div id="map_canvas" style="height: 600px;"></div>
@section scripts {
<section class="scripts">
<script type="text/javascript">
$(document).ready(function () {
Initialize();
});
function Initialize() {
google.maps.visualRefresh = true;
var Liverpool = new google.maps.LatLng(-26.71452, 27.097047);
var mapOptions = {
zoom: 14,
center: Liverpool,
mapTypeId: google.maps.MapTypeId.G_NORMAL_MAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var myLatlng = new google.maps.LatLng(-26.674359 , 27.095391);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Tate Gallery'
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')
var data = [
{ "Id": 1, "PlaceName": "Liverpool Museum", "OpeningHours":"9-5, M-F","GeoLong": "53.410146", "GeoLat": "-2.979919" },
{ "Id": 2, "PlaceName": "Merseyside Maritime Museum ", "OpeningHours": "9-1,2-5, M-F", "GeoLong": "53.401217", "GeoLat": "-2.993052" },
{ "Id": 3, "PlaceName": "Walker Art Gallery", "OpeningHours": "9-7, M-F", "GeoLong": "53.409839", "GeoLat": "-2.979447" },
{ "Id": 4, "PlaceName": "National Conservation Centre", "OpeningHours": "10-6, M-F", "GeoLong": "53.407511", "GeoLat": "-2.984683" }
];
$.each(data, function (i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(item.GeoLong, item.GeoLat),
'map': map,
'title': item.PlaceName
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png')
var infowindow = new google.maps.InfoWindow({
content: "<div class='infoDiv'><h2>" + item.PlaceName + "</h2>" + "<div><h4>Opening hours: " + item.OpeningHours + "</h4></div></div>"
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
})
}
</script>
</section>
}
我目前正在用伪数据预填充它,但我无法从我的模型中检索数据。
由于无法在脚本中运行foreach循环,我将如何使用该模型?
提前感谢!
找到答案了!我需要使用以下方法从控制器返回我的JSon数据:
public ActionResult MapView()
{
var tags = db.Tags.ToList();
var jsonTags = from x in tags
select new Tags
{
GeoName = x.GeoName,
GeoLat = x.GeoLat,
GeoLong = x.GeoLong
};
return View(jsonTags);
}
谢谢你的帮助!Json.Encode也是需要的!
在您的视图Json.Encode可以将您的列表转换为Json数组
剃须刀
@Html.Raw(Json.Encode(YourModel))
ASPX-
<%=html.Raw(Json.Encode(YourModel)%>
-
var data = <%=html.Raw(Json.Encode(YourModel)%>;
相关文章:
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 使用Google Maps API向标记添加多个字符
- Google Maps API OverlayView()在AngularJS指令中不起作用
- 使用Google Maps API驱动时间多边形
- 如何在Maps API v3.中更改多段线的路径
- 根据PHP中数组的长度在Google Maps API中循环遍历标记
- 加载Google MAPS API v3的JSON数据
- Javascript:Google Maps API:如果地图加载在隐藏容器中,则为空白地图
- 来自JSON数组的Google Maps API生成器
- JavaScript 的全新功能,使用 Google Maps API v3 w/ custom fitbounds (
- ETA在Android / IOS上使用Google Maps API
- 从Google Maps API V3中删除标记
- Slick.js不适用于Google Maps API中的infoBox.js
- 如何制作Google Maps API v3六边形拼接地图,最好是基于坐标的
- Google Maps API a.lat不是函数错误
- 在Javascript/Google Maps Api中将结果放入数组时出现问题
- 当鼠标光标纬度等于标记纬度时,发出警报(Google Maps API v3)
- Google Maps API v3 MapTypeId作为变量
- Clojudescript:调用Google Maps API的问题
- .Net windows应用程序WebBrowser/Google Maps API v3