我正在尝试调用谷歌地图地理编码,并在他们的网页上关注示例,尝试将其应用于我的
http://code.google.com/apis/maps/documentation/geocoding/index.html
在此示例中,Geocoding API请求xml响应 上面显示的相同查询“1600 Amphitheatre Parkway,Mountain 查看,CA“: 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行给出了一个超出范围的索引错误。任何人都可以指出我正确的方向,至少让示例工作,所以我可以将逻辑应用于我自己的应用程序?
由于
答案 0 :(得分:4)
如果您直接在浏览器中访问“http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false”链接,则可以看到它返回的内容。它给了我一个REQUEST DENIED错误。
问题是由sensor = true_or_false参数引起的。您必须选择是否要为真或假。谷歌在他们的例子中这样说,所以你必须自己明确决定。此设置指示您的应用程序是否使用位置传感器。在你的情况下,我猜不是,所以把它设置为假。
如果您将正在使用的链接更改为http://maps.googleapis.com/maps/api/geocode/xml?address=1600%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA&sensor=false,我认为您将获得您期望的结果。