• C# 调用高德地图API获取经纬度以及定位【万字详解附完整代码】


    最近有个需求,需要用到定位,本来打算用百度地图API定位,但是发现百度地图定位申请AppKey太麻烦了。因为是写的web端,百度地图定位API申请的Appkey需要网址过滤。索性就用高德定位了(有一说一,高德地图是真得劲)。话不多说,先讲解一下定位实现的一些需要注意的细节问题。

    首先,不管使用的是高德定位还是百度定位,都需要获取AppKey。下面先讲解高德定位APPKEY的获取。 用百度定位的,百度APPKEY获取方法自行百度。

    1.打开高德地图Appkey申请网址:https://lbs.amap.com/,点击右上角控制台,然后进行登录和一系列的认证【进行个人认证就好,日配额5000次。如果定位的量大,再考虑走企业认证】。

    如下:
    在这里插入图片描述

    认证完成后,进入高德地图控制台,创建应用分组:

    在这里插入图片描述
    然后创建AppKey:

    在这里插入图片描述
    这就是我们创建的AppKey:

    在这里插入图片描述

    我已经创建了一个,就直接用开头创建的那个做讲解。高德地图个人开发者每天的各种定位的配额5000次完全够用。

    2.实战代码[发布网址必须是HTTPS,因为是web端,最好用谷歌浏览器,定位精度最高]:

    html:

      
    
    • 1

    Js的前端代码:

        
        
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148

    前端获取到浏览器的定位以后,可以通过网址发送一些get请求,看看定位效果:

    请求地址:https://restapi.amap.com/v3/assistant/coordinate/convert?parameters

    AppKey是你自己申请的那个,

    然后经纬度是前端发送到后台的经纬度。这里请求的时候用逗号分隔一下,经度在前。
    后面的coordsys是经纬度类型。我是转成了百度定位去请求的高德地图的接口,所以填的baidu,
    有好几个类型可以填的,具体的参考高德API开发者文档,地址:https://lbs.amap.com/api/webservice/guide/api/convert

    在这里插入图片描述

    C#后端代码:

     #region 获取用户定位的相关接口,前端传百度坐标系,后端转高德后继续前端请求,获取具体定位,web手机端需使用Google Chrome 浏览器
    
            public IActionResult GPSIndex() 
            {
                return View();
            }
            /// 
            /// 定位请求,返回高德坐标系
            /// 
            /// 经度
            /// 纬度
            /// 密钥
            /// 坐标系类型,此处用百度坐标系
            /// 
            public IActionResult GetLocation(string lat,string lng,string AppKey, string coordsys) 
            {
                var latlng = string.Format(lat + "," + lng);
                //获取高德定位
                var location = LocationResult(latlng,AppKey,coordsys);
                //获取具体位置
                var Gps = GetLocationByLngLat(latlng, AppKey, 10000);
                return Json(Gps);
            }
    
    
    
    
    
            /// 
            ///     根据经纬度获取地址
            /// 
            /// 经度纬度组成的字符串 例如:"113.692100,34.752853"
            /// 超时时间默认10秒
            /// 失败返回"" 
            public static string GetLocationByLngLat(string lngLatStr,string Key,int timeout = 10000)
            {
                var url = $"http://restapi.amap.com/v3/geocode/regeo?key={Key}&location={lngLatStr}";
                return GetLocationByUrl(url, timeout);
            }
    
            /// 
            ///     根据经纬度获取地址
            /// 
            /// 经度 例如:113.692100
            /// 维度 例如:34.752853
            /// 超时时间默认10秒
            /// 失败返回"" 
            public static string GetLocationByLngLat(double lng, double lat,string Key, int timeout = 10000)
            {
                var url = $"http://restapi.amap.com/v3/geocode/regeo?key={Key}&location={lng},{lat}";
                return GetLocationByUrl(url, timeout);
            }
    
            /// 
            ///     根据URL获取地址
            /// 
            /// Get方法的URL
            /// 超时时间默认10秒
            /// 
            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;
            }
    
            public static GaodeGetCoding LocationResult(string latlng, string AppKey, string coordsys)
            {
                var url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters";
                url += string.Format(AppKey, latlng, coordsys);
                var Json = GetResponseString(CreateGetHttpResponse(url, null));
                var model = JsonConvert.DeserializeObject(Json);
                return model;
            }
    
            ///   
            /// 创建GET方式的HTTP请求  
            ///   
            public static HttpWebResponse CreateGetHttpResponse(string url, CookieCollection cookies)
            {
                HttpWebRequest request = null;
                request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                request.Proxy = null;
    
                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }
                return request.GetResponse() as HttpWebResponse;
            }
    
            /// 
            /// 获取请求的数据
            /// 
            public static string GetResponseString(HttpWebResponse webresponse)
            {
                using (Stream s = webresponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(s, Encoding.UTF8);
                    return reader.ReadToEnd();
    
                }
            }
            #endregion
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128

    返回值的封装:

    namespace Rongbo.DDPG.Entity.GaoDeLocationAPI
    {
        public class GaodeGetCoding
        {
            /// 
            /// 返回状态,1=成功,0=失败
            /// 
            public int Status { get; set; }
            /// 
            /// 成功编码  OK
            /// 
            public string Info { get; set; }
            
            public string InfoCode { get; set; }
            public List Regeocode { get; set; }
        }
    
        public class Gaode 
        {
            public string Formatted_address { get; set; }
    
            public List AddressComponent { get; set; }
        }
    
        public class GaodeList 
        {
            /// 
            /// 国籍
            /// 
            public string  Country { get; set; }
            /// 
            /// 省名
            /// 
            public string Province { get; set; }
            /// 
            /// 市名
            /// 
            public string City { get; set; }
            /// 
            /// 市区码
            /// 
            public string CityCode { get; set; }
            /// 
            /// 省县名
            /// 
            public string district { get; set; }
            /// 
            /// 省县码
            /// 
            public string adCode { get; set; }
            /// 
            /// 街道名
            /// 
            public string TownShip { get; set; }
    
            public string TownCode { get; set; }
    
            public List StreetNumber { get; set; }
    
            public List BusinessAreas { get; set; }
    
            public List Building { get; set; }
    
            public List Neighborhood { get; set; }
        }
    
    
        public class Neighborhood 
        {
            public string Name { get; set; }
    
            public string Type { get; set; }
        }
        public class Building 
        {
            public string Name { get; set; }
    
            public string Type { get; set; }
        }
    
        public class StreetNumber
        {
            public List Street { get; set; }
    
            public List Number { get; set; }
    
            public string Location { get; set; }
    
            public List Direction { get; set; }
    
            public List Distance { get; set; }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    前端返回的Json效果【具体地址是我自己隐藏的】:

    {“status”:“1”,“regeocode”:{“addressComponent”:{“city”:“杭州市”,“province”:“浙江省”,“adcode”:“330110”,“district”:“余杭区”,“towncode”:“330110012000”,“streetNumber”:{“number”:“359号”,“location”:“119.993782,30.277486”,“direction”:“西北”,“distance”:“41.1047”,“street”:“舒心路”},“country”:“中国”,“township”:“仓前街道”,“businessAreas”:[[]],“building”:{“name”:[],“type”:[]},“neighborhood”:{“name”:[],“type”:[]},“citycode”:“0571”},“formatted_address”:“浙江省杭州市余杭区仓前街道**********”},“info”:“OK”,“infocode”:“10000”}

    然后拿到了以后,你们就可以进行一系列操作了。

    原创不易,看完记得点赞收藏鼓励。

  • 相关阅读:
    嵌入式Linux应用开发基础知识(五)——Makefile函数
    生活中的视音频技术
    代码都成屎山了,还在用if-else?不如试试我的这套工厂模式+Map+自定义注解+枚举
    『忘了再学』Shell基础 — 3、echo命令的介绍与使用
    Fastbin attack&&Double free和Unsortbin leak的综合使用
    多线程下使用随机数ThreadLocalRandom
    SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版
    电子图书馆
    滤波算法_标准卡尔曼滤波(SKF, Standard Kalman filter)_详细数学推导
    AI人脸识别网关接口使用说明
  • 原文地址:https://blog.csdn.net/qq_42782011/article/details/126259207