Google Geolocation API - 使用经度和纬度在文本框中获取地址?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google Geolocation API - 使用经度和纬度在文本框中获取地址?相关的知识,希望对你有一定的参考价值。

我已经注意到很多关于如何根据IP地址使用Google地理位置查找您的位置的信息。但我想知道是否以及如何使用此服务输入位置(经度和纬度)并返回当前地址,或至少是一个城市,州。

我想在C#中这样做,但我会使用任何语言。

有什么建议?

答案

你所描述的被称为Reverse Geocoding。 Google提供了Geocoding Web Service API,您可以从服务器端应用程序(使用任何语言)调用to do reverse geocoding

例如,以下请求:

http://maps.google.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false

...将返回如下所示的响应(截断):

<GeocodeResponse> 
 <status>OK</status> 
 <result> 
  <type>street_address</type> 
  <formatted_address>277 Bedford Ave, Brooklyn, NY 11211, USA</formatted_address> 
  <address_component> 
   <long_name>277</long_name> 
   <short_name>277</short_name> 
   <type>street_number</type> 
  </address_component> 
  <address_component> 
   <long_name>Bedford Ave</long_name> 
   <short_name>Bedford Ave</short_name> 
   <type>route</type> 
  </address_component> 
  <address_component> 
   <long_name>Brooklyn</long_name> 
   <short_name>Brooklyn</short_name> 
   <type>sublocality</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>New York</long_name> 
   <short_name>New York</short_name> 
   <type>locality</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>Kings</long_name> 
   <short_name>Kings</short_name> 
   <type>administrative_area_level_2</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>New York</long_name> 
   <short_name>NY</short_name> 
   <type>administrative_area_level_1</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>United States</long_name> 
   <short_name>US</short_name> 
   <type>country</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>11211</long_name> 
   <short_name>11211</short_name> 
   <type>postal_code</type> 
  </address_component> 
  <geometry> 
   <location> 
    <lat>40.7142330</lat> 
    <lng>-73.9612910</lng> 
   </location> 
   <location_type>ROOFTOP</location_type> 
   <viewport> 
    <southwest> 
     <lat>40.7110854</lat> 
     <lng>-73.9644386</lng> 
    </southwest> 
    <northeast> 
     <lat>40.7173806</lat> 
     <lng>-73.9581434</lng> 
    </northeast> 
   </viewport> 
  </geometry> 
 </result> 
</GeocodeResponse> 

但请注意,Google Maps API Terms of Use似乎是prohibit the storage of the results,除非商店充当Google地图中使用的数据的缓存。您可能希望与Google取得联系,并询问Google Maps API Premier,以便为您的地理编码要求提供更灵活的使用条款。

另一答案

这是一个c#实现。请注意,我不是c#程序员 - 因此代码可能很难看。但它的确有效。它为您提供的不仅仅是地址。这是一个控制台应用程序,您应该能够轻松地将其用于webforms或winforms。

using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.XPath;

namespace ReverseGeoLookup
{
 // http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding
    public static string ReverseGeoLoc(string longitude, string latitude,
        out string Address_ShortName,
        out string Address_country,
        out string Address_administrative_area_level_1,
        out string Address_administrative_area_level_2,
        out string Address_administrative_area_level_3,
        out string Address_colloquial_area,
        out string Address_locality,
        out string Address_sublocality,
        out string Address_neighborhood)
    {

        Address_ShortName = "";
        Address_country = "";
        Address_administrative_area_level_1 = "";
        Address_administrative_area_level_2 = "";
        Address_administrative_area_level_3 = "";
        Address_colloquial_area = "";
        Address_locality = "";
        Address_sublocality = "";
        Address_neighborhood = "";

        XmlDocument doc = new XmlDocument();

        try
        {
            doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false");
            XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
            if (element.InnerText == "ZERO_RESULTS")
            {
                return ("No data available for the specified location");
            }
            else
            {

                element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");

                string longname="";
                string shortname="";
                string typename ="";
                bool fHit=false;


                XmlNodeList xnList = doc.SelectNodes("//GeocodeResponse/result/address_component");
                foreach (XmlNode xn in xnList)
                {
                    try
                    {
                        longname = xn["long_name"].InnerText;
                        shortname = xn["short_name"].InnerText;
                        typename = xn["type"].InnerText;


                        fHit = true;
                        switch (typename)
                        {
                            //Add whatever you are looking for below
                            case "country":
                                {
                                    Address_country = longname;
                                    Address_ShortName = shortname;
                                    break;
                                }

                            case "locality":
                                {
                                    Address_locality = longname;
                                    //Address_locality = shortname; //Om Longname visar sig innehålla konstigheter kan man använda shortname istället
                                    break;
                                }

                            case "sublocality":
                                {
                                    Address_sublocality = longname;
                                    break;
                                }

                            case "neighborhood":
                                {
                                    Address_neighborhood = longname;
                                    break;
                                }

                            case "colloquial_area":
                                {
                                    Address_colloquial_area = longname;
                                    break;
                                }

                            case "administrative_area_level_1":
                                {
                                    Address_administrative_area_level_1 = longname;
                                    break;
                                }

                            case "administrative_area_l

以上是关于Google Geolocation API - 使用经度和纬度在文本框中获取地址?的主要内容,如果未能解决你的问题,请参考以下文章

Google Geolocation API 和 Google Maps Android SDK

Google Maps Geolocation API 的替代方案

Google Maps Geolocation API 准确性

用于 wifiAccessPoints 的 Google Maps Geolocation API 返回未找到的地理位置

Google Geolocation API 总是返回相同的位置,不管 cellTower 是不是指定

Google Geolocation API - 使用经度和纬度在文本框中获取地址?