zoukankan      html  css  js  c++  java
  • google map 地址编码及反向地址编码

    之前有做过一些地图相关的开发,查阅了很多资料,也学习到很多,但一直没有时间记录下来与大家分享,目前项目在继续完善及维护阶段,添加了一些功能,比如在地图上取点、画线、画圆以及多边形等,功能是做好了 但是我们的操作人员反馈有些不人性化的地方,在我要去画图的时候,首先我要找到那个地方,虽然涉及的范围不大,但对于不熟悉该地区的人来说就得慢慢去找,大海捞针啊那是,于是乎我就需要地址搜索,其实这是很简单的,但是对于初次接触地图开发的人来说就不知道从哪里获取信息来源,这也是我之前学习过程中走过的路,记下来方便自己,同时也方便他人,本人比较懒所以直接上代码

    通过地址搜索地图位置有两种方式:

    地址格式为:http://maps.googleapis.com/maps/api/geocode/output?parameters

    一、通过地址(如道路信息)查找经纬度(地址编码)

         [Invoke]
            public ResultValue GetLatLngByAddr(string addr)
            {
                var resultVal = ResultValue.Current;
    
                string result = "";
            //目标地址,通过此地址获取经纬度信息
                string tagUrl = String.Format("http://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=true", addr);
                var cookies = new CookieCollection();
                try
                {         
              //执行http请求,HttpWebResponseUtility是封装的http请求,代码就不贴出了,如需要的请自行google
                    var response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, cookies);
    
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
    
                    if (!string.IsNullOrEmpty(result))
                    {           
                //将获取的结果转化为JSON对象
                        var jobj = (JObject)JsonConvert.DeserializeObject(result);
    
                        string status = jobj["status"].ToString();
                        if (status.Equals("OK"))
                        {
                            var jarr = (JArray)jobj["results"];              
                  //获取地址经纬度信息jLatLng即为JSON格式的经纬度信息
                            string jLatLng = jarr[0]["geometry"]["location"].ToString();
    
                            resultVal.result = true;
                            resultVal.msg = jLatLng;
                        }
                    }
                }
                catch (Exception ex)
                {
                    resultVal.result = false;
                    resultVal.msg = ex.Message;
                }
    
                return resultVal;
            }
    

      二、通过经纬度信息查找地址名称(反向地址编码)

         [Invoke]
            public ResultValue GetAddrByLatLng(string latLng)
            {
                var resultVal = ResultValue.Current;
    
                string result = "";
            //目标地址,通过此地址获取经纬度信息
                string tagUrl = String.Format("http://maps.googleapis.com/maps/api/geocode/json?latlng={0}&sensor=true", latLng);
                var cookies = new CookieCollection();
                try
                {
                    var response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, cookies);
    
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
    
                    if (!string.IsNullOrEmpty(result))
                    {            
                //将获取的结果转化为JSON对象
                        var obj = (JObject)JsonConvert.DeserializeObject(result);
    
                        string status = obj["status"].ToString();
                        if (status.Equals("OK"))
                        {
                            var arr = (JArray)obj["results"];              
                  //获取地址信息addr
                            string addr = arr[0]["formatted_address"].ToString();
    
                            resultVal.result = true;
                            resultVal.msg = addr;
                        }
                    }
                }
                catch (Exception ex)
                {
                    resultVal.result = false;
                    resultVal.msg = ex.Message;
                }
    
                return resultVal;
            }
    

      注:这是本人开博以来第一篇随笔,往后还会继续将工作学习中碰到的问题都记录下来与大家一起学习交流,分享更多更好的内容.

  • 相关阅读:
    内存管理
    git学习思维导图
    shell编程 条件判断式----利用 case ..... esac 判断
    shell编程 条件判断式----利用 if .... then ----多重
    shell编程 Shell script 的默认变量($0, $1...)
    VxWorks实验六 基于优先级的抢占式调度及实验的源程序和实验步骤
    Linux下FTP用户的使用配置
    Linux 下 FTP虚拟用户的使用配置
    Linux 常用命令
    实验一 用户界面与Shell命令
  • 原文地址:https://www.cnblogs.com/brandycoffee/p/gmapapi.html
Copyright © 2011-2022 走看看