-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathLocationCardBuilder.cs
95 lines (79 loc) · 3.55 KB
/
LocationCardBuilder.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using Bot.Builder.Community.Dialogs.Location.Azure;
using Bot.Builder.Community.Dialogs.Location.Bing;
using Microsoft.Bot.Schema;
namespace Bot.Builder.Community.Dialogs.Location
{
public class LocationCardBuilder : ILocationCardBuilder
{
private readonly string apiKey;
private readonly LocationResourceManager resourceManager;
private readonly bool useAzureMaps = true;
/// <summary>
/// Initializes a new instance of the <see cref="LocationCardBuilder"/> class.
/// </summary>
/// <param name="apiKey">The geo spatial API key.</param>
public LocationCardBuilder(string apiKey, LocationResourceManager resourceManager)
{
//SetField.NotNull(out this.apiKey, nameof(apiKey), apiKey);
//SetField.NotNull(out this.resourceManager, nameof(resourceManager), resourceManager);
if (string.IsNullOrEmpty(apiKey))
throw new ArgumentNullException(nameof(apiKey));
if (resourceManager == null)
throw new ArgumentNullException(nameof(resourceManager));
this.apiKey = apiKey;
this.resourceManager = resourceManager;
if (!string.IsNullOrEmpty(this.apiKey) && this.apiKey.Length > 60)
{
useAzureMaps = false;
}
}
/// <summary>
/// Creates locations hero cards.
/// </summary>
/// <param name="locations">List of the locations.</param>
/// <param name="alwaysShowNumericPrefix">Indicates whether a list containing exactly one location should have a '1.' prefix in its label.</param>
/// <param name="locationNames">List of strings that can be used as names or labels for the locations.</param>
/// <returns>The locations card as a list.</returns>
public IEnumerable<HeroCard> CreateHeroCards(IList<Bing.Location> locations, bool alwaysShowNumericPrefix = false, IList<string> locationNames = null)
{
var cards = new List<HeroCard>();
int i = 1;
foreach (var location in locations)
{
try
{
string nameString = locationNames == null ? string.Empty : $"{locationNames[i - 1]}: ";
string locationString = $"{nameString}{location.GetFormattedAddress(resourceManager.AddressSeparator)}";
string address = alwaysShowNumericPrefix || locations.Count > 1 ? $"{i}. {locationString}" : locationString;
var heroCard = new HeroCard
{
Subtitle = address
};
if (location.Point != null)
{
IGeoSpatialService geoService;
if (useAzureMaps)
{
geoService = new AzureMapsSpatialService(apiKey);
}
else
{
geoService = new BingGeoSpatialService(apiKey);
}
var image =
new CardImage(
url: geoService.GetLocationMapImageUrl(location, i));
heroCard.Images = new[] { image };
}
cards.Add(heroCard);
i++;
}
catch(Exception)
{ }
}
return cards;
}
}
}