简体   繁体   English

如何使用C#.net从数据库获取经度

[英]How to get lat long from database using c#.net

  1. My ajax call looks like this . 我的ajax调用看起来像这样。

$(document).ready(function () { $(document).ready(function(){

$.ajax({
    type: "POST",
    url: "map.aspx/getCityPopulation2",
    //data: jsonData,
    contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: function (data) {

            var points = L.geoJson(data, {
                pointToLayer: function (feature, latlng) {
                    marker = L.marker(latlng, { icon: ratIcon })
                    marker.bindPopup(feature.properties.Source + '<br/>');
                    return marker;
                }
            }).addTo(pointsCluster);

            mymap.addLayer(pointsCluster);
        }
        });

2. Here is my code behind from where query is executing. 2.这是我执行查询后的代码。

 [WebMethod]
public static List<cityPopulation2> getCityPopulation2()
{
    List<cityPopulation2> p = new List<cityPopulation2>();

    using (NpgsqlConnection con = new NpgsqlConnection("Server=Localhost;Port=5432;User Id=postgres;Password=postgres;Database=post;"))
    {
        string myQuery = "select complaintlgeolon,complaintlgeolat from mandapet.pgr_demo_view";
        NpgsqlCommand cmd = new NpgsqlCommand();
        cmd.CommandText = myQuery;
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        NpgsqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                cityPopulation2 cpData = new cityPopulation2();
                cpData.lat = Convert.ToInt32(dr["complaintlgeolon"]);
                cpData.lng = Convert.ToInt32(dr["complaintlgeolon"]);

                p.Add(cpData);
            }
        }
    }

    return p;
}

} }

 public class cityPopulation2
    {
        public int lat { get; set; }
        public int lng { get; set; }
       // public string id { get; set; }
    }

3.Please any one guide me where i'm going wrong. 3.请任何人指导我我要去哪里错了。 i need to fetch data as j son and plot Markers 我需要以儿子的身份获取数据并绘制标记

As per your comments if you what to return json..You can use JavaScriptSerializer 根据您的评论(如果要返回json)。可以使用JavaScriptSerializer

 JavaScriptSerializer ASerializer = new JavaScriptSerializer();

  //you can create your own custom converter of cityPopulation2
  ASerializer.RegisterConverters(new JavaScriptConverter[] {new cityPopulation2()});
var Json = ASerializer.Serialize(p);
return Json;

Also make sure your DB values for lag and long are int 还要确保您的lag and long数据库值是int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM