简体   繁体   English

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

[英]Google Geolocation API - Use longitude and latitude to get address in textbox?

I have noticed a lot of information about how to get your location using Google geolocation looks, based on IP address. 我已经注意到很多关于如何根据IP地址使用Google地理位置查找您的位置的信息。 But I am wondering if and how I could use this service to input a location (longitude and latitude) and get back the current address, or at least a city, state. 但我想知道是否以及如何使用此服务输入位置(经度和纬度)并返回当前地址,或至少是一个城市,州。

I would like to do this in C#, but I'll work with any language. 我想在C#中这样做,但我会使用任何语言。

Any advice? 有什么建议?

What you describe is called Reverse Geocoding . 您描述的内容称为反向地理编码 Google provides a Geocoding Web Service API which you can call from your server-side application (using any language) to do reverse geocoding . Google提供了一个地理编码Web服务API ,您可以从服务器端应用程序(使用任何语言)调用它来执行反向地理编码

For example, the following request: 例如,以下请求:

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

... will return a response that looks like the following (truncated): ...将返回如下所示的响应(截断):

<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> 

However be aware that the Google Maps API Terms of Use seem to prohibit the storage of the results , unless the store acts as a cache for data that will used in Google Maps. 但请注意, Google Maps API使用条款似乎禁止存储结果 ,除非该商店充当Google地图中将使用的数据的缓存。 You may want to get in touch with Google and enquire on the Google Maps API Premier to have more flexible terms of use for your geocoding requirements. 您可能希望与Google取得联系,并在Google Maps API Premier上查询,以便为您的地理编码要求提供更灵活的使用条款。

Here is ac# implementation. 这是ac#implementation。 Please note that I am no c# programmer - so the code might be ugly. 请注意,我不是c#程序员 - 因此代码可能很难看。 But it works. 但它的确有效。 It gives you more than just the address. 它为您提供的不仅仅是地址。 This is a console application, you should be able to adapt it for webforms or winforms easily. 这是一个控制台应用程序,您应该能够轻松地将其用于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_level_2":
                                {
                                    Address_administrative_area_level_2 = longname;
                                    break;
                                }

                            case "administrative_area_level_3":
                                {
                                    Address_administrative_area_level_3 = longname;
                                    break;
                                }

                            default:
                                fHit = false;
                                break;
                        }


                        if (fHit)
                        {
                            Console.Write(typename);
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("\tL: " + longname + "\tS:" + shortname + "\r\n");
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                    }

                    catch (Exception e)
                    {
                        //Node missing either, longname, shortname or typename
                        fHit = false;
                        Console.Write(" Invalid data: ");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("\tX: " + xn.InnerXml + "\r\n");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }


                }

                //Console.ReadKey();
                return (element.InnerText);
            }

        }
        catch (Exception ex)
        {
            return ("(Address lookup failed: ) " + ex.Message);
        }
        }
}

What you are trying to do is reverse geocoding (See Daniel's answer). 您要做的是反向地理编码(请参阅Daniel的回答)。

An example implementation in PHP: PHP中的示例实现:

/**
* reverse geocoding via google maps api
* convert lat/lon into a name
*/
function reverse_geocode($lat, $lon) {
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
    $data = json_decode(file_get_contents($url));
    if (!isset($data->results[0]->formatted_address)){
        return "unknown Place";
    }
    return $data->results[0]->formatted_address;
}
protected void Button1_Click(object sender, EventArgs e)
    {
        this.calcularRota(txtOrigem.Text.Trim(), txtDestino.Text.Trim());
    }


    public void calcularRota(string latitude, string longitude)
    {
        //URL do distancematrix - adicionando endereco de origem e destino
        string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude);
        XElement xml = XElement.Load(url);

        // verifica se o status é ok
        if (xml.Element("status").Value == "OK")
        {
            //Formatar a resposta
            Label3.Text = string.Format("<strong>Origem</strong>: {0}",
                //Pegar endereço de origem 
                xml.Element("result").Element("formatted_address").Value);
            //Pegar endereço de destino                    
        }
        else
        {
            Label3.Text = String.Concat("Ocorreu o seguinte erro: ", xml.Element("status").Value);
        }
    }
}

Google's Maps APIs goes over http, so sending request with get and then parsing the request... It should be possible to do in any language. 谷歌的Maps API遍布http,所以用get然后解析请求发送请求......应该可以用任何语言进行。

For example, in PHP: 例如,在PHP中:

$ret = file_get_contents("http://maps.google.com/maps/api/geocode/xml?address=" .
            urlencode($address) .
            "&sensor=false" .
            "&key=" . $this->key
    );

$xml = new SimpleXMLElement($ret);
$error = $xml->status;

Same works for all APIs. 所有API都适用。

An easy way to get an Address is through google's API. 获取地址的简单方法是通过谷歌的API。

For example. 例如。

using System.Xml;

//Console.WriteLine("enter coordinate:");
string coordinate = "32.797821,-96.781720"; //Console.ReadLine();

XmlDocument xDoc = new XmlDocument();
xDoc.Load("https://maps.googleapis.com/maps/api/geocode/xml?latlng=" + coordinate);

XmlNodeList xNodelst = xDoc.GetElementsByTagName("result");
XmlNode xNode = xNodelst.Item(0);
string FullAddress = xNode.SelectSingleNode("formatted_address").InnerText;
string Number = xNode.SelectSingleNode("address_component[1]/long_name").InnerText;
string Street = xNode.SelectSingleNode("address_component[2]/long_name").InnerText;
string Village = xNode.SelectSingleNode("address_component[3]/long_name").InnerText;
string Area = xNode.SelectSingleNode("address_component[4]/long_name").InnerText;
string County = xNode.SelectSingleNode("address_component[5]/long_name").InnerText;
string State = xNode.SelectSingleNode("address_component[6]/long_name").InnerText;
string Zip = xNode.SelectSingleNode("address_component[8]/long_name").InnerText;
string Country = xNode.SelectSingleNode("address_component[7]/long_name").InnerText;
Console.WriteLine("Full Address: " + FullAddress);
Console.WriteLine("Number: " + Number);
Console.WriteLine("Street: " + Street);
Console.WriteLine("Village: " + Village);
Console.WriteLine("Area: " + Area);
Console.WriteLine("County: " + County);
Console.WriteLine("State: " + State);
Console.WriteLine("Zip: " + Zip);
Console.WriteLine("Country: " + Country);

Console.ReadLine();

PS be careful with different addresses with different components. PS请注意不同组件的不同地址。

reverse geocoding: get address from latitude and longitude using google maps geocoding api in asp.net 反向地理编码:使用谷歌地图在asp.net中进行地理编码api从纬度和经度获取地址

protected void Page_Load(object sender, EventArgs e)
    {
        GetAddress("53.2734", "-7.77832031");
    }
    private string GetAddress(string latitude, string longitude)
    {
        string locationName = "";
        string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude);
        XElement xml = XElement.Load(url);
        if (xml.Element("status").Value == "OK")
        {
            locationName = string.Format("{0}",
                xml.Element("result").Element("formatted_address").Value);
            Label1.Text = locationName;
        }
        return locationName;

    }  
private string getLocationByGeoLocation(string longitude, string latitude)
    {
        string locationName = string.Empty;

        try
        {
            if (string.IsNullOrEmpty(longitude) || string.IsNullOrEmpty(latitude))
                return "";

            string url = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude);


            WebRequest request = WebRequest.Create(url);

            using (WebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    DataSet dsResult = new DataSet();
                    dsResult.ReadXml(reader);
                    try
                    {
                        foreach (DataRow row in dsResult.Tables["result"].Rows)
                        {
                            string fullAddress = row["formatted_address"].ToString();
                        }
                    }
                    catch (Exception)
                    {

                    }

                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }


        return locationName;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google API获取与C#中的纬度和经度相对应的地址 - Google API to get address corresponding to Latitude and Longitude in C# 问题使用Google API获取给定地址的经度和纬度 - Issue getting latitude and longitude of a given address using google api 解析解析以使用C#获取Google Geocoding API的纬度和经度 - Parse Parse to get Latitude and Longitude of the Google Geocoding API using C # 使用 Google API 获取多个地址的经纬度 - Get the latitude and longitude of multiple addresses using Google API 通过c# webforms asp.net中的google map api从下拉列表中的选定城市获取textbox1和textbox2的纬度和经度 - Get latitude and longitude to textbox1 and textbox2 from selected city in dropdownlist through google map api in c# webforms asp.net 如何在gmap中使用经度和纬度获取地址? - How to get the address using Latitude and Longitude with gmap? 如何获取地址的经纬度并保存到数据库 - How to get latitude and longitude of an address and save to database 使用经度和纬度获取地址和地名 - Get address and place name using latitude and longitude 从地址 C# 获取纬度经度 - Get Latitude Longitude from an address c# .net中每个地址的经度和纬度为零 - get Zero as longitude and latitude with every address in .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM