我正在取得很好的进展使用谷歌地图。
下面的示例使用硬编码的JSON数组创建注释
// You can make markers different colors... google it up!
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')
// a sample list of JSON encoded data of places to visit in Liverpool, UK
// you can either make up a JSON list server side, or call it from a controller using JSONResult
var data = [
{ "Id": 1, "PlaceName": "Liverpool Museum", "OpeningHours":"9-5, M-F","GeoLong": "53.410146", "GeoLat": "-2.979919" },
{ "Id": 2, "PlaceName": "Merseyside Maritime Museum ", "OpeningHours": "9-1,2-5, M-F", "GeoLong": "53.401217", "GeoLat": "-2.993052" },
{ "Id": 3, "PlaceName": "Walker Art Gallery", "OpeningHours": "9-7, M-F", "GeoLong": "53.409839", "GeoLat": "-2.979447" },
{ "Id": 4, "PlaceName": "National Conservation Centre", "OpeningHours": "10-6, M-F", "GeoLong": "53.407511", "GeoLat": "-2.984683" }
];
// Using the JQuery "each" selector to iterate through the JSON list and drop marker pins
$.each(data, function (i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(item.GeoLong, item.GeoLat),
'map': map,
'title': item.PlaceName
});不过,我对MVC还是新手。请有人澄清评论中提到的两种方法?我有C#对象列表中的所有点。此列表位于页面的模型对象中。AlertsDashboardModel,它包含一个列表-警报
我可能会直接在模型中使用列表,但我想知道另一种方法是如何工作的。
我在Razor中使用MVC 5
保罗
发布于 2015-01-08 01:45:56
第二种方法是在页面加载(或由于用户在页面上的交互(如按下按钮)后加载数据),然后将ajax请求发送给返回数据的服务器:
客户端:
$(function () {
//fetch data points from server on page load
$.get("/YourController/GetDataPoints", function(data) {
$.each(data, function (i, item) {
//rest omitted
});
}服务器:(在Controller中)
public JsonResult GetDataPoints()
{
return new JsonResult { Data = ...your list here };
}更新
如果您只想将列表作为javascript嵌入服务器端的第一种方法,请在您的剃须刀视图中这样做:
<script>
var data = @Html.Raw(JsonConvert.SerializeObject(Model.DataPoints));
</script>关于安全,您需要确保列表中的所有内容都来自受信任的源,并且没有未经验证的字符串可以注入到页面中(如果数据点类只包含int,您就可以了)
https://stackoverflow.com/questions/27831646
复制相似问题