Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
        }
Posted
Updated 11-Jun-18 9:56am

I'm not very familiar with Google's maps API, but since you haven't received an answer, I'll give it a try. My guess is that (at a minimum) you need to URL encode the address portion of your query string:
queryString = System.Web.HttpUtility.UrlEncode(queryString);
Without this, the spaces in your address will likely cause problems. Also, I'd assign you're entire URL (not just the query string) to a variable. Then, I'd examine the variable in the debugger (or write it out) to make certain it conforms to the documentation for the Google API.

I am perhaps mistaken, but the presence of both the query parameters address and address_components seems wrong. Also, it seems unusual that you both pass a parameter and have a fixed value for the address.
 
Share this answer
 
v3
No matter what you do, there is only 1 reliable way to use a webapi like that.
Get 1 example from documentation, make sure it works.
If it don't work, check that exact parameter is what it should, do not assume something is ok.
When doing a request, always store the final request string in a variable and ensure the request is what it should.
My guess is that it would be interesting to see what is in queryString

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900