-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlacesService.cs
77 lines (68 loc) · 2.38 KB
/
PlacesService.cs
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
77
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PlacesApi.Model;
using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace PlacesApi
{
public class PlacesService
{
private static string baseUrl = "http://places.nlp.nokia.com/places/v1/discover/around";
private string appId;
private string appToken;
public PlacesService(string appId, string appToken)
{
this.appId = appId;
this.appToken = appToken;
}
public async Task<List<Place>> ListPlacesAroundLocation(GeoCoordinate coordinate)
{
var rawPlaces = await this.GetRawPlaces(coordinate);
Response response = JsonConvert.DeserializeObject<Response>(rawPlaces);
return (from Place p in response.Results.Places
orderby p.Distance ascending
where p.Position != null
select p).ToList(); // Order By Distance
}
private Task<string> GetRawPlaces(GeoCoordinate coordinate)
{
var tcs = new TaskCompletionSource<string>();
var client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
if (e.Error == null)
{
tcs.SetResult(e.Result);
}
else
{
tcs.SetException(e.Error);
}
};
client.DownloadStringAsync(this.GetPlaceQuery(coordinate));
return tcs.Task;
}
private Uri GetPlaceQuery(GeoCoordinate coordinate)
{
return new Uri(string.Format("{0}?app_id={1}&app_code={2}&at={3},{4};u=100&size=100&tf=plain",
baseUrl,
appId,
appToken,
coordinate.Latitude.ToString(CultureInfo.InvariantCulture),
coordinate.Longitude.ToString(CultureInfo.InvariantCulture)));
}
}
}