【发布时间】:2010-05-09 09:01:27
【问题描述】:
我正在我的项目中创建一个谷歌地图部分视图/用户控件,该控件传递了一个包含纬度和经度值的强类型对象列表。
目前,这是我的部分代码:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Project.Models.Entities.Location>>" %>
<!-- Place for google to put the map -->
<div id="report_map_canvas" style="width: 100%; height: 728px; margin-bottom: 2px;">
</div>
<script type='text/javascript'>
google.load("maps", "2");
$(document).ready(initializeMap);
function initializeMap() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('report_map_canvas'));
map.setCenter(new GLatLng(51.5, -0.1167), 2);
<% foreach (var item in Model) { %>
map.addOverlay(new GMarker(new GLatLng('<%= Html.Encode(item.latitude)%>','<%= Html.Encode(item.longitude)%>'),{ title: '<%= Html.Encode(String.Format("{0:F}",item.speed)) %> km/h '}));
<% } %>
map.setUIToDefault();
}
}
</script>
以这种方式通过循环遍历列表并发出javascript来动态创建javascript文件是否正确?
有没有更好的方法?
【问题讨论】:
标签: javascript jquery asp.net-mvc google-maps asp.net-mvc-2