我有一个包含标题、纬度、经度和描述的对象列表,并希望将它们放置在谷歌地图中,但我无法将图钉放置在地图中。知道我做错了什么吗?
先感谢您!:)
foreach (var item in list)
{
markers += "{";
markers += string.Format("'title':'{0}',", item.Name);
markers += string.Format("'lat':'{0}',", item.Latitude);
markers += string.Format("'lng':'{0}',", item.Longitude);
markers += string.Format("'description':'{0}',", item.Description);
markers += "},";
}
markers += "];";
ViewBag.Markers = markers;
return View();
和 .cshtml 代码
function InitializeMap() {
var latlng = new google.maps.LatLng(24.4324, 26.83333);
var myOptions = {
zoom: 11,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
var markers = @Html.Raw(ViewBag.Markers);
for (i = 0; i < markers.length; i++) {
var data = markers[i];
var myLatLng = { data.lat, data.lng };
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
Title: data.title
});
}
}