如何在C#中动态使用地址变量fr地理编码来获取 [英] How can I dynamically use address variable fr geocode in C# for getting

查看:101
本文介绍了如何在C#中动态使用地址变量fr地理编码来获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到在C#中使用Geocode的经度和纬度,但我有一个错误,例如

{

error_message:无效请求。错过了'address','components','latlng'或'place_id'参数。,

结果:[],

status:INVALID_REQUEST< br $>
}





有时响应错误

i需要使用地址变量内部参数并传入查询字符串



我尝试过:



public static List GetAddressesByKeyword(string keyword,string address = null)



{

var parameters = new Dictionary< string,string> ()

{

{地址,836 Kilbridge Lane,Coppell,TX},

{input,关键字。替换(,+)},

{类型,地理编码},

{key,Configura tionManager.AppSettings [GoogleApiKey]}

};



if(!String.IsNullOrWhiteSpace(address))

parameters [address_components] = String.Format(address {0},address.ToLower());



var queryString = String.Join (&,参数。选择(kv => kv.Key +=+ kv.Value));



var webRequest = WebRequest.Create(http://maps.googleapis.com/maps/ api / geocode / json?+ queryString);

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;
        }

推荐答案

我对谷歌的地图API不太熟悉,但由于你没有收到答案,我试试看。我的猜测是(至少)你需要对查询字符串的地址部分进行URL编码:

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);

如果没有这个,地址中的空格可能会导致问题。另外,我将整个URL(不仅仅是查询字符串)分配给变量。然后,我会检查调试器中的变量(或写出来),以确保它符合Google API的文档。



我可能会弄错,但是查询参数地址 address_components 的存在似乎是错误的。此外,您传递参数并为地址设置固定值似乎很不寻常。

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.


无论您做什么,只有一种可靠的方法来使用这样的webapi。

从文档中获取1个示例,确保它有效。

如果它不起作用,请检查确切的参数是否应该是什么,不要假设一些正常。

在执行请求时,始终将最终请求字符串存储在变量中并确保请求是应该的。

我的猜测是看到什么会很有趣在queryString



你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较它应该这样做。

调试器中没有魔法,它不知道你是什么应该这样做,它没有发现错误,只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只显示你的代码正在做,你的任务是与它应该做的事情进行比较。
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.


这篇关于如何在C#中动态使用地址变量fr地理编码来获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆