地址解析(結構化地址 解析獲得 經緯度):git
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; }
地理位置轉換經緯度座標 - 逆地址解析(經緯度 解析獲得 結構化地址)json
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; }