Asp.net如何将一个列表从codebehind发送到javascript,以便在googlemap中显示坐标

Asp.net如何将一个列表从codebehind发送到javascript,以便在googlemap中显示坐标,javascript,html,asp.net,google-maps,Javascript,Html,Asp.net,Google Maps,我目前是asp.net的新手。我希望使用谷歌地图在地图上显示gps位置列表 我有一个按钮事件,它在我的后端调用一个webservice,它返回一个坐标列表(纬度/经度)。这个很好用。我的问题是如何在我的aspx页面中将此列表发送到javascript protected void btnGetMapForAdr_Click(object sender, EventArgs e) { List<MultipleAddress> listofadd

我目前是asp.net的新手。我希望使用谷歌地图在地图上显示gps位置列表

我有一个按钮事件,它在我的后端调用一个webservice,它返回一个坐标列表(纬度/经度)。这个很好用。我的问题是如何在我的aspx页面中将此列表发送到javascript

protected void btnGetMapForAdr_Click(object sender, EventArgs e)
        {
            List<MultipleAddress> listofaddr;
            MultipleAddress multiAddr1;
            MultipleAddress multiAddr2;
            MultipleAddress multiAddr3;
            ...

            listofaddr = new List<MultipleAddress>()
            {
                multiAddr1,
                multiAddr2,
                multiAddr3
            };


            //Class1 is a library that contains the webservice method that returns the coordinates.

            Class1 service = new Class1();
            Dictionary<string, LongitudeLatitude> returnAdrListDict = service.GetMultipleLongLat(listofaddr);

            List<LongitudeLatitude> newListForASPX = CreateNewList(returnAdrListDict);

            List<string> listForJavascript = ConvertToListOfStrings(newListForASPX);

        }
        //convert dictionary to type LongitudeLatitude
            private List<LongitudeLatitude> CreateNewList(Dictionary<string, LongitudeLatitude> input){
            List<LongitudeLatitude> longlatListCollection = new List<LongitudeLatitude>();

            foreach (KeyValuePair<string, LongitudeLatitude> item in input)
            {
                LongitudeLatitude adrlonglat = new LongitudeLatitude();
                adrlonglat.AdressInfo = item.Key;
                adrlonglat.Latitude = item.Value.Latitude;
                adrlonglat.Longitude = item.Value.Longitude;

                longlatListCollection.Add(adrlonglat);

            }
                 return longlatListCollection;
            }

        //convert list of type LongitudeLatitude to a list of type string
           private List<string> ConvertToListOfStrings(List<LongitudeLatitude> input)
           {
               List<string> listToReturn = new List<string>(); 
               foreach (var item in input)
               {
                   listToReturn.Add(item.AdressInfo.ToString() + ":" + item.Latitude.ToString() + "," + item.Longitude.ToString());
               }
               return listToReturn;
           }
        }
protectedvoid btnGetMapForAdr\u单击(对象发送方,事件参数e)
{
地址列表;
多线程多地址1;
多线程多地址2;
多地址多地址3;
...
listofaddr=新列表()
{
多地址1,
多地址2,
多地址3
};
//Class1是一个包含返回坐标的webservice方法的库。
Class1服务=新的Class1();
Dictionary returnAdrListDict=service.GetMultipleLongLat(listofaddr);
List newListForASPX=CreateNewList(returnAdrListDict);
List listForJavascript=ConvertToListOfStrings(newListForASPX);
}
//将字典转换为纵向相关类型
私有列表CreateNewList(字典输入){
List longlatListCollection=新列表();
foreach(输入中的KeyValuePair项)
{
纵向相关性adrlonglat=新的纵向相关性();
adrlonglat.adresinfo=item.Key;
adrlonglat.Latitude=item.Value.Latitude;
adrlonglat.Longitude=item.Value.Longitude;
添加(adrlonglat);
}
返回longlatListCollection;
}
//将“纵向相关性”类型的列表转换为“字符串”类型的列表
私有列表转换列表字符串(列表输入)
{
List LISTORETURN=新列表();
foreach(输入中的变量项)
{
添加(item.adresinfo.ToString()+“:“+item.Latitude.ToString()+”,“+item.Longitude.ToString()”);
}
返回列表返回;
}
}
我在Google地图中找到了这个如何用标记显示多个位置的示例,但我不知道如何将列表(listToReturn)从代码隐藏发送到aspx页面中的javascript。我曾尝试使用以下hiddenfield、autopostback和session,但似乎没有任何效果,可能是我做错了什么

<%--<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
      var locations = [
        ['Bondi Beach', -33.890542, 151.274856, 4],
        ['Coogee Beach', -33.923036, 151.259052, 5],
        ['Cronulla Beach', -34.028249, 151.157507, 3],
        ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
        ['Maroubra Beach', -33.950198, 151.259302, 1]
      ];

      var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 10,
          center: new google.maps.LatLng(-33.92, 151.25),
          mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      var infowindow = new google.maps.InfoWindow();

      var marker, i;

      for (i = 0; i < locations.length; i++) {
          marker = new google.maps.Marker({
              position: new google.maps.LatLng(locations[i][1], locations[i][2]),
              map: map
          });

          google.maps.event.addListener(marker, 'click', (function (marker, i) {
              return function () {
                  infowindow.setContent(locations[i][0]);
                  infowindow.open(map, marker);
              }
          })(marker, i));
      }
  </script>
</body>
</html>--%>

我使用扩展方法:

        /// <summary>
        /// Registers an object as a variable on the page
        /// </summary>
        public static void RegisterObjectAsVariable(this ClientScriptManager mgr, Type type, string variableName, object objectToEncode)
        {
            mgr.RegisterClientScriptBlock(type,
                string.Concat("ClientScriptManagerExtensions_", variableName),
                string.Concat("var ", variableName, " = ", new JavaScriptSerializer().Serialize(objectToEncode), ";"),
                true);
        }

这将在页面上创建一个名为locations的javascript对象供您访问。

我对googlemaps有点陌生,因为我从来没有使用过它,但是如果您想在按钮上将列表从代码返回到javascript,请单击按钮,然后进行
jquery ajax
调用并以
json
的形式返回列表如何?您可以在
ajax
调用的
success
部分使用此列表

这里有一个很好的例子


希望这能有所帮助。

我对jquery/ajax一无所知。这可能是我以后必须要看的东西,然后我认为是时候开始了解它了,因为它使javascript的使用变得更加简单。休息由你决定。:)但是我必须注册的对象,是我的方法调用返回的列表吗?此列表包含字符串的列表“代码”公共静态无效RegisterObjectAsVariable(此ClientScriptManager mgr,类型类型,字符串listToReturn,对象objectToEncode){mgr.RegisterClientScriptBlock(类型,string.Concat(“ClientScriptManagerExtrances”,variableName),string.Concat(“var”,listToReturn,“=”,new JavaScriptSerializer().Serialize(objectToEncode),“;”,true);}@user3058620对象是您想要放入JS变量中的任何内容。我建议使用纬度/经度,然后您可以在JS中循环这些内容并创建google.maps.LatLng()objectsCan我很乐意得到更多关于如何解决此任务的想法。我是asp.net新手,对前端(javascript…)没有太多经验
var locations = new[]
{
  new {latitude = 1, longitude = 1},
  new {latitude = 2, longitude = 2},
  new {latitude = 3, longitude = 3},
};

this.Page.ClientScript.RegisterObjectAsVariable(typeof(MyPage), "locations", locations);