“Get out from smog” – obtaining precise location

Get out from smog – part III

To measure the distance from the measuring station to current locaction of the user. Is required accurate data such as – latitude and logitude measurement station. Google provide useful api. I send a request to the address https://maps.googleapis.com/maps/api/geocode/json?address= and my adress station and api return me json with coordinates. Examples: I want to know where is Wawel Dragon. Adress to api: https://maps.googleapis.com/maps/api/geocode/json?address=krakowsmokwawelski. You can see exact coordinates in JSON.

Return to coding

Now i must parse JSON again… OK. It’s time to make universal extenstion class for parsing JSON.It will be look like:

    public static class JsonExtensions
    {
        public static List<JToken> FindTokens(this JToken containerToken, string name)
        {
            List<JToken> matches = new List<JToken>();
            FindTokens(containerToken, name, matches);
            return matches;
        }
        private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
        {
            if (containerToken.Type == JTokenType.Object)
            {
                foreach (JProperty child in containerToken.Children<JProperty>())
                {
                    if (child.Name == name)
                    {
                        matches.Add(child.Value);
                    }
                    FindTokens(child.Value, name, matches);
                }
            }
            else if (containerToken.Type == JTokenType.Array)
            {
                foreach (JToken child in containerToken.Children())
                {
                    FindTokens(child, name, matches);
                }
            }
        }
    }

Simple class which return me JToken which currenlty needs. Let’s see it on action.

public class GetInfoAboutLongitude
{
    public List<ParseJsonToList> GetLongitudeAfterAdress(List<ParseJsonToList> adress)
    {
        var webClient = new System.Net.WebClient { Encoding = System.Text.Encoding.UTF8 };
        foreach (var item in adress)
        {
            var json = webClient.DownloadString(@"https://maps.googleapis.com/maps/api/geocode/json?address=" + item.StationLocation);
            var arrayJson = JObject.Parse(json);
            foreach (JToken latidute in arrayJson.FindTokens("location"))
            {
                item.LatAndLong = new LatitudesLongitudes
                {
                    Latitudes = latidute.FindTokens("lat").FirstOrDefault()?.ToString(),
                    Longitudes = latidute.FindTokens("lng").FirstOrDefault()?.ToString(),
                };
            }
        }
        return adress;
    }
}

As you can see I used it to get location node and then obtain information about latitudes and longsitudes.

Method GetLongitudeAfterAdress gets the data from the class which is responsible for parsing the results of the measurement station and adds to them the latitudes and longitudes.

Also I added LatitudesLongitudes model class to storage information about coordinates.

And it looks like eveything is ok. 😉

Get coordinate json

Summary

Application can now download the measuring station and, based on thier address, download thier latitudes and longitudes. The next step will be finding the nearest measuring station from the current location of the user.

Link to the project: HERE

2 Comments on ““Get out from smog” – obtaining precise location”

  1. Don’t you feel that restriction for max 2,500 request per day will not be sufficient?
    I used free Nominatim API in my project to omit this restriction.

    1. For now 2,500, it’s enough for me. But thank you for information about this API. I’ll try it for sure in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *