如何从谷歌地图读取返回的xml值

发布于 2024-12-05 03:57:14 字数 1346 浏览 0 评论 0 原文

我正在尝试调用谷歌地图地理编码,并按照其网页上的示例尝试将其应用到我的

http://code.google.com/apis/maps/documentation/geocoding/index.html

在此示例中,地理编码 API 请求以下内容的 xml 响应: 上面显示的与“1600 Amphitheatre Parkway, Mountain”相同的查询 加利福尼亚州维尤”: http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false 该请求返回的 XML 如下所示。

现在我尝试在我的 c# winforms 应用程序中运行该 url

    string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false";
    WebRequest req = HttpWebRequest.Create(url);
    WebResponse res = req.GetResponse();
    StreamReader sr = new StreamReader(res.GetResponseStream());
    try
    {
        Match coord = Regex.Match(sr.ReadToEnd(), "<coordinates>.*</coordinates>");
        var b = coord.Value.Substring(13, coord.Length - 27);
    }
    finally
    {
        sr.Close();
    }

,但是它似乎没有返回任何内容,因此我的 var b 行给出了索引越界错误。任何人都可以为我指明正确的方向,至少让示例正常工作,以便我可以将逻辑应用到我自己的应用程序中吗?

谢谢

I am trying to call google maps geocode and am following the example on their webpage to try and apply it to mine

http://code.google.com/apis/maps/documentation/geocoding/index.html

in this example, the Geocoding API requests an xml response for the
identical query shown above for "1600 Amphitheatre Parkway, Mountain
View, CA":
http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false
The XML returned by this request is shown below.

Now i am trying to run that url like this in my c# winforms application

    string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false";
    WebRequest req = HttpWebRequest.Create(url);
    WebResponse res = req.GetResponse();
    StreamReader sr = new StreamReader(res.GetResponseStream());
    try
    {
        Match coord = Regex.Match(sr.ReadToEnd(), "<coordinates>.*</coordinates>");
        var b = coord.Value.Substring(13, coord.Length - 27);
    }
    finally
    {
        sr.Close();
    }

However it doesnt seem to be returning anything and as such my var b line gives an index out of bounds error. Can anyone point me in the right direction for at least getting the example to work so i can apply the logic to my own application?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-12-12 03:57:14

如果您访问链接“http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false”直接在浏览器可以看到它返回的内容。它给了我一个请求被拒绝的错误。

该问题是由sensor=true_or_false参数引起的。你必须选择你想要它是真的还是假的。谷歌在他们的例子中是这样说的,所以你必须自己明确决定。此设置指示您的应用程序是否正在使用位置传感器。就你的情况而言,我猜不是,所以将其设置为 false。

如果您将正在使用的链接更改为 http://maps.googleapis.com/maps/api/geocode/xml?address=1600%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA&sensor=false,我认为你会得到你期望的结果。

If you visit your link "http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false" directly in a browser you can see what it's returning. It's giving me a REQUEST DENIED error.

The problem is caused by the sensor=true_or_false parameter. You have to choose if you want it to be true or false. Google put it this way in their example so that you have to explicitly decide for yourself. This setting indicates if your application is using a location sensor or not. In your case, I'm guessing not, so set it to false.

If you change the link you're using to http://maps.googleapis.com/maps/api/geocode/xml?address=1600%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA&sensor=false, I think you'll get the results you were expecting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文