• 如何通过地址转换为WGS经纬度


    原理如下:

    1.我们需要创建两张表  SourceTable和LonLatTable

    其中SourceTable作为需要转换的地址存储信息LonLatTable为转换后的地址存储信息

    这里可以不需要LonLatTable直接修改SourceTable其实也可以,但是为了方便(insert比update方便的多)所以就直接新建了LonLatTable这张表

    字段值如下:

    public int Id { get; set; }
    public string TEL { get; set; }
    public string Address { get; set; }
    public string Lon { get; set; }
    public string Lat { get; set; }
    public string ReportTime { get; set; }
    public string Area { get; set; }
    public string MobileType { get; set; }

    2.我们需要根据地址转换为经纬度,这里用到了百度(百度坐标)和高德(火星坐标)两种转换方式,百度转换是带置信度的,高德的不带置信度,当百度转换时置信度(Precise)为1时,则为准确转换这里我们一百度为准,当百度转换是置信度不为1或者无法转换是时我们在用高德进行转换

    代码如下

     public partial class AddressToLongitudeLatitude : Form
        {
            public JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            public List<SourceTable> import = new List<SourceTable>();
            public AddressToLongitudeLatitude()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                List<SourceTable> address = GetAddress();
    
                int count = 0;
                foreach (var item in address)
                {
                    string strURL = "http://api.map.baidu.com/geocoder?address=" + item.Address + "&output=json&key=你的api&city=";
    
                    count++;
                    string info = GaoDeAnalysis(strURL);
    
                    if (info != "" && info.Contains("location"))
                    {
                        //执行反序列化
                        ShowLocation _Personnel = jsonSerializer.Deserialize<ShowLocation>(info);
    
                        if ((_Personnel != null) && (Convert.ToInt32(_Personnel.status) <= 1) && (_Personnel.result.precise == "1"))
                        {
                            SourceTable toTonLat = new SourceTable();
                            toTonLat.Address = item.Address;
                            //这里是将百度坐标转换为wgs坐标
                            string strLocation = GaoDeAnalysis("http://api.zdoz.net/bd2wgs.aspx?lat=" + _Personnel.result.location.lat + "&lng=" + _Personnel.result.location.lng);
    
                            location xy = jsonSerializer.Deserialize<location>(strLocation);
    
                            toTonLat.Lon = xy.lng.ToString();
                            toTonLat.Lat = xy.lat.ToString();
                            toTonLat.TEL = item.TEL;
                            //toTonLat.Precise = 1;
                            toTonLat.Area = item.Area;
                            toTonLat.ReportTime = item.ReportTime;
                            toTonLat.MobileType = item.MobileType;
                            import.Add(toTonLat);
                        }
                        else
                        {
                            getGDAddress(item.Address, item.TEL, item.Area, item.ReportTime, item.MobileType);
                        }
                    }
                    else
                    {
                        getGDAddress(item.Address, item.TEL, item.Area, item.ReportTime, item.MobileType);
                    }
    
                    if (count % 100 == 0)
                    {
                        Insert();
    
                        import.Clear();
                        import = null;
                        import = new List<SourceTable>();
                    }
                }
                Insert();
                MessageBox.Show("成功");
            }
            public static string GaoDeAnalysis(string url)
            {
                string strResult = "";
    
                try
                {
                    HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                    req.ContentType = "multipart/form-data";
                    req.Accept = "*/*";
                    //req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
                    req.UserAgent = "";
                    req.Timeout = 30000;//30秒连接不成功就中断 
                    req.Method = "GET";
                    req.KeepAlive = true;
    
                    HttpWebResponse response = req.GetResponse() as HttpWebResponse;
                    using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        strResult = sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    strResult = "";
                }
                return strResult;
            }
    
            public void getGDAddress(string item, string TEL, string Area, string ReportTime, string MobileType)
            {
                string strURLGD = "https://restapi.amap.com/v3/geocode/geo?key=高德key值&address=" + item + "&city=";
    
                JavaScriptSerializer jsonSerializerGD = new JavaScriptSerializer();
    
                string infoGD = GaoDeAnalysis(strURLGD);
    
                if (infoGD != "" && infoGD.Contains("location"))
                {
                    infoGD = infoGD.Replace("[]", """");
                    infoGD = infoGD.Replace("[", "");
                    infoGD = infoGD.Replace("]", "");
    
                    //执行反序列化
                    GDInfo _PersonnelGD = jsonSerializer.Deserialize<GDInfo>(infoGD);
                    if (_PersonnelGD != null)
                    {
                        SourceTable toTonLat = new SourceTable();
                        toTonLat.Address = item;
                        toTonLat.TEL = TEL;
                        string locat = _PersonnelGD.geocodes.location;
                        string[] lonlat = locat.Split(',');
                        if (lonlat.Length == 2)
                        {
    //这里是将火星坐标转换为WGS坐标
    string strLocation = GaoDeAnalysis("http://api.zdoz.net/gcj2wgs.aspx?lat=" + lonlat[1] + "&lng=" + lonlat[0]); location xy = jsonSerializer.Deserialize<location>(strLocation); toTonLat.Lon = xy.lng.ToString(); toTonLat.Lat = xy.lat.ToString(); toTonLat.MobileType = MobileType; toTonLat.Area = Area; toTonLat.ReportTime = ReportTime; import.Add(toTonLat); //toTonLat..ToString(); } } } } public List<SourceTable> GetAddress() { List<SourceTable> source = new List<SourceTable>(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandText = "select * from SourceTable order by TEL"; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 10 * 60 * 60; using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { SourceTable tab = new SourceTable(); tab.Id = Convert.ToInt32(sdr["Id"]); tab.TEL = sdr["TEL"].ToString(); tab.Address = sdr["Address"].ToString(); tab.ReportTime = sdr["ReportTime"].ToString(); tab.MobileType = sdr["MobileType"].ToString(); tab.Area = sdr["Area"].ToString(); source.Add(tab); } } } conn.Close(); } return source; } public void Insert() { string sqlText = ""; foreach (var item in import) { sqlText += "insert into LonLatTable(TEL,Address,Lon,LAT,ReportTime,MobileType,Area) values('" + item.TEL + "','" + item.Address + "','" + item.Lon + "','" + item.Lat + "','" + item.ReportTime + "','" + item.MobileType + "','" + item.Area + "');"; } using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandText = sqlText; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 10 * 60 * 60; object obj = cmd.ExecuteNonQuery(); } conn.Close(); } } public class SourceTable { public int Id { get; set; } public string TEL { get; set; } public string Address { get; set; } public string Lon { get; set; } public string Lat { get; set; } public string ReportTime { get; set; } public string Area { get; set; } public string MobileType { get; set; } } public class ShowLocation { public string status { get; set; } public result result { get; set; } } public class result { public location location { get; set; } public string precise { get; set; } } public class location { public string lng { get; set; } public string lat { get; set; } } public class GDInfo { public string status { get; set; } public string info { get; set; } public string infocode { get; set; } public string count { get; set; } public geocodes geocodes { get; set; } } public class geocodes { public string location { get; set; } } }
  • 相关阅读:
    启动另外一个activity,并返回结果
    MySQL5.0版本的安装图解
    android 通过Eclipse进行数字签名
    asp.net 未能写入输出文件--“拒绝访问的解决办法
    NeatUpload——支持大文件上传的控件
    ImageView的缩放模式
    开启手机LogCat
    得到Access数据库中的所有表名
    如何保护Excel工作表,不被人修改或删除指定区域
    Excel实现下拉列表选择
  • 原文地址:https://www.cnblogs.com/dushaojun/p/7986158.html
Copyright © 2020-2023  润新知