如何从谷歌地图读取返回的xml值
我正在尝试调用谷歌地图地理编码,并按照其网页上的示例尝试将其应用到我的
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 行给出了索引越界错误。任何人都可以为我指明正确的方向,至少让示例正常工作,以便我可以将逻辑应用到我自己的应用程序中吗?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您访问链接“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.