2017年3月30日 星期四

利用PageMethod,從JS執行CodeBehind函式

1.必要條件:
        a.加入ScriptManager控制項,EnablePageMethods屬性設為True
        b.此函式必須加上[System.Web.Services.WebMethod]
        c.必須宣告為靜態 Static
        d.JS使用PageMethods物件呼叫伺服器端函式。

2.範例
  2.1 ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
2.2 CodeBehind:
      [System.Web.Services.WebMethod]
        public static string GetServerTime(string format)
        {
            return DateTime.Now.ToString(format);
        }
2.3 JS:
     //呼叫伺服端函式
     PageMethods.GetServerTime("yyyy/MM/dd HH:mm:ss", onSuccess, onError);
   
     //成功時取出內容  
     function onSuccess(Data) {
        var data = Data;
    }
 
     / /失敗時彈出失敗訊息
    function onError(error) {
        if (error != null)
            alert(error);
    }

依經緯度取得地址

1.在CodeBehind建立命名空間,for Google map api轉出的JSON資料之用途

namespace GoogleGeocodingAPI
{
    public class AddressComponent
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public List<string> types { get; set; }
    }
    public class Location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }
    public class Northeast
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    public class Southwest
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    public class Viewport
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }
    public class Geometry
    {
        public Location location { get; set; }
        public string location_type { get; set; }
        public Viewport viewport { get; set; }
    }
    public class Result
    {
        public List<AddressComponent> address_components { get; set; }
        public string formatted_address { get; set; }
        public Geometry geometry { get; set; }
        public bool partial_match { get; set; }
        public List<string> types { get; set; }
    }
    public class RootObject
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }
}

2. 建立函式
        //=======================================================
        //<aummary>緯度經度轉中文地址</summary>
        //<param name="latLng"></param>
        //=======================================================
        private static string latLngToChineseAddress(params string[] latLng)
        {
            string url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + string.Join(",", latLng) + "&language=zh-TW&sensor=true";
            string json = string.Empty;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            using (var response = request.GetResponse())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    json = sr.ReadToEnd();
                }
            }
            GoogleGeocodingAPI.RootObject rootObj = JsonConvert.DeserializeObject<GoogleGeocodingAPI.RootObject>(json);
            return rootObj.results[0].formatted_address;
        }

3.呼叫轉換函示
private static string GetAddress(string lat,string lng)
        {
            string result = string.Empty;
            result = latLngToChineseAddress(lat, lng);
            return result;
           
        }