Skip to content

Instantly share code, notes, and snippets.

@robocide
Last active August 29, 2015 14:06
Show Gist options
  • Save robocide/dfbdf70592ff287d6629 to your computer and use it in GitHub Desktop.
Save robocide/dfbdf70592ff287d6629 to your computer and use it in GitHub Desktop.
C# Geolocation - GoogleMaps API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
class Program
{
//static string baseUri = "http://maps.googleapis.com/maps/api/" +
// "geocode/xml?latlng={0},{1}&sensor=false";
static string baseUri = "http://maps.googleapis.com/maps/api/" +
"geocode/xml?address={0}+{1}+{2}&sensor=false";
static void Main(string[] args)
{
//RetrieveFormatedAddress("51.962146", "7.602304");
//RetrieveFormatedAddress("32.054727", "34.770838");
RetrieveFormatedAddress("hertsel","91","tel aviv");
Console.ReadLine();
}
public static void RetrieveFormatedAddress(string lat, string lng,string lng2)
{
string requestUri = string.Format(baseUri, lat, lng,lng2);
using (WebClient wc = new WebClient())
{
wc.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(requestUri));
}
}
static void wc_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
var xmlElm = XElement.Parse(e.Result);
var status = (from elm in xmlElm.Descendants()
where elm.Name == "status"
select elm).FirstOrDefault();
if (status.Value.ToLower() == "ok")
{
var res = (from elm in xmlElm.Descendants()
where elm.Name == "location"
select elm).ToList();
for (int i = 0; i < res.Count; ++i)
{
Console.WriteLine(res[i]);
}
}
else
{
Console.WriteLine("No Address Found");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment