c#封裝百度web辦事geocoding api 、百度坐標轉換示例。本站提示廣大學習愛好者:(c#封裝百度web辦事geocoding api 、百度坐標轉換示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#封裝百度web辦事geocoding api 、百度坐標轉換示例正文
1、創立基本參數類
public static class BaiduConstParams
{
public const string PlaceApIv2Search = "http://api.map.百度.com/place/v2/search";
public const string PlaceApIv2Detail = "http://api.map.百度.com/place/v2/detail";
public const string PlaceApIv2Eventsearch = "http://api.map.百度.com/place/v2/eventsearch";
public const string PlaceApIv2Eventdetail = "http://api.map.百度.com/place/v2/eventdetail";
public const string GeocodingApIv2 = "http://api.map.百度.com/geocoder/v2/";
public const string GeocodingApIv2Reverse = "http://api.map.百度.com/geocoder/v2/";
public const string TranslateApi = "http://openapi.百度.com/public/2.0/bmt/translate";
public const string GeoconvApi = "http://api.map.百度.com/geoconv/v1/";
}
public static class BaiduErrorMessages
{
public const string NotKey = "密鑰不存在";
public const string LackParam = "缺乏需要要求參數";
}
2、界說API毛病信息與產物信息
public enum BaiduLbsType
{
PlaceApIv2Search,
PlaceApIv2Detail,
PlaceApIv2Eventsearch,
PlaceApIv2Eventdetail,
GeocodingApIv2,
GeocodingApIv2Reverse,
Translate,
Geoconv
}
public enum Status
{
/// <summary>
/// 正常
/// </summary>
Ok = 0,
/// <summary>
/// 要求參數不法
/// </summary>
ParameterInvalid = 2,
/// <summary>
/// 權限校驗掉敗
/// </summary>
VerifyFailure = 3,
/// <summary>
/// 配額校驗掉敗
/// </summary>
QuotaFailure = 4,
/// <summary>
/// 不存在或許不法
/// </summary>
AkFailure = 5,
/// <summary>
/// Transform 外部毛病
/// </summary>
InternalError = 1,
/// <summary>
/// from不法
/// </summary>
FromIllegal = 21,
/// <summary>
/// to不法
/// </summary>
ToIllegal = 22,
/// <summary>
/// coords不法
/// </summary>
CoordsIllegal = 24,
/// <summary>
/// coords個數不法,跨越限制
/// </summary>
CoordsCountIllegal = 25
}
3、界說API成果前往實體映照類
public class BaiduGeocodingResults
{
/// <summary>
/// 前往成果狀況值, 勝利前往0,其他值請檢查附錄。
/// </summary>
[JsonProperty(PropertyName = "status")]
public Status Status;
/// <summary>
/// 前往成果狀況值, 勝利前往0,其他值請檢查附錄。
/// </summary>
[JsonProperty(PropertyName = "result")]
public BaiduGeocodingResult Result;
}
public class BaiduGeocodingResult
{
/// <summary>
/// 經緯度坐標
/// </summary>
[JsonProperty(PropertyName = "location")]
public BaiduGeocodingLoaction Location;
/// <summary>
/// 地位的附加信息,能否准確查找。1為准確查找,0為不准確。
/// </summary>
[JsonProperty(PropertyName = "precise")]
public int Precise;
/// <summary>
/// 可托度
/// </summary>
[JsonProperty(PropertyName = "confidence")]
public int Confidence;
/// <summary>
/// 地址類型
/// </summary>
[JsonProperty(PropertyName = "level")]
public string Level;
/// <summary>
/// 構造化地址信息
/// </summary>
[JsonProperty(PropertyName = "formatted_address")]
public string FormattedAddress;
/// <summary>
/// 地點商圈信息,如 "國民年夜學,中關村,姑蘇街"
/// </summary>
[JsonProperty(PropertyName = "business")]
public string Business;
/// <summary>
/// 詳細地址
/// </summary>
[JsonProperty(PropertyName = "addressComponent")]
public BaiduGeocodingAddress AddressComponent;
}
public class BaiduGeocodingLoaction
{
/// <summary>
/// 緯度值
/// </summary>
[JsonProperty(PropertyName = "lat")]
public decimal Lat;
/// <summary>
/// 經度值
/// </summary>
[JsonProperty(PropertyName = "lng")]
public decimal Lng;
}
public class BaiduGeocodingAddress
{
/// <summary>
/// 城市名
/// </summary>
[JsonProperty(PropertyName = "city")]
public string City;
/// <summary>
/// 區縣名
/// </summary>
[JsonProperty(PropertyName = "district")]
public string District;
/// <summary>
/// 省名
/// </summary>
[JsonProperty(PropertyName = "province")]
public string Province;
/// <summary>
/// 街道名
/// </summary>
[JsonProperty(PropertyName = "street")]
public string Street;
/// <summary>
/// 街道門商標
/// </summary>
[JsonProperty(PropertyName = "street_number")]
public string StreetNumber;
}
4、創立API通用途理類
public class BaiduLbs
{
private readonly string _key;
public static string CurrentRequest = "";
public BaiduLbs(string key)
{
_key = key;
}
/// <summary>
/// 要求
/// </summary>
/// <param name="param"></param>
/// <param name="百度LbsType"></param>
/// <param name="encoding"></param>
/// <param name="action"></param>
public void Request(string param, BaiduLbsType 百度LbsType, Encoding encoding, Action<string> action)
{
WebClient webClient = new WebClient { Encoding = encoding };
string url = "";
switch (百度LbsType)
{
case BaiduLbsType.PlaceApIv2Search:
url = string.Format(BaiduConstParams.PlaceApIv2Search + "?{0}", param);
break;
case BaiduLbsType.PlaceApIv2Detail:
url = string.Format(BaiduConstParams.PlaceApIv2Detail + "?{0}", param);
break;
case BaiduLbsType.PlaceApIv2Eventsearch:
url = string.Format(BaiduConstParams.PlaceApIv2Eventsearch + "?{0}", param);
break;
case BaiduLbsType.PlaceApIv2Eventdetail:
url = string.Format(BaiduConstParams.PlaceApIv2Eventdetail + "?{0}", param);
break;
case BaiduLbsType.GeocodingApIv2:
case BaiduLbsType.GeocodingApIv2Reverse:
url = string.Format(BaiduConstParams.GeocodingApIv2 + "?{0}", param);
break;
case BaiduLbsType.Translate:
url = string.Format(BaiduConstParams.TranslateApi + "?{0}", param);
break;
case BaiduLbsType.Geoconv:
url = string.Format(BaiduConstParams.GeoconvApi + "?{0}", param);
break;
}
CurrentRequest = url;
action(webClient.DownloadString(url));
}
/// <summary>
/// 呼應
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="param"></param>
/// <param name="百度LbsType"></param>
/// <param name="encoding"></param>
public T Response<T>(string param, BaiduLbsType 百度LbsType, Encoding encoding)
{
T t = default(T);
Request(param, 百度LbsType, encoding, json =>
{
if (百度LbsType == BaiduLbsType.GeocodingApIv2 || 百度LbsType == BaiduLbsType.GeocodingApIv2Reverse)
{
if (json.Contains("\"result\":[]"))
{
json = json.WordStr("\"result\":[]", "\"result\":{}");
}
}
t = (T)JsonConvert.DeserializeObject(json, typeof(T));
});
return t;
}
public BaiduGeocodingResults BaiduGeocoding(string address, string city)
{
address = System.Web.HttpUtility.UrlEncode(address);
city = System.Web.HttpUtility.UrlEncode(city);
string request = string.Format("address={0}&output=json&ak={1}&city={2}", address, _key, city);
var result = Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
if (result.Status == Status.Ok && result.Result.Location == null)
{
request = string.Format("address={0}&output=json&ak={1}&city={2}", city + address, _key, city);
return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
}
return result;
}
public BaiduGeocodingResults BaiduGeocoding(string longitude, string dimensions, string pois)
{
var location = longitude + "," + dimensions;
string request = string.Format("ak={0}&location={1}&pois={2}", _key, location, pois);
return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
}
public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, ref List<GeoconvPOI> geoconvPois)
{
geoconvParams.Ak = _key;
return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
}
public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, GeoconvPOI geoconvPoi)
{
geoconvParams.Ak = _key;
List<GeoconvPOI> geoconvPois = new List<GeoconvPOI>
{
geoconvPoi
};
return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
}
}