【问题标题】:Asp.net how can I send a list from codebehind to javascript for displaying coordinates in googlemapAsp.net 如何将列表从代码隐藏发送到 javascript 以在 googlemap 中显示坐标
【发布时间】:2014-04-17 22:36:30
【问题描述】:

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

我有一个在我的后端调用 web 服务的按钮事件,它返回一个坐标列表(纬度/经度)。这很好用。我的问题是如何在我的 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;
           }
        }

我找到了如何在 googlemap 中使用标记显示多个位置的示例,但我不知道如何将列表 (listToReturn) 从后面的代码发送到 aspx 页面中的 javascript。我尝试使用以下隐藏字段、自动回发、会话,但似乎没有任何效果,也许我做错了什么。

<%--<!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>--%>

【问题讨论】:

  • 我能否获得更多关于如何解决此任务的想法。我是 asp.net 的新手,对前端(javascript...)没有太多经验

标签: javascript html asp.net google-maps


【解决方案1】:

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

这里有一个很好的例子。

http://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery

希望这会有所帮助。

【讨论】:

  • 我对 jquery/ajax 毫无头绪。这可能是我以后要看的东西
  • 那么我认为是时候开始了解它了,因为就 javascript 而言,它让生活变得更加简单。休息是你的电话。 :)
【解决方案2】:

我使用扩展方法:

        /// <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);
        }

这很容易从代码中调用,你甚至可以使用匿名类型:

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);

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

【讨论】:

  • 但是我必须注册的对象,是我的方法调用返回的列表吗?此列表包含字符串“code”的列表 public static void RegisterObjectAsVariable(this ClientScriptManager mgr, Type type, string listToReturn, object objectToEncode) { mgr.RegisterClientScriptBlock(type, string.Concat("ClientScriptManagerExtensions_", variableName), string.Concat( "var ", listToReturn, " = ", new JavaScriptSerializer().Serialize(objectToEncode), ";"), true); }
  • @user3058620 该对象是您想要放入 JS 变量中的任何内容。我建议了纬度/经度,然后您可以在 JS 中循环这些并创建 google.maps.LatLng() 对象
猜你喜欢
  • 2016-05-11
  • 2016-11-09
  • 1970-01-01
  • 2011-04-01
  • 2012-06-25
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多