C# 高德地图调用帮助类 GaodeHelper
标签:end put oca des char ddr web .json for
///
/// 高德地图调用帮助类
/// 更多详情请参考 高德api
///
public class GaodeHelper
{
//高德平台申请的秘钥
public static string SecretKey = "申请的秘钥";
///
/// 获取经纬度
///
///
///
///
public static string GetGeocode(string address,string city)
{
string geocodeUrl = "http://restapi.amap.com/v3/geocode/geo?address={Address}&city={City}&output=json&key={SecretKey}"
.Replace("{SecretKey}", SecretKey)
.Replace("{Address}", address)
.Replace("{City}", city);
string geocode = WebClientDownloadInfoToString(geocodeUrl);
geocode = GetLatitudeAndLongitude(geocode);
return geocode;
}
///
/// 获取城市之间的距离
///
///
///
///
///
///
public static string GetDistance(string begin,string beginCity, string end, string endCity)
{
string origin = GetGeocode(begin,beginCity);
string destination = GetGeocode(end, endCity);
string driveUri = "http://restapi.amap.com/v3/direction/driving?key={SecretKey}&origin={Origin}&destination={Destination}"
.Replace("{SecretKey}", SecretKey)
.Replace("{Origin}", origin)
.Replace("{Destination}", destination);
string result= WebClientDownloadInfo(driveUri);
//var gd = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
return result;
}
private static string WebClientDownloadInfo(string uri)
{
string result = string.Empty;
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/xml;charset=UTF-8";
result = wc.DownloadString(uri);
}
return result;
}
///
/// 模拟请求
///
///
///
private static string WebClientDownloadInfoToString(string uri)
{
string result = string.Empty;
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/xml;charset=UTF-8";
result = wc.DownloadString(uri);
}
return result;
}
///
/// 解析返回的经纬度信息
///
///
///
private static string GetLatitudeAndLongitude(string GeocodeJsonFormat)
{
JObject o = JObject.Parse(GeocodeJsonFormat);
string geocodes = (string)o["geocodes"][0]["location"];
return geocodes;
}
}
C# 高德地图调用帮助类 GaodeHelper
标签:end put oca des char ddr web .json for
原文地址:https://www.cnblogs.com/benbenfishfish/p/9680479.html
评论