HI i am trying to retrieve Latitude and longitude of a given Zip code.
This is my code
public ActionResult GoogleMap(string address, string Zip)
{
string Country = "US";
int CustomerID = Convert.ToInt32(Session["CustomerID"]);
string DefaultLocation = PrescriberModel.GetDefaultLocation((long)CustomerID).Address.ToString();
ViewBag.PL = address;
ViewBag.DF = DefaultLocation;
string Lat = "";
string Lon = "";
string PostUrl = "http://ws.geonames.org/postalCodeSearch?postalcode=" + Zip + "&maxRows=10&country=" + Country;
// WebRequest webRequest= null;
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ }
else
{
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string Result = sr.ReadToEnd().Trim();
if (Result != "")
{
// Load the response into an XML doc
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(Result);
// Navigate to latitude node
XmlNodeList name = xdoc.GetElementsByTagName("lat");
if (name.Count > 0)
{
Lat = name[0].InnerText;
}
// Navigate to longitude node
name = xdoc.GetElementsByTagName("lng");
if (name.Count > 0)
{
Lon = name[0].InnerText;
}
}
}
ViewBag.Lat = Lat;
ViewBag.Lng = Lon;
return PartialView("_GoogleMap");
}
I have picked up this code from here
But this is throwing an error for me at webRequest.GetResponse();
I noticed that in that example WebRequest is not instantiated, more over WebRequest is a abstract Class, it cannot be instantiated.
Any help how i can make it Code work.
Thanks for your time.
The problem is that i / we cant see how your
webRequestinstance is created.This will work.