zoukankan      html  css  js  c++  java
  • 通过google地图的webservice根据城市名称获取经纬度

    谷歌Geocoding webservice接口获取经纬度信息,由于获取地点的数量级太大,2000多条记录,从response的xml格式中取出该地点的经纬度信息。google有访问限制,如果超出2500次访问,它会停止你的服务,所以这个就是麻烦所在,在调试的时候经常会遇到每个地名的错误回复,原因应该是该地点取不到,因为谷歌地图的webservice也没有该城市的信息,尽管该城市是存在的。或者会遇到over_query_limit 的返回结果,恭喜你,今天的限额已经用完,不过据我发现,大概1小时之后,就可以继续使用了,所以在调试的时候注意这一点。
     

    Google Geocoding API 请求必须采用以下形式:

    http://maps.google.com/maps/api/geocode/output?parameters

    其中,output 可以是以下值之一:

    • json(建议)表示以 JavaScript 对象表示法 (JSON) 的形式输出
    • xml 表示以 XML 的形式输出

    有些参数是必需的,有些是可选的。根据网址的标准,所有参数均使用字符 & (&) 分隔。下面枚举了这些参数及其可能的值。

    Google Geocoding API 使用以下网址参数定义地址解析请求:

    • address(必需)- 您要进行地址解析的地址。*
    •      或者

    • latlng(必需)- 您希望获取的、距离最近的、可人工读取地址的纬度/经度文本值。*
    • bounds(可选)- 要在其中更显著地偏移地址解析结果的可视区域的边框。(有关详细信息,请参见下文的可视区域偏向。)
    • region(可选)- 区域代码,指定为 ccTLD(“顶级域”)双字符值。(有关详细信息,请参见下文的区域偏向。)
    • language(可选)- 传回结果时所使用的语言。请参见支持的区域语言列表。请注意,我们会经常更新支持的语言,因此该列表可能并不详尽。如果未提供language,地址解析器将尝试尽可能使用发送请求的区域的本地语言。
    • sensor(必需)- 指示地址解析请求是否来自装有位置传感器的设备。该值必须为 true 或 false

    * 请注意:您可以传递 address 或 latlng 进行查找。(如果传递 latlng,则地址解析器执行反向地址解析。有关详细信息,请参阅反向地址解析。)

    bounds 和 region 参数只会影响地址解析器返回的结果,但不能对其进行完全限制。

    访问websevice的结果请参看:http://maps.google.com/maps/api/geocode/xml?address=香港&sensor=false

    代码如下:

        XmlDocument xmldoc = new XmlDocument();
                string fn1 = AppDomain.CurrentDomain.BaseDirectory + "CapitalCities1.xml";
                xmldoc.Load(fn1);
                XmlNodeList xl = xmldoc.SelectNodes("HFData/city");
                XmlNode xn;
                location loc;
                for (int i = 0; i < xl.Count; i++)
                {
                    xn = xl[i].SelectSingleNode("CityName");
                    loc=getlocation(xn.InnerText);
                    xl[i].SelectSingleNode("Latitude").InnerText = loc.lat;
                    xl[i].SelectSingleNode("Longitude").InnerText = loc.lon;
                }
                xmldoc.Save(fn1);
     private location getlocation(string cityname)
     {
                string url = "http://maps.google.com/maps/api/geocode/xml?address=" + cityname + "&sensor=false";
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                String strConfig = String.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    strConfig = reader.ReadToEnd();
                }
                XmlDocument node = new XmlDocument();
                node.LoadXml(strConfig);
                location loc = new location();
               loc.lat=node.SelectSingleNode("GeocodeResponse/result/geometry/location/lat").InnerText;
                loc.lon = node.SelectSingleNode("GeocodeResponse/result/geometry/location/lng").InnerText;
                return loc;
    }
    xml格式示例:
    <?xml version="1.0" encoding="utf-8"?>
    <HFData>
      <city>
        <StationID>45005</StationID>
        <CityName>香港</CityName>
        <Latitude>22.3964280</Latitude>
        <Longitude>114.1094970</Longitude>
        <Population>137</Population>
        <Province>香港</Province>
      </city>
    </HFData>
  • 相关阅读:
    Docker容器启动时初始化Mysql数据库
    使用Buildpacks高效构建Docker镜像
    Mybatis 强大的结果集映射器resultMap
    Java 集合排序策略接口 Comparator
    Spring MVC 函数式编程进阶
    换一种方式编写 Spring MVC 接口
    【asp.net core 系列】6 实战之 一个项目的完整结构
    【asp.net core 系列】5 布局页和静态资源
    【asp.net core 系列】4. 更高更强的路由
    【Java Spring Cloud 实战之路】- 使用Nacos和网关中心的创建
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8333908.html
Copyright © 2011-2022 走看看