I have a Windows Forms (WinForm) billing product that currently running .Net Framework 4.6.1 and is being maintained in Visual Studio 2015.
We have been asked to modify the mapping tool that takes a physical address and returns a Latitude/Longitude grid coordinates.
I got myself a key for testing, and applied that in my code.
private void GoogleGeoCodeSearch(LocationRecord currentRecord, String key)
{
var street = String.Format("{0}", currentRecord.street_name).Trim();
var city = String.Format("{0}", currentRecord.city).Trim();
var state = String.Format("{0}", currentRecord.state).Trim();
var postal = String.Format("{0}", currentRecord.postal_code).Trim();
var country = (state.Length == 2 && STATES.Contains(state)) ? "USA" : String.Format("{0}", currentRecord.country).Trim();
var address = String.Format("{0}, {1}, {2}, {3} {4}", street, city, state, postal, country).Replace("-", String.Empty).Replace("'", String.Empty);
var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false®ion={1}&key={2}", Uri.EscapeDataString(address), country, key);
var request = System.Net.WebRequest.Create(requestUri);
using (var response = request.GetResponse())
{
var ok = false;
var title = String.Format("{0}::Google API WebRequest", _title);
var source = "response stream";
var element = "XDocument";
var xdoc = System.Xml.Linq.XDocument.Load(response.GetResponseStream());
if (xdoc != null)
{
source = element;
element = "GeocodeResponse";
var geoResponse = xdoc.Element(element);
if (geoResponse != null)
{
source = element;
element = "result";
var result = geoResponse.Element(element);
if (result != null)
{
source = element;
element = "geometry";
var geometry = result.Element(element);
if (geometry != null)
{
source = element;
element = "location";
var locationElement = geometry.Element(element);
if (locationElement != null)
{
source = element;
element = "lat/lng";
var lat = locationElement.Element("lat");
var lng = locationElement.Element("lng");
if ((lat != null) && (lng != null))
{
Double latitude, longitude;
Double.TryParse(lat.Value, out latitude);
Double.TryParse(lng.Value, out longitude);
SaveCoordinates(currentRecord, latitude, longitude);
ok = true;
}
}
}
} else
{
result = geoResponse.Element("status");
if (result != null)
{
var msg = result.Value;
result = geoResponse.Element("error_message");
if (result != null)
{
msg = String.Format("{0}{1}{1}{2}", msg, Environment.NewLine, result.Value);
}
MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
if (!ok)
{
MessageBox.Show(String.Format("No [{0}] information found in [{1}] element.", element, source), title, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Every time I get to this line, I get a NULL
response:
var result = geoResponse.Element(element);
This is the explanation:
<GeocodeResponse>
<status>REQUEST_DENIED</status>
<error_message>The provided API key is invalid.</error_message>
</GeocodeResponse>
Of course you can use geocoding API from you WinForms app. For the API it doesn't matter what sends the request. The important thing is that the request parameters are correct and the API key is valid.
Indeed I've just tested your code with my own API key and it works fine, which makes me think that there is something wrong with your API key.
So try to create a new one by following these steps:
Library
(2) and search forGeocoding API
(3)Enable
(4) to enable itCreate credentials
button (5)API key
from credentials types (6)