我是使用 javascript 和 ASP.Net MVC 4 的新手。但我尝试在 Razor @Model 的 bing 地图中添加多个引脚。我试过了:
@model IEnumerable<Foundation3SinglePageRWD.Models.Locations>
<div id="map" style="height: 800px; width: 100%"></div>
<script type="text/javascript">
$(function () {
var map = null;
var location = null;
function loadMap() {
// initialize the map
map = new Microsoft.Maps.Map(document.getElementById('map'), {
credentials: "My Bing Map Key",
enableSearchLogo: false
});
}
function showPosition(position) {
function showPosition(position) {
//display position
var location = position.coords;
map.setView({ zoom: 10, center: new Microsoft.Maps.Location(location.latitude, location.longitude) });
//var pushpinOptions = { icon: virtualPath + '/Content/images/foundation/orbit/Push.png' };
var test = '@model';
alert(test);
var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), null);
for (var i = 0; i < test.l.length; i++) {
map.entities.push(pushpin);
pushpin.setLocation(new Microsoft.Maps.Location(test.latitude, test.longitude));
};
}
}
function positionError(position) {
alert("Error getting position. Code: " + position.code);
}
loadMap();
navigator.geolocation.getCurrentPosition(showPosition, positionError);
});
</script>
控制器:
public ActionResult Index()
{
List<Models.Locations> loc = new List<Models.Locations>();
Models.Locations temp = new Models.Locations("55.473746", "8.447411");
loc.Add(temp);
Models.Locations temp1 = new Models.Locations("55.504991", "8.443698");
loc.Add(temp1);
Models.Locations temp2 = new Models.Locations("55.468283", "8.438");
loc.Add(temp2);
Models.Locations temp3 = new Models.Locations("55.498978", "8.40002");
loc.Add(temp3);
return View(loc);
}
最后是模型:
public class Locations
{
public string latitude { get; set; }
public string longitude { get; set; }
public List<Locations> Loca { get; set; }
public Locations(string latitude, string longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
}