I have noticed a lot of information about how to get your location using Google geolocation looks, based on IP address. But I am wondering if and how I could use this service t
What you are trying to do is reverse geocoding (See Daniel's answer).
An example implementation in 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;
}
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;
}
An easy way to get an Address is through google's 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();
P.S. be careful with different addresses with different components.
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.
For example, in 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.
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.
For example, the following request:
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. 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.