所以我在一个 asp.net MVC4地理定位项目中工作,我的 foreach 循环有问题,我的地图只显示最后一个位置,这是我的视图代码:
@model List<AgencyWebApplication.Models.HomeModel>
@foreach(var ch in Model)
{
<script>
var city = '@Html.Raw(ch.adress)';
var attitude = '@Html.Raw(ch.attitude)';
var longitude = '@Html.Raw(ch.longidue)';
var indice = '@Html.Raw(ch.indice)';
var array = [[city, attitude, longitude, indice]];
</script>
}
<script src="@Url.Content("~/Scripts/Map.js")" type="text/javascript"></script>
<input type="submit" name="Go" value="Go" onclick="getMap(array)" />
<div id="map_canvas" style="width:600px; height:400px"></div>
这是我的地图脚本代码:
function getMap(array) {
var myLatlng = new google.maps.LatLng(35.728329, -5.882750);
var mapOptions = {
center: myLatlng,
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
setMarkers(map, array);
}
function setMarkers(map, locations) {
for (var i = 0; i < locations.length; i++)
{
var place = locations[i];
var myLatLng = new google.maps.LatLng(place[1], place[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: place[0],
zIndex: place[3]
});
}
return marker;
}
因此,如果您有任何想法,我将不胜感激。