我如何从代码隐藏发送一个列表到javascript在googlemap中显示坐标

Asp.net how can I send a list from codebehind to javascript for displaying coordinates in googlemap

本文关键字:javascript 列表 一个 googlemap 坐标 显示 代码 隐藏      更新时间:2023-09-26

我目前是asp.net工作的新手。我希望使用googlemap在地图上显示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;
           }
        }

我发现了如何在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>--%>

我对googlemaps有点陌生,因为我从来没有在它上面工作过,但是如果你想在按钮单击上从代码返回一个列表到javascript,那么如何使jquery ajax调用并以json的形式返回列表?您可以在ajax呼叫的success部分使用此列表。

这里有一个很好的例子。

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

我使用了一个扩展方法:

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

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