时间:2021-05-20
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);
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这是百度地图的摩卡托坐标与火星坐标的相互转换方法,大家参考使用吧复制代码代码如下:/***百度摩卡拖坐标与火星坐标的加密解密算法*@authorXFan**/p
如何能够获得百度地图上某个位置的坐标?有一种最简单的方法。百度地图上的坐标很容易获得,因为百度提供了一个“百度地图拾取坐标系统”,只要登
问:如何能够获得百度地图上某个位置的坐标?要最简单的方法。小威:百度地图上的坐标很容易获得,因为百度提供了一个“百度地图拾取坐标系统”,
百度搜索手机客户端版是百度推出的一款方便手机用户随时随地使用百度搜索服务的应用。依托百度网页、百度图片、百度新闻、百度知道、百度百科、百度地图、百度音乐、百度视
最近做了个项目,其中项目中有个需求需要用到百度地图进行导航,通过查阅相关资料参考百度地图api完成了一个例子。示例一:API地址:http://develope