I needed to write a function to get a set of co-ordinates from an address, supplied as a string.
Using the Google API, I came up with this:
First, I created a struct, to represent the lat and lon co-ordinates:
public struct Coordinate
{
private double _latitude;
private double _longitude;
public Coordinate(double latitude, double longitude)
{
_latitude = latitude;
_longitude = longitude;
}
public double Latitude { get { return _latitude; } set { _latitude = value; } }
public double Longitude { get { return _longitude; } set { _longitude = value; } }
}
This is a simple representation of a set of latitude and longitude co-ordinates.
The main method, which returns the above Coordinate struct is:
public static Coordinate GetCoordinates(string address)
{
using (var client = new WebClient())
{
Uri uri = YOUR_URI_HERE;
/* The first number is the status code,
* the second is the accuracy,
* the third is the latitude,
* the fourth one is the longitude.
*/
string[] geocodeInfo = client.DownloadString(uri)
.Split(',');
return new Coordinate(
Convert.ToDouble(geocodeInfo[2]),
Convert.ToDouble(geocodeInfo[3]));
}
}
On line 5, you need to specify your Google API URL, in the format of:
http://maps.google.com/maps/geo?q=ADDRESS GOES HERE&output=csv&key=YOUR KEY HERE
And that’s it!
Obviously, there are usage restrictions when using the Google API for this, as outlined on the documentation pages
3 responses to “Reverse GeoCoding with Google API and C#”
Thanks for the example, works like a charm.
Just one thing: that’s not reverse geocoding. Reverse geocoding is the translation from lat/lon to a human readable address (at least in the terms of google) http://code.google.com/intl/en-US/apis/maps/documentation/services.html#ReverseGeocoding
Hi All,
i’m getting error in using above code. plz let me know where i’m wrong
GetCoordinates(“http://localhost:24094/webform2.aspx”)
and my URI is
http://maps.google.com/maps/geo?q=address&output=csv&key=MYKEY
Error Message is
GoogleSorry…
We’re sorry…
… but your computer or network may be sending automated queries. To protect our users, we can’t process your request right now.
See Google Help for more information.
This indicates a problem with your network / ISP
http://www.southbourne.com/blog/2012/06/29/google-blocking-search-results/
Try this
https://productforums.google.com/forum/#!topic/websearch/F3TlQl5Wvos