当前位置:   article > 正文

使用地址调用百度地图API返会经纬度(随手笔记)_百度接口返回的经纬度定位

百度接口返回的经纬度定位

使用地址调用百度地图API返会经纬度

A.注册百度地图账号

1.地址
添加链接描述

2.开始注册账号

3.申请AK
在这里插入图片描述
在这里插入图片描述

B.开发文档

1.Web服务API
在这里插入图片描述
说明:百度API会介绍接口都是在什么情况下使用

2.地理编码(就是介绍使用地址返回经纬度的接口说明)
在这里插入图片描述

c.代码示例

        /// <summary>
        /// 地理编码
        /// </summary>
        /// <param name="DetailedAddress"></param>
        /// <returns></returns>
        public void GetGeocoding(string DetailedAddress)
        {
            try
            {
                string url = "http://api.map.baidu.com/geocoding/v3/?";
                List<GeocodingResultJson> listGeocoding = new List<GeocodingResultJson>();
                string[] DetailedCount = DetailedAddress.Split(',');
                for (int i = 0; i < DetailedCount.Length; i++)
                {
                    if (!string.IsNullOrEmpty(DetailedCount[i]))
                    {
                        GeocodingResultJson Geocoding = new GeocodingResultJson();
                        StringBuilder str = new StringBuilder();
                        str.Append(url);
                        str.Append("address=" + DetailedCount[i] + "&output=json&ak=" + BaiDuAK + "");
                        string ResultParameter = HttpUtils.DoGet(str.ToString(), null, "").ToString();
                        if (!string.IsNullOrEmpty(ResultParameter))
                        {
                            ApiGeocodingJson resultJsonData = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiGeocodingJson>(ResultParameter);
                            if (resultJsonData.status == 0)
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.lat = resultJsonData.result.location.lat;
                                Geocoding.lng = resultJsonData.result.location.lng;
                                Geocoding.State = 0;
                                Geocoding.Content = "访问BaiDu接口成功";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据:访问BaiDu接口成功");

                            }
                            else if (resultJsonData.status == 1)
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "访问BaiDu接口失败";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据:访问BaiDu接口失败");
                            }
                            else if (resultJsonData.status == 2)
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "请求参数非法";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据:请求参数非法");
                            }
                            else if (resultJsonData.status == 3)
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "权限校验失败";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据:权限校验失败");
                            }
                            else if (resultJsonData.status == 4)
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "配额校验失败";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据:配额校验失败");
                            }
                            else if (resultJsonData.status == 5)
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "ak不存在或者非法";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据ak不存在或者非法");
                            }
                            else if (resultJsonData.status.ToString() == "101")
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "服务禁用";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据服务禁用");
                            }
                            else if (resultJsonData.status == 102)
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "不通过白名单或者安全码不对";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据不通过白名单或者安全码不对");
                            }
                            else if (resultJsonData.status.ToString() == "2xx")
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "无权限";
                                Log.CreateLogManager().Error("" + DetailedCount[i] + "地址,请求数据无权限");
                            }
                            else if (resultJsonData.status.ToString() == "3xx")
                            {
                                Geocoding.Detaile = DetailedCount[i];
                                Geocoding.State = 1;
                                Geocoding.Content = "配额错误";
                                Log.CreateLogManager().Error(""+ DetailedCount[i] + "地址,请求数据配额错误");
                            }

                            listGeocoding.Add(Geocoding);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.CreateLogManager().Error("接口异常:" + ex);
            }
        }
  • 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
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号