Google Geolocation API-użyj długości i szerokości geograficznej, aby uzyskać adres w polu tekstowym?

Zauważyłem wiele informacji o tym, jak uzyskać swoją lokalizację za pomocą Google geolocation wygląda, na podstawie adresu IP. Ale zastanawiam się, czy i jak mógłbym użyć tej usługi, aby wprowadzić lokalizację (długość i szerokość geograficzną) i odzyskać aktualny adres, lub przynajmniej miasto, stan.

Chciałbym to zrobić w C#, ale będę pracował z każdym językiem.

Jakaś rada?

Author: Kumar PG, 2010-06-30

8 answers

To co opisujesz nazywa się odwrotne geokodowanie . Google zapewnia Geocoding Web Service API, które można wywołać z aplikacji po stronie serwera (przy użyciu dowolnego języka) , aby zrobić odwrotne geocoding.

Na przykład następujące żądanie:

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

... zwróci odpowiedź, która wygląda następująco (okrojony): {]}

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

Należy jednak pamiętać, że Warunki korzystania z interfejsu API Google Maps wydają się zabraniać przechowywania wyników, chyba że sklep działa jako pamięć podręczna dla danych, które będą używane w Google Maps. Możesz skontaktować się z Google i zapytać na Google Maps API Premier , aby uzyskać bardziej elastyczne warunki użytkowania dla Twoich wymagań geokodowania.

 22
Author: Daniel Vassallo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-30 16:54:07

Oto implementacja c#. Proszę zauważyć, że nie jestem programistą c# - więc kod może być brzydki. Ale to działa. Daje Ci więcej niż tylko adres. Jest to aplikacja konsolowa, powinieneś być w stanie łatwo dostosować ją do webforms lub 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);
        }
        }
}
 12
Author: bob,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-19 20:21:06

To, co próbujesz zrobić, to odwrócić geokodowanie (Zobacz odpowiedź Daniela).

Przykładowa implementacja w 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;
}
 3
Author: igorw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-30 16:57:47

Google Maps API przechodzi przez http, więc wysyłanie żądania za pomocą get, a następnie parsowanie żądania... Powinno to być możliwe do zrobienia w dowolnym języku.

Na przykład w 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;

To samo działa dla wszystkich API.

 1
Author: Smar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-30 16:51:45
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);
        }
    }
}
 1
Author: Vitor Myra Moreira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-07 11:40:02

Łatwy sposób na uzyskanie adresu jest za pośrednictwem API google.

Na przykład.

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();

P. S. uważaj na różne adresy z różnymi komponentami.

 1
Author: N T,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-08 14:46:50
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;
    }
 1
Author: Md. Zakir Hossain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-03 11:42:57

Reverse geocoding: get address from latitude and longitude using google maps Geocoding api in asp.net

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;

    }  
 0
Author: Sagar Jadhav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-13 18:52:39