我可以使用以下内容在我的地图上绘制点完全没有问题。但现在我想用折线路径连接点,但继续收到错误:“InvalidValueError:not an Array”。我的代码如下,非常感谢任何帮助。
//控制器动作
public async Task<IactionResult> Index()
{
var requests = _context.Complaints.ToList();
var locations = requests.Where(x=>x.ComplaintId>11830).ToList();
string markers = "[";
int i;
for (i = 0; i < locations.Count; i++)
{
markers += "{";
//markers += string.Format("'title': '{0}',",locations[i].ComplaintId);
markers += string.Format("lat: {0},locations[i].Lat);
markers += string.Format("lng: {0}",locations[i].Lon);
//markers += string.Format("'description': '{0}'",locations[i].ComplaintId);
markers += "},";
}
markers += "]";
ViewBag.markers = markers;
return View(requests.Take(50));
}
//索引视图
<script type="text/javascript">
var markers = @Html.Raw(ViewBag.markers);
window.onload = function () {
var mapoptions = {
center: new google.maps.LatLng(-43.1022,147.7485 ),zoom: 10,mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"),mapoptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
const myLatlng = new google.maps.LatLng(data.lat,data.lng);
const marker = new google.maps.Polyline({
path: myLatlng,map: map
//title: data.title
});
(function (marker,data) {
google.maps.event.addListener(marker,"click",function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map,marker);
});
})(marker,data);
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?#myKeyhidden#"></script>