• 百度地图API-根据地址解析坐标,根据坐标解析地址


    一.根据地址解析坐标

    1.官方文档地址

     http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

    2.c#封装方法 

            /// <summary> 

            /// 根据地址解析经纬度 

            /// </summary> 

            /// <param name="address">地址</param> 

            /// <param name="city">城市</param>

            /// <returns></returns> 

      public static string GetMapLatOrLong(string address, string city)

             {

                string posturl = "http://api.map.baidu.com/geocoder?address=" + address + "&output=json&key=自己的key&city=" + city + ""; 

                string postret = HttpGet(posturl);           

                //返回json数据:{ "status":"OK","result":{ "location":{ "lng":经度,"lat":纬度 },"precise":1,"confidence":75,"level":"u5546u52a1u5927u53a6"} } 

                string citylist = "";

                       JsonData jd = JsonMapper.ToObject(postret); 

                if (jd["status"].ToString() == "OK") 

                {                

                    citylist = "{"lng":""+ jd["result"]["location"]["lng"].ToString() 

                        + "","lat":""+ jd["result"]["location"]["lat"].ToString() 

                        + "","level":""+ jd["result"]["level"].ToString() + ""}"; 

                }

                else

                {

                    citylist = "{"lng":"","lat":"","level":""}"; 

                }

                return citylist;

            } 

    3.调用 

      string retcity= GetMapLatOrLong("地址","城市");

       Response.Write(retcity);   

    二.根据坐标解析地址 

    1.官方文档地址 

    http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad

     2.c#封装方法 

            /// <summary>

            /// 按照定位的坐标,获取省市县

            /// </summary>

            /// <param name="lat"></param>

            /// <param name="lng"></param>

            /// <returns></returns>

            public static string DecodeMapPostion(string lat, string lng)

            {

                string posturl = "http://api.map.baidu.com/geocoder/v2/?output=json&ak=自己的key&location=" + lat + "," + lng;

                string postret =HttpGet(posturl);

                string citylist = "";

                JsonData jd = JsonMapper.ToObject(postret);

                if (jd["status"].ToString() == "0")

                {

                    //具体地址

                    string jtaddrs = jd["result"]["formatted_address"].ToString();

                    string bussinsaddrs = jd["result"]["sematic_description"].ToString();

                    citylist = "{"province":"" + jd["result"]["addressComponent"]["province"].ToString() + "","city":""

                        + jd["result"]["addressComponent"]["city"].ToString() +

                        "","area":"" + jd["result"]["addressComponent"]["district"].ToString() + "","address":""

                        + HtmlEncode(jtaddrs) + ""}";

                }

                else

                {

                    citylist = "{"province":"","city":"","area":""}";

                }

                return citylist;

            } 

    3.调用  

      string retcity= DecodeMapPostion(lat,lng); 

       Response.Write(retcity); 

     

     

  • 相关阅读:
    使用greenDAO遇到的问题
    使用greenDAO生成DAO代码
    Spring中Bean的生命周期
    视频弹幕开源库
    最简MacOs10.8安装
    apache-virtual host
    带删除的EditText
    替换默认debug.keystore文件
    Intellij格式化java和xml
    【数据结构】之二叉树的java实现
  • 原文地址:https://www.cnblogs.com/dsjbk/p/12659055.html
Copyright © 2020-2023  润新知