在Google地图上创建多个标记点在C#对象的Web应用程序中

问题描述 投票:2回答:1

我有一个带有C#代码的Web应用程序,它使用long和lat创建一个对象列表。我想在Google地图上使用Javascript为他们创建标记点。我目前有这个代码:

  var marker;
  for (var i = 0; i < ('<%= Buses.Count %>' - 1); i++) {
       marker = new google.maps.Marker({
       position: new google.maps.LatLng('<%= Buses['i'].latitude %>', '<%= Buses['i'].longitude  %>'),
        map: map
       });
  }

哪个不起作用,我相信错误是在哪里我引用“i”来说明要使用的列表中的哪个对象。

如果我手动设置没有for循环的标记点并将['i']更改为[0],则会将标记设置为罚款。但是,由于项目数量的变化而且达到100,我需要使用for循环。我试过删除''周围的',这会产生一个错误说

“在目前的情况下,我不存在。”

我看过这些帖子:Add two marker points in google maps

Google Maps JS API v3 - Simple Multiple Marker Example

Google Maps JS API v3 - Simple Multiple Marker Example

但是,所有这些都在JavaScript中使用位置。我的对象作为自定义对象包含在C#代码中,定义为:

public static List<Bus> Buses = new List<Bus>();

这是我的C#代码:https://pastebin.com/2M1hWjnM

javascript c# asp.net google-maps webforms
1个回答
1
投票

你正在混淆javascript for code in behind / C#中的数据。它不起作用。首先,您要将标记数据放在javascript数组上。然后在没有webforms标签<%%>的情况下进行操作

把这样的东西放在你的页面代码后面(C#)

public static string getBusesJson()
    {
        var buses = new List<Bus>();

        buses.Add(new Bus() {latitude = 10, longitude = 20 });
        buses.Add(new Bus() { latitude = 15, longitude = 30 });
        buses.Add(new Bus() { latitude = 5, longitude = 40 });

        return JsonConvert.SerializeObject(buses);

    }

把它放在你的aspx标题上:

<script type="text/javascript">

    var points = <%= WebApplication3.WebForm1.getBusesJson()%>;

    for (var i = 0; i < points.length; i++) {
        //pass points[i].latitude and points[i].longitude to google maps
    }

</script>
© www.soinside.com 2019 - 2024. All rights reserved.