-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathGeocoderViewModel.js
307 lines (275 loc) · 9.76 KB
/
GeocoderViewModel.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*global define*/
define([
'../../Core/BingMapsApi',
'../../Core/Cartesian3',
'../../Core/defaultValue',
'../../Core/defined',
'../../Core/defineProperties',
'../../Core/DeveloperError',
'../../Core/Event',
'../../Core/jsonp',
'../../Core/Matrix4',
'../../Core/Rectangle',
'../../ThirdParty/knockout',
'../../ThirdParty/when',
'../createCommand'
], function(
BingMapsApi,
Cartesian3,
defaultValue,
defined,
defineProperties,
DeveloperError,
Event,
jsonp,
Matrix4,
Rectangle,
knockout,
when,
createCommand) {
"use strict";
/**
* The view model for the {@link Geocoder} widget.
* @alias GeocoderViewModel
* @constructor
*
* @param {Object} options Object with the following properties:
* @param {Scene} options.scene The Scene instance to use.
* @param {String} [options.url='//dev.virtualearth.net'] The base URL of the Bing Maps API.
* @param {String} [options.key] The Bing Maps key for your application, which can be
* created at {@link https://www.bingmapsportal.com}.
* If this parameter is not provided, {@link BingMapsApi.defaultKey} is used.
* If {@link BingMapsApi.defaultKey} is undefined as well, a message is
* written to the console reminding you that you must create and supply a Bing Maps
* key as soon as possible. Please do not deploy an application that uses
* this widget without creating a separate key for your application.
* @param {Number} [options.flightDuration] The duration of the camera flight to an entered location, in seconds.
*/
var GeocoderViewModel = function(options) {
//>>includeStart('debug', pragmas.debug);
if (!defined(options) || !defined(options.scene)) {
throw new DeveloperError('options.scene is required.');
}
//>>includeEnd('debug');
this._url = defaultValue(options.url, '//dev.virtualearth.net/');
if (this._url.length > 0 && this._url[this._url.length - 1] !== '/') {
this._url += '/';
}
this._key = BingMapsApi.getKey(options.key);
this._scene = options.scene;
this._flightDuration = options.flightDuration;
this._searchText = '';
this._isSearchInProgress = false;
this._geocodeInProgress = undefined;
this._complete = new Event();
var that = this;
this._searchCommand = createCommand(function() {
if (that.isSearchInProgress) {
cancelGeocode(that);
} else {
geocode(that);
}
});
knockout.track(this, ['_searchText', '_isSearchInProgress']);
/**
* Gets a value indicating whether a search is currently in progress. This property is observable.
*
* @type {Boolean}
*/
this.isSearchInProgress = undefined;
knockout.defineProperty(this, 'isSearchInProgress', {
get : function() {
return this._isSearchInProgress;
}
});
/**
* Gets or sets the text to search for. The text can be an address, or longitude, latitude,
* and optional height, where longitude and latitude are in degrees and height is in meters.
*
* @type {String}
*/
this.searchText = undefined;
knockout.defineProperty(this, 'searchText', {
get : function() {
if (this.isSearchInProgress) {
return 'Searching...';
}
return this._searchText;
},
set : function(value) {
//>>includeStart('debug', pragmas.debug);
if (typeof value !== 'string') {
throw new DeveloperError('value must be a valid string.');
}
//>>includeEnd('debug');
this._searchText = value;
}
});
/**
* Gets or sets the the duration of the camera flight in seconds.
* A value of zero causes the camera to instantly switch to the geocoding location.
* The duration will be computed based on the distance when undefined.
*
* @type {Number|undefined}
* @default undefined
*/
this.flightDuration = undefined;
knockout.defineProperty(this, 'flightDuration', {
get : function() {
return this._flightDuration;
},
set : function(value) {
//>>includeStart('debug', pragmas.debug);
if (defined(value) && value < 0) {
throw new DeveloperError('value must be positive.');
}
//>>includeEnd('debug');
this._flightDuration = value;
}
});
};
defineProperties(GeocoderViewModel.prototype, {
/**
* Gets the Bing maps url.
* @memberof GeocoderViewModel.prototype
*
* @type {String}
*/
url : {
get : function() {
return this._url;
}
},
/**
* Gets the Bing maps key.
* @memberof GeocoderViewModel.prototype
*
* @type {String}
*/
key : {
get : function() {
return this._key;
}
},
/**
* Gets the event triggered on flight completion.
* @memberof GeocoderViewModel.prototype
*
* @type {Event}
*/
complete : {
get : function() {
return this._complete;
}
},
/**
* Gets the scene to control.
* @memberof GeocoderViewModel.prototype
*
* @type {Scene}
*/
scene : {
get : function() {
return this._scene;
}
},
/**
* Gets the Command that is executed when the button is clicked.
* @memberof GeocoderViewModel.prototype
*
* @type {Command}
*/
search : {
get : function() {
return this._searchCommand;
}
}
});
function updateCamera(viewModel, position) {
if (viewModel._flightDuration === 0) {
viewModel._scene.camera.setView({position: position});
viewModel._complete.raiseEvent();
} else {
viewModel._scene.camera.flyTo({
destination : position,
complete: function() {
viewModel._complete.raiseEvent();
},
duration : viewModel._flightDuration,
endTransform : Matrix4.IDENTITY,
convert : false
});
}
}
function geocode(viewModel) {
var query = viewModel.searchText;
if (/^\s*$/.test(query)) {
//whitespace string
return;
}
// If the user entered (longitude, latitude, [height]) in degrees/meters,
// fly without calling the geocoder.
var splitQuery = query.match(/[^\s,\n]+/g);
if ((splitQuery.length === 2) || (splitQuery.length === 3)) {
var longitude = +splitQuery[0];
var latitude = +splitQuery[1];
var height = (splitQuery.length === 3) ? +splitQuery[2] : 300.0;
if (!isNaN(longitude) && !isNaN(latitude) && !isNaN(height)) {
updateCamera(viewModel, Cartesian3.fromDegrees(longitude, latitude, height));
return;
}
}
viewModel._isSearchInProgress = true;
var promise = jsonp(viewModel._url + 'REST/v1/Locations', {
parameters : {
query : query,
key : viewModel._key
},
callbackParameterName : 'jsonp'
});
var geocodeInProgress = viewModel._geocodeInProgress = when(promise, function(result) {
if (geocodeInProgress.cancel) {
return;
}
viewModel._isSearchInProgress = false;
if (result.resourceSets.length === 0) {
viewModel.searchText = viewModel._searchText + ' (not found)';
return;
}
var resourceSet = result.resourceSets[0];
if (resourceSet.resources.length === 0) {
viewModel.searchText = viewModel._searchText + ' (not found)';
return;
}
var resource = resourceSet.resources[0];
viewModel._searchText = resource.name;
var bbox = resource.bbox;
var south = bbox[0];
var west = bbox[1];
var north = bbox[2];
var east = bbox[3];
var rectangle = Rectangle.fromDegrees(west, south, east, north);
var camera = viewModel._scene.camera;
var position = camera.getRectangleCameraCoordinates(rectangle);
if (!defined(position)) {
// This can happen during a scene mode transition.
return;
}
updateCamera(viewModel, position);
}, function() {
if (geocodeInProgress.cancel) {
return;
}
viewModel._isSearchInProgress = false;
viewModel.searchText = viewModel._searchText + ' (error)';
});
}
function cancelGeocode(viewModel) {
viewModel._isSearchInProgress = false;
if (defined(viewModel._geocodeInProgress)) {
viewModel._geocodeInProgress.cancel = true;
viewModel._geocodeInProgress = undefined;
}
}
return GeocoderViewModel;
});