0
public void FindCityName()
        {
            string url = "http://maps.google.com/maps/geo?q=39.920794,32.853902&output=json&oe=utf8&sensor=true&key=MYKEY";

            var w = new WebClient();
            Observable.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted").Subscribe(r =>
                {
                    var deserialized = JsonConvert.DeserializeObject<RootObject>(r.EventArgs.Result);


                    string s = deserialized.Placemark[0].AddressDetails.Country.SubAdministrativeArea.Locality.LocalityName;
/// setCity() and City=s produce the same thing
                    setCity(s);
                    City = s;
                    //foreach (var item in deserialized.Placemark)
                    //{
                    //    //MessageBox.Show(item.AddressDetails.Country.SubAdministrativeArea.Locality.LocalityName);
                    //    City = (string)item.AddressDetails.Country.SubAdministrativeArea.Locality.LocalityName;
                    //}
//Problem here >>>>>
                    ////MessageBox.Show(City);
                });
            w.DownloadStringAsync(new Uri(url)); 
        }

问题:

我正在开发一个 windows phone 7 应用程序,我需要从 GPS 坐标中找到“城市名称”才能继续前进......

我在互联网上找到了上面的代码并尝试了它。我可以通过使用这些代码查看城市名称(Message.Box(City) 显示我想要的,城市名称)。但是,这行代码

deserialized.Placemark[0].AddressDetails.Country.SubAdministrativeArea.Locality.LocalityName;

这给了我城市名称似乎给出了一个不稳定的字符串值。

例如,我创建了一个方法,它将字符串变量“s”的值分配给我的类的字符串字段,名称为 City。如果我在调用 FindCityName() 方法后尝试获取 City 的内容,我会看到 City 的内容没有更新。

同样,同样的事情发生了,然后我在注释“这里的问题 >>>>>”下调用代码行,MessageBox.Show(City) 没有显示任何新内容......

有人可以解释我的问题的原因吗?

4

2 回答 2

1

你也把这个问题放在我的博客上,但我会在这里回答。首先,我觉得有一点责任把示例代码放在首位;-)

我将假设包含您的代码的类如下所示:

public class MyClass
{
    private void MyMethod()
    {
      FindCityName();
      MessageBox.Show(City);
    }

    private void FindCityName()
    {
      // Code omitted - see your question
    }

    private string City;
}

字符串没有任何易失性。您的问题是异步性。如果你仔细看,你会发现我使用了一个在触发 DownloadStringCompleted 时触发的 observable。Observable.Event 中的代码仅在下载完成时调用,但这是异步发生的。但我假设您要做的是调用 FindCityName 方法,然后直接尝试访问结果,就像我在 MyMethod 方法中显示的那样。这就像在触发请求后直接想要结果一样。结果还没出来!这就像一个网页下载 - 需要一段时间。您可以通过回调来解决这个问题,如下所示:

public class MyClass
{
    private void MyMethod()
    {
      FindName();       
    }

    public void FindCityName()
    {
        string url = "http://maps.google.com/maps/geo?q=39.920794,32.853902&output=json&oe=utf8&sensor=true&key=MYKEY";

        var w = new WebClient();
        Observable.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted").Subscribe(r =>
            {
                var deserialized = JsonConvert.DeserializeObject<RootObject>(r.EventArgs.Result);

                City = deserialized.Placemark[0].AddressDetails.Country.SubAdministrativeArea.Locality.LocalityName;
                DoneDownloading();
            });
        w.DownloadStringAsync(new Uri(url)); 
    }

    private string City;

    private void DoneDownloading
    {
        MessageBox.Show(City);
    }
}

这有帮助吗?

于 2012-08-19T14:20:37.080 回答
0

我会推荐你​​使用这个谷歌地图 API

http://maps.googleapis.com/maps/api/geocode/json?latlng=39.920794,32.853902&sensor=true

一旦您在请求中获得 JSON 响应。您可以使用 NEWTONSOFT for wp7 轻松解析

WebClient wc = new WebClient();
var json = (JObject)JsonConvert.DeserializeObject(wc.DownloadString(url));

var locality= json["results"]
                .SelectMany(x => x["address_components"])
                .FirstOrDefault(t => t["types"].First().ToString() == "locality");

var name = locality!=null ? locality["long_name"].ToString() : "";
于 2012-08-16T04:49:51.327 回答