C# 高德地图WebApi对接示例

2023-10-31 09:35:21 浏览数 (1)

1、登录或注册高德地图开放平台然后申请应用key(需要认证个人或企业开发者)

高德开放平台 | 高德地图API (amap.com)

2、创建新应用,为新应用添加key,完成第一项后即可看到key管理

3、具体的开发文档、接口入参出参以及结果示例等详见高德地图开放平台官网

地理/逆地理编码-API文档-开发指南-Web服务 API | 高德地图API (amap.com)

4、直接书写具体示例(以区域查询为例)

开发语言:C#

开发工具:visual studio 2019

开发项目类型:控制台程序

代码语言:c#复制
/// <summary>
/// 行政区域查询 - 高德地图
/// </summary>
/// <param name="args">请求入参</param>
/// <returns>行政区域返回值</returns>
private static string GetRegionByURL(string parameters) 
{
	string strResult = "";
	string url = string.Format("https://restapi.amap.com/v3/config/district?{0}", parameters);
	try 
	{
		HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
		req.ContentType = "multipart/form-data";
		req.Accept = "*/*";
		//req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
		req.UserAgent = "";
		req.Timeout = 30000; // 30秒连接不成功就中断 
		req.Method = "GET";
		req.KeepAlive = true;
		HttpWebResponse response = req.GetResponse() as HttpWebResponse;
		using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 
		{
			strResult = sr.ReadToEnd();
		}
	}
	catch (Exception ex) 
	{
		strResult = "";
		Console.WriteLine("异常信息提示:"   ex.Message);
		Console.ReadLine();
	}
	return strResult;
}

主函数调用

代码语言:c#复制
const string key = "xxxxxxxxxx"; // 个人高德申请的应用key
static void Main(string[] args) 
{
	Console.WriteLine("请输入要查询的省份名称,如山东");
	string keywords = Console.ReadLine();
	string queryRegionResult = GetRegionByURL($"key={key}&subdistrict=2&extensions=base&keywords="   keywords);
	Console.WriteLine($"查询{keywords}的结果为:"   queryRegionResult);
	Console.ReadLine();
}

运行结果如下:

以上就是C# 高德地图WebApi对接示例的介绍,做此记录,如有帮助,欢迎点赞关注收藏!

0 人点赞