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

标签 c# winforms google-maps google-maps-api-3 httpwebrequest

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

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.

现在我尝试在我的 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 行给出了索引越界错误。任何人都可以为我指明正确的方向,至少让示例正常工作,以便我可以将逻辑应用到我自己的应用程序中吗?

谢谢

最佳答案

如果您直接在浏览器中访问链接“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 ,我想您会得到您期望的结果。

关于c# - 如何从谷歌地图读取返回的xml值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7474996/

相关文章:

c# - 解密 EFS 加密文件

c# - 如何使用自定义属性属性对类的属性进行排序

c# - 在变量 C# 中保存 richTextBox 的特定行

android - Google Activity Recognition 是否适用于旧版 Android?

php - 在 Google map 上打印大 map

java - 谷歌地图未在 Android 应用程序上加载,应用程序运行时出现空白屏幕

c# - 无法访问基本的 Fabric Azure WebAPI OWIN 无状态服务

c# - 在工作进程 C# 上验证 Youtube API

c# - 如何从 Windows 客户端应用程序使用 openAuth?

.net - 数据绑定(bind)文本框无法退出