-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathBingMapsSearchProvider.ts
More file actions
188 lines (161 loc) · 5.37 KB
/
BingMapsSearchProvider.ts
File metadata and controls
188 lines (161 loc) · 5.37 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import i18next from "i18next";
import { action, makeObservable, override, runInAction } from "mobx";
import Rectangle from "terriajs-cesium/Source/Core/Rectangle";
import Resource from "terriajs-cesium/Source/Core/Resource";
import defined from "terriajs-cesium/Source/Core/defined";
import {
Category,
SearchAction
} from "../../Core/AnalyticEvents/analyticEvents";
import { loadJsonAbortable } from "../../Core/loadJson";
import { applyTranslationIfExists } from "../../Language/languageHelpers";
import LocationSearchProviderMixin, {
getMapCenter
} from "../../ModelMixins/SearchProviders/LocationSearchProviderMixin";
import BingMapsSearchProviderTraits from "../../Traits/SearchProviders/BingMapsSearchProviderTraits";
import CreateModel from "../Definition/CreateModel";
import Terria from "../Terria";
import CommonStrata from "./../Definition/CommonStrata";
import SearchResult from "./SearchResult";
export default class BingMapsSearchProvider extends LocationSearchProviderMixin(
CreateModel(BingMapsSearchProviderTraits)
) {
static readonly type = "bing-maps-search-provider";
get type() {
return BingMapsSearchProvider.type;
}
constructor(uniqueId: string | undefined, terria: Terria) {
super(uniqueId, terria);
makeObservable(this);
runInAction(() => {
if (this.terria.configParameters.bingMapsKey) {
this.setTrait(
CommonStrata.defaults,
"key",
this.terria.configParameters.bingMapsKey
);
}
});
}
@override
override showWarning() {
if (!this.key || this.key === "") {
console.warn(
`The ${applyTranslationIfExists(this.name, i18next)}(${
this.type
}) geocoder will always return no results because a Bing Maps key has not been provided. Please get a Bing Maps key from bingmapsportal.com and add it to parameters.bingMapsKey in config.json.`
);
}
}
protected logEvent(searchText: string) {
this.terria.analytics?.logEvent(
Category.search,
SearchAction.bing,
searchText
);
}
@action
protected async doSearch(
searchText: string,
abortSignal: AbortSignal
): Promise<void> {
this.searchResult.clear();
const searchQuery = new Resource({
url: this.url + "REST/v1/Locations",
queryParameters: {
culture: this.culture,
query: searchText,
key: this.key,
maxResults: this.maxResults
}
});
if (this.mapCenter) {
const mapCenter = getMapCenter(this.terria);
searchQuery.appendQueryParameters({
userLocation: `${mapCenter.latitude}, ${mapCenter.longitude}`
});
}
const promise: Promise<any> = loadJsonAbortable(searchQuery, {
abortSignal
});
try {
const result = await promise;
if (abortSignal.aborted) {
return;
}
if (result.resourceSets.length === 0) {
this.searchResult.noResults("translate#viewModels.searchNoLocations");
return;
}
const resourceSet = result.resourceSets[0];
if (resourceSet.resources.length === 0) {
this.searchResult.noResults("translate#viewModels.searchNoLocations");
return;
}
const locations = this.sortByPriority(resourceSet.resources);
this.searchResult.results.push(...locations.primaryCountry);
this.searchResult.results.push(...locations.other);
if (this.searchResult.results.length === 0) {
this.searchResult.noResults("translate#viewModels.searchNoLocations");
}
} catch {
if (abortSignal.aborted) {
return;
}
this.searchResult.errorOccurred();
}
}
protected sortByPriority(resources: any[]) {
const primaryCountryLocations: any[] = [];
const otherLocations: any[] = [];
// Locations in the primary country go on top, locations elsewhere go undernearth and we add
// the country name to them.
for (let i = 0; i < resources.length; ++i) {
const resource = resources[i];
let name = resource.name;
if (!defined(name)) {
continue;
}
let list = primaryCountryLocations;
let isImportant = true;
const country = resource.address
? resource.address.countryRegion
: undefined;
if (defined(this.primaryCountry) && country !== this.primaryCountry) {
// Add this location to the list of other locations.
list = otherLocations;
isImportant = false;
// Add the country to the name, if it's not already there.
if (
defined(country) &&
name.lastIndexOf(country) !== name.length - country.length
) {
name += ", " + country;
}
}
list.push(
new SearchResult({
name: name,
isImportant: isImportant,
clickAction: createZoomToFunction(this, resource),
location: {
latitude: resource.point.coordinates[0],
longitude: resource.point.coordinates[1]
}
})
);
}
return {
primaryCountry: primaryCountryLocations,
other: otherLocations
};
}
}
function createZoomToFunction(model: BingMapsSearchProvider, resource: any) {
const [south, west, north, east] = resource.bbox;
const rectangle = Rectangle.fromDegrees(west, south, east, north);
return function () {
const terria = model.terria;
terria.currentViewer.zoomTo(rectangle, model.flightDurationSeconds);
};
}