aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Web/Formatters/ProtoBufFormatter.cs
blob: eee35ec6bae43725e2f251ae56d90642ff64aa89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Tango.AutoComplete.Editors;
using Tango.Logging;

namespace Tango.MachineStudio.UsersAndRoles.Providers
{
    public class PlacesProvider : ISuggestionProvider
    {
        public IEnumerable GetSuggestions(string filter)
        {
            List<Place> places = new List<Place>();

            using (WebClient web = new WebClient())
            {
                try
                {
                    String json = null;

                    web.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
                    web.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                    web.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US");

                    json = web.DownloadString(String.Format("https://nominatim.openstreetmap.org/search?q={0}&format=json&addressdetails=1", filter));

                    if (json != null)
                    {
                        List<Place> results = JsonConvert.DeserializeObject<List<Place>>(json);
                        places.AddRange(results);
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Default.Log(ex, LogCategory.Debug);
                }
            }

            return places;
        }
    }
}