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)); 
        }

Problem:

I am working on a windows phone 7 application and I need to find the "City Name" from GPS coordinates in order to move forward...

I found the code above on the internet and tried it. I can see the city name by using these codes(Message.Box(City) show exactly what I want, the city name). However, this line of code

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

which gives me the city name seems to give a volatile string value.

For example, I created a method which assigns the value of string variable "s" to the string field of my class, name City. If I try to get the City's content after calling FindCityName() method, I see that City's content is not updated.

Again, same thing happens then I call the code line under the comment "Problem here >>>>>" that MessageBox.Show(City) shows nothing new...

Can someone explain me the reason of my problem?

2 Answers 2

1

you put this question on my blog as well, but I will answer it here. I feel a bit responsible for putting up the sample code in the first place ;-)

I am going to assume the class containing your code looks like this:

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

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

    private string City;
}

There is nothing volatile about the string. Your problem is asynchronicity. If you look carefully you will see that I use an observable that fires when the DownloadStringCompleted is fired. The code inside Observable.Event is only called when the download is finished but that happens asynchronously. But what I assume you do is call the FindCityName method and then directly trying to access results like I show in the MyMethod method. That's like directly wanting the result after firing the request. The results are not in yet! It's like a web page downloading - it takes a while. You can fix that with a callback, something like this:

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);
    }
}

Does that help?

Sign up to request clarification or add additional context in comments.

2 Comments

I solved the problem by using breakpoints on every method and reorganized my method calls as you did. Many thanks :)
@LocalJoost I saw your blog and it sure helped me. I was stuck at the same issue as the OP. Thanks! For other guys who are looking for the blog, its here - dotnetbyexample.blogspot.gr/2012/01/…
0

I would recommend you to use this Google Map API

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

And once you get JSON response in your request. You can parse easily with 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() : "";

1 Comment

WebClient wc does not have a method called DownloadString(...). I added Newtonsoft.Json to my references...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.