当前位置:网站首页>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。
边栏推荐
猜你喜欢
随机推荐
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
D interface and domain problems
PCL calculates the intersection of three mutually nonparallel planes
C language replaces spaces in strings with%20
ARP and ARP Spoofing
Makefile基本原理
Qt的拖动事件
Simple implementation scheme of transcoding and streaming (I)
Nacos download, start and configure MySQL database
Sqli labs level 8 (Boolean blind note)
Finishing the interview essentials of secsha system!!!
Jumping | Blue Bridge Cup
路由基础—动态路由
Luogu greedy part of the backpack line segment covers the queue to receive water
libusb的使用
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
Dip1000 implicitly tagged with fields
File upload and download performance test based on the locust framework
Minecraft install resource pack
Web技术发展史