我正在尝试调用谷歌地图地理编码,并按照其网页上的示例尝试将其应用到我的
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/