I am trying to locate the longitude and latitude of using Geocode in C# but i have an error like
{
"error_message" : "Invalid request. Missing the 'address', 'components', 'latlng' or 'place_id' parameter.",
"results" : [],
"status" : "INVALID_REQUEST"
}
and sometimes response error
i need to use the address variables inside parameters and pass into the query string
What I have tried:
public static List GetAddressesByKeyword(string keyword , string address = null)
{
var parameters = new Dictionary<string, string>()
{
{"address", " 836 Kilbridge Lane, Coppell, TX" },
{"input",keyword.Replace(" ", "+") },
{"types", "geocode" },
{"key", ConfigurationManager.AppSettings["GoogleApiKey"]}
};
if (!String.IsNullOrWhiteSpace(address))
parameters["address_components"] = String.Format("address{0}", address.ToLower());
var queryString = String.Join("&", parameters.Select(kv => kv.Key + "=" + kv.Value));
var webRequest = WebRequest.Create("http://maps.googleapis.com/maps/api/geocode/json?" + queryString );
public static List GetAddressesByKeyword(string keyword , string address = null)
{
var parameters = new Dictionary<string, string>()
{
{"address", " 836 Kilbridge Lane, Coppell, TX" },
{"input",keyword.Replace(" ", "+") },
{"types", "geocode" },
{"key", ConfigurationManager.AppSettings["GoogleApiKey"]}
};
if (!String.IsNullOrWhiteSpace(address))
parameters["address_components"] = String.Format("address{0}", address.ToLower());
var queryString = String.Join("&", parameters.Select(kv => kv.Key + "=" + kv.Value));
var webRequest = WebRequest.Create("http://maps.googleapis.com/maps/api/geocode/json?" + queryString );
var webResponse = webRequest.GetResponse();
var items = new List<googleaddressmodel>();
using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
var content = reader.ReadToEnd();
var mapItem = JsonConvert.DeserializeObject<googlegeocoderesponse>(content);
if (mapItem.status.ToLower() == "ok")
{
foreach (var r in mapItem.results)
{
items.Add(ResultToModel(r));
}
}
}
return items;
}