我尝试在Windows Phone 7应用中使用网络服务http://www.geoplugin.com/webservices/extras,以获取基于纬度/经度的位置详情。 我首先尝试使用随机值的Web服务并获得此xml文件
<geoPlugin>
<geoplugin_place>Redmond</geoplugin_place>
<geoplugin_countryCode>US</geoplugin_countryCode>
<geoplugin_region>Washington</geoplugin_region>
<geoplugin_regionAbbreviated>WA</geoplugin_regionAbbreviated>
<geoplugin_latitude>47.6739900</geoplugin_latitude>
<geoplugin_longitude>-122.1215100</geoplugin_longitude>
<geoplugin_distanceMiles>0.07</geoplugin_distanceMiles>
<geoplugin_distanceKilometers>0.11</geoplugin_distanceKilometers>
</geoPlugin>
但是当我在我的应用程序中尝试使用它时,它返回了一个空的xml文件。 这是代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
GeoCoordinateWatcher pos = new GeoCoordinateWatcher();
pos.Start();
var mypos = pos.Position;
pos.Stop();
double latitude = 47.674;
double longitude = -122.12;
if (!mypos.Location.IsUnknown)
{
latitude = mypos.Location.Latitude;
longitude = mypos.Location.Longitude;
}
WebRequest.RegisterPrefix("http://www.geoplugin.net/extras", WebRequestCreator.ClientHttp);
Uri req = new Uri(string.Format("http://www.geoplugin.net/extras/location.gp?lat={0}&long={1}&format=xml", latitude, longitude));
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(req);
}
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
XmlReader reader = XmlReader.Create(e.Result);
reader.ReadStartElement("geoplugin_place");
textBox1.Text = reader.ReadContentAsString();
}
}
它给出了一个例外:找不到元素'geoplugin_place'。第2行,第2位。
我的代码或服务有什么问题吗? 或者,如果有任何其他服务可以用来获得相同的结果,请告诉我。
答案 0 :(得分:0)
我放弃了geoplugin api并使用了Bing地图api,它运行正常。