当前位置:网站首页>C# 高德地图 根据经纬度获取地址
C# 高德地图 根据经纬度获取地址
2022-07-02 06:32:00 【清山博客】
using System;
using System.IO;
using System.Net;
using System.Text;
namespace PhotoHelper
{
public class AmapUtil
{
private const string Key = "3ee6df6847015dec899aa46032226fa0";
/// <summary>
/// 根据经纬度获取地址
/// </summary>
/// <param name="lngLatStr">经度纬度组成的字符串 例如:"113.692100,34.752853"</param>
/// <param name="timeout">超时时间默认10秒</param>
/// <returns>失败返回"" </returns>
public static string GetLocationByLngLat(string lngLatStr, int timeout = 10000)
{
var url = $"http://restapi.amap.com/v3/geocode/regeo?key={Key}&location={lngLatStr}";
return GetLocationByUrl(url, timeout);
}
/// <summary>
/// 根据经纬度获取地址
/// </summary>
/// <param name="lng">经度 例如:113.692100</param>
/// <param name="lat">维度 例如:34.752853</param>
/// <param name="timeout">超时时间默认10秒</param>
/// <returns>失败返回"" </returns>
public static string GetLocationByLngLat(double lng, double lat, int timeout = 10000)
{
var url = $"http://restapi.amap.com/v3/geocode/regeo?key={Key}&location={lng},{lat}";
return GetLocationByUrl(url, timeout);
}
/// <summary>
/// 根据URL获取地址
/// </summary>
/// <param name="url">Get方法的URL</param>
/// <param name="timeout">超时时间默认10秒</param>
/// <returns></returns>
private static string GetLocationByUrl(string url, int timeout = 10000)
{
var s = "";
try
{
if (WebRequest.Create(url) is HttpWebRequest req)
{
req.ContentType = "multipart/form-data";
req.Accept = "*/*";
req.UserAgent = "";
req.Timeout = timeout;
req.Method = "GET";
req.KeepAlive = true;
if (req.GetResponse() is HttpWebResponse response)
using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
s = sr.ReadToEnd();
return s;
}
}
}
catch (Exception ex)
{
s = ex.ToString();
}
return s;
}
}
}
注意:文中的Key是我自己申请的个人应用,有次数和频率限制,正式环境请替换为你自己的Key。
边栏推荐
猜你喜欢
Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
DWORD ptr[]
2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
commands out of sync. did you run multiple statements at once
c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
commands out of sync. did you run multiple statements at once
Analysis of the use of comparable, comparator and clonable interfaces
顺序表基本功能函数的实现
随机推荐
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Use the kaggle training model and download your own training model
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
When a custom exception encounters reflection
c语言将字符串中的空格替换成%20
web安全--逻辑越权
Gateway is easy to use
Detailed explanation of NIN network
Installing Oracle database 19C for Linux
Minecraft插件服开服
Nacos 下载启动、配置 MySQL 数据库
整理秒杀系统的面试必备!!!
HCIA—应用层
HCIA - application layer
TCP/IP—传输层
Analysis of the use of comparable, comparator and clonable interfaces
Kubesphere virtualization KSV installation experience
sqli-labs第1关
commands out of sync. did you run multiple statements at once
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24