1、创建基础参数类
复制代码 代码如下:
public static class BaiduConstParams
{
public const string PlaceApIv2Search = "http://api.map.baidu.com/place/v2/search";
public const string PlaceApIv2Detail = "http://api.map.baidu.com/place/v2/detail";
public const string PlaceApIv2Eventsearch = "http://api.map.baidu.com/place/v2/eventsearch";
public const string PlaceApIv2Eventdetail = "http://api.map.baidu.com/place/v2/eventdetail";
public const string GeocodingApIv2 = "http://api.map.baidu.com/geocoder/v2/";
public const string GeocodingApIv2Reverse = "http://api.map.baidu.com/geocoder/v2/";
public const string TranslateApi = "http://openapi.baidu.com/public/2.0/bmt/translate";
public const string GeoconvApi = "http://api.map.baidu.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="baiduLbsType"></param>
/// <param name="encoding"></param>
/// <param name="action"></param>
public void Request(string param, BaiduLbsType baiduLbsType, Encoding encoding, Action<string> action)
{
WebClient webClient = new WebClient { Encoding = encoding };
string url = "";
switch (baiduLbsType)
{
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="baiduLbsType"></param>
/// <param name="encoding"></param>
public T Response<T>(string param, BaiduLbsType baiduLbsType, Encoding encoding)
{
T t = default(T);
Request(param, baiduLbsType, encoding, json =>
{
if (baiduLbsType == BaiduLbsType.GeocodingApIv2 || baiduLbsType == BaiduLbsType.GeocodingApIv2Reverse)
{
if (json.Contains(""result":[]"))
{
json = json.Replace(""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);
}
}