aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.UsersAndRoles/Providers/PlacesProvider.cs
blob: 681815d4c73f56342524f40eb6d37b4a99006e59 (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
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;
        }
    }
}