当前位置:网站首页>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。
边栏推荐
- Qt QTimer类
- Linux安装Oracle Database 19c
- Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
- Makefile基本原理
- HCIA - data link layer
- c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
- Viewing JS array through V8
- sqli-labs第2关
- Qunhui NAS configuring iSCSI storage
- Qt的右键菜单
猜你喜欢
![DWORD ptr[]](/img/6e/f68863c9f5b8608b22a24d9c1836d9.jpg)
DWORD ptr[]

群辉 NAS 配置 iSCSI 存储

Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
![[blackmail virus data recovery] suffix Crylock blackmail virus](/img/b2/8e3a65dd250b9194cfc175138c740c.jpg)
[blackmail virus data recovery] suffix Crylock blackmail virus

方法递归(斐波那契数列,青蛙跳台阶,汉诺塔问题)

Solid principle: explanation and examples

Linux binary installation Oracle database 19C

Finishing the interview essentials of secsha system!!!

OpenShift构建镜像

Minecraft模组服开服
随机推荐
Detailed explanation of NIN network
Pointer initialization
[blackmail virus data recovery] suffix Rook3 blackmail virus
File upload and download performance test based on the locust framework
Linux安装Oracle Database 19c
Judge whether it is Sudoku
Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
Minecraft air Island service
Qt的connect函数和disconnect函数
Qt的右键菜单
Minecraft群組服開服
Sentinel easy to use
Qunhui NAS configuring iSCSI storage
idea中注释代码取消代码的快捷键
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
Kubesphere virtualization KSV installation experience
What are the platforms for selling green label domain names? What is the green label domain name like?
Service de groupe minecraft
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings