Reputation: 531
I am writing functionality for checking an entered addresses validity with google API. The user types in street number, street name, suburb etc which then looks up google api and returns a formatted address which is closest suited to the entered address.
I have got the following so far:
public string getFormattedAddress(string address)
{
string url = "https://maps.googleapis.com/maps/api/geocode/json?address=%" + address + "&key=" + MyStaticMethods.GOOGLE_API_KEY;
object result = new WebClient().DownloadString(url);
string json = result.ToString();
JObject obj = JObject.Parse(json);
...
}
But i'm having trouble reading the response from the API. The following is pseudocode of what I want to do:
var streetNumber = apiResult.streetNumber
var address = apiResult.address;
var postcode = apiResult.postcode;
I also want a way of telling if the API lookup could not find any matching addresses for the given address.
Any help would be great,
Thanks in advance.
Upvotes: 2
Views: 5156
Reputation: 694
I suggest you to use this library: https://github.com/chadly/Geocoding.net
Example:
GoogleGeocoder geocoder = new GoogleGeocoder();
IEnumerable<GoogleAddress> addresses = await geocoder.GeocodeAsync("1600 pennsylvania ave washington dc");
var country = addresses.Where(a => !a.IsPartialMatch).Select(a => a[GoogleAddressType.Country]).First();
Console.WriteLine("Country: " + country.LongName + ", " + country.ShortName);
//Country: United States, U
Upvotes: 2