调用百度API接口 正解析地址和逆解析

地址解析(结构化地址 解析得到 经纬度):

public void SaveLocation(DataRequest<Location> request, DataResponse<ResultModel> response)
       
          {
            var result = new ResultModel();
            try
            {
                var LongitudeAndLatitude = Config.GetConfig("LongitudeAndLatitude");
                string ak = LongitudeAndLatitude["ak"];
                //地址解析(结构化地址 解析得到 经纬度)
                //用户可通过该功能,将结构化地址(省/市/区/街道/门牌号)解析为对应的位置坐标。地址结构越完整,地址内容越准确,解析的坐标精度越高。 
                //使用示例一:http://api.map.baidu.com/geocoder/v2/address=结构化的地址&output=json&ak=你的密钥
                //API服务地址:http://api.map.baidu.com/geocoder/v2/?          //GET请求
                string ApiUrl = string.Format("http://api.map.baidu.com/geocoder/v2/?address={0}&output=json&ak={1}", request.ObjectData.ServiceAddress, ak);
                string httpResult = HttpHelper.Download(ApiUrl);
                dynamic jsonResult = JsonHelper.DeserializeObject<dynamic>(httpResult);
                if (jsonResult.status.ToString() == "0")
                {
                    decimal lng =decimal.Parse(jsonResult.result.location.lng);
                    decimal lat = decimal.Parse(jsonResult.result.location.lat);

                    Location Location = new Location
                    {
                        Longitude = lng,
                        Latitude = lat,
                        DriveTime = DateTime.Now,
                        ServiceType = "完成试驾用车服务",
                        ServiceAddress = request.ObjectData.ServiceAddress
                    };
                    SQLHelper.SaveEntry(Location, EntityState.Added);
                }
                result.Status = ResultStatus.Success;
                result.Message = "完成体验驾驶服务定位成功!";
            }
            catch (Exception ex)
            {
             
                result.Status =ResultStatus.Fail;
                result.Message = ex.Message;
                if (ex.InnerException != null)
                    result.Message = ex.InnerException.Message;
                
            }
            response.ObjectData = result;

        }

地理位置转换经纬度坐标 - 逆地址解析(经纬度 解析得到 结构化地址)

 public void SaveLongitudeAndLatitude(DataRequest<dynamic> request, DataResponse<ResultModel> response)
        {
            var result = new ResultModel();
            try
            {
                string ClientInfo = null;
                var httpContext = Common.CommonHelper.GetHttpContext();
                if (httpContext != null)
                {
                    if (httpContext.Request.Headers.ContainsKey("ClientInfo"))
                    {
                        ClientInfo = httpContext.Request.Headers["ClientInfo"];

                        var LongitudeAndLatitude = Config.GetConfig("LongitudeAndLatitude");
                        string ak = LongitudeAndLatitude["ak"];
                        dynamic jsonResult = JsonHelper.DeserializeObject<dynamic>(ClientInfo);
                        //API服务地址:http://api.map.baidu.com/geocoder/v2/?          //GET请求
                        //使用示例:http://api.map.baidu.com/geocoder/v2/?location=纬度,经度&output=xml&pois=1&ak=你的ak    [ !注意:location=纬度,经度 ,不要写反了 ]
                        string ApiUrl = string.Format("http://api.map.baidu.com/geocoder/v2/?location={0}&output=json&ak={1}",
                            jsonResult.Latitude + "," + jsonResult.Longitude, ak);

                        string httpResult = HttpHelper.Download(ApiUrl);
                        dynamic jsonRespone = JsonHelper.DeserializeObject<dynamic>(httpResult);

                        var address = string.Empty;
                        if (jsonRespone.status.ToString() == "0")
                        {
                          
                            string business = jsonRespone.result.business.ToString();
                            string formatted_address = ConvertHelper.GetString(jsonRespone.result.formatted_address);
                            string sematic_description = ConvertHelper.GetString(jsonRespone.result.sematic_description);

                            address = formatted_address + sematic_description;
  
                        }
                        else
                        {
                            address = "无法获取当前地理位置,调用服务异常...";
                        }
                       
                        Location Location = new Location
                        {
                        
                            Longitude = jsonResult.Longitude,
                            Latitude = jsonResult.Latitude,
                            DriveTime = DateTime.Now,
                            ServiceType = "开始体验驾驶服务",
                            ServiceAddress = address
                        };
                        SQLHelper.SaveEntry(Location, EntityState.Added);
                    }
                    result.Status = ResultStatus.Success;
                    result.Message = "记录开始驾驶定位信息!";
                }
            }
            catch (Exception  ex)
            {
                result.Status = ResultStatus.Fail;
                result.Message = ex.Message;
                if (ex.InnerException != null)
                    result.Message = ex.InnerException.Message;
                
            }
            response.ObjectData = result;
           
        }
经纬度获取地址 逆解析
Headers
  [{"key":"ClientInfo","value":"{\"Latitude\" :\"22.615589046911805\",\"Longitude\":\"114.03483089395202\"}","description":"","type":"text","enabled":true}]
{"Latitude" :"22.615589046911805","Longitude":"114.03483089395202"}
 
 
 

 

posted @ 2019-07-18 16:38  蜜雪粮液  阅读(4824)  评论(0编辑  收藏  举报