This will get you the latitude and longitude of an address.
Note: After testing you should add you’re specific Google API key.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Xml;
using System.Xml.XPath;
namespace GoogleMaps
{
class Program
{
static void Main(string[] args)
{
#region GeoCode - Start
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=One Microsoft, Redmond, VA&sensor=false";
WebResponse response = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
response = request.GetResponse();
if (response != null)
{
XPathDocument document = new XPathDocument(response.GetResponseStream());
XPathNavigator navigator = document.CreateNavigator();
// get response status
XPathNodeIterator statusIterator = navigator.Select("/GeocodeResponse/status");
while (statusIterator.MoveNext())
{
if (statusIterator.Current.Value != "OK")
{
Console.Write("Geocode Problem");
}
}
// get results
XPathNodeIterator resultIterator = navigator.Select("/GeocodeResponse/result");
while (resultIterator.MoveNext())
{
XPathNodeIterator geometryIterator = resultIterator.Current.Select("geometry");
while (geometryIterator.MoveNext())
{
XPathNodeIterator locationIterator = geometryIterator.Current.Select("location");
while (locationIterator.MoveNext())
{
XPathNodeIterator latIterator = locationIterator.Current.Select("lat");
while (latIterator.MoveNext())
{
Console.WriteLine(" lat: " + latIterator.Current.Value);
}
XPathNodeIterator lngIterator = locationIterator.Current.Select("lng");
while (lngIterator.MoveNext())
{
Console.WriteLine(" lng: " + lngIterator.Current.Value);
}
}
}
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
}
#endregion GeoCode - Stop
}
}
}
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.