Skip to content

Commit

Permalink
chain geocoder promises
Browse files Browse the repository at this point in the history
  • Loading branch information
hpinkos committed Dec 16, 2016
1 parent 2659c7f commit f10c75f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 143 deletions.
34 changes: 14 additions & 20 deletions Source/Core/BingMapsGeocoderService.js
Expand Up @@ -2,13 +2,17 @@
define([
'./BingMapsApi',
'./defaultValue',
'./defined',
'./defineProperties',
'./DeveloperError',
'./loadJsonp',
'./Rectangle',
'./Rectangle'
], function(
BingMapsApi,
defaultValue,
defined,
defineProperties,
DeveloperError,
loadJsonp,
Rectangle) {
'use strict';
Expand All @@ -26,23 +30,16 @@ define([
*/
function BingMapsGeocoderService(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
this._canceled = false;
this._url = 'https://dev.virtualearth.net/REST/v1/Locations';
this._key = BingMapsApi.getKey(options.key);

/**
* Indicates whether this geocoding service is to be used for autocomplete.
*
* @type {boolean}
* @default false
*/
this.autoComplete = defaultValue(options.autoComplete, false);
}

defineProperties(BingMapsGeocoderService.prototype, {
/**
* The URL endpoint for the Bing geocoder service
* @type {String}
* @memberof {BingMapsGeocoderService.prototype}
* @readonly
*/
url : {
get : function () {
Expand All @@ -53,6 +50,8 @@ define([
/**
* The key for the Bing geocoder service
* @type {String}
* @memberof {BingMapsGeocoderService.prototype}
* @readonly
*/
key : {
get : function () {
Expand All @@ -61,18 +60,18 @@ define([
}
});

BingMapsGeocoderService.prototype.cancel = function() {
this._canceled = true;
};

/**
* @function
*
* @param {String} query The query to be sent to the geocoder service
* @returns {Promise<GeocoderResult[]>}
*/
BingMapsGeocoderService.prototype.geocode = function(query) {
this._canceled = false;
//>>includeStart('debug', pragmas.debug);
if (!defined(query)) {
throw new DeveloperError('query must be defined');
}
//>>includeEnd('debug');

var key = this.key;
var promise = loadJsonp(url, {
Expand All @@ -83,12 +82,7 @@ define([
callbackParameterName : 'jsonp'
});

var that = this;

return promise.then(function(result) {
if (that._canceled) {
return;
}
if (result.resourceSets.length === 0) {
return [];
}
Expand Down
31 changes: 10 additions & 21 deletions Source/Core/CartographicGeocoderService.js
Expand Up @@ -3,11 +3,15 @@ define([
'./Cartesian3',
'./defaultValue',
'./defineProperties',
'./defined',
'./DeveloperError',
'../ThirdParty/when'
], function(
Cartesian3,
defaultValue,
defineProperties,
defined,
DeveloperError,
when) {
'use strict';

Expand All @@ -19,36 +23,21 @@ define([
* @constructor
*/
function CartographicGeocoderService() {
this._autoComplete = false;
}

defineProperties(CartographicGeocoderService.prototype, {
/**
* This geocoder does not support autocomplete, so this property will always be false.
*
* @type {boolean}
*/
autoComplete : {
get : function () {
return this._autoComplete;
}
}
});

/**
* This service completes geocoding synchronously and therefore does not
* need to handle canceled requests that have not finished yet.
*/
CartographicGeocoderService.prototype.cancel = function() {
};

/**
* @function
*
* @param {String} query The query to be sent to the geocoder service
* @returns {Promise<GeocoderResult[]>}
*/
CartographicGeocoderService.prototype.geocode = function(query) {
//>>includeStart('debug', pragmas.debug);
if (!defined(query)) {
throw new DeveloperError('query must be defined');
}
//>>includeEnd('debug');

try {
var splitQuery = query.match(/[^\s,\n]+/g);
if ((splitQuery.length === 2) || (splitQuery.length === 3)) {
Expand Down
16 changes: 0 additions & 16 deletions Source/Core/GeocoderService.js
Expand Up @@ -22,13 +22,6 @@ define([
* @see BingMapsGeocoderService
*/
function GeocoderService() {
/**
* Indicates whether this geocoding service is to be used for autocomplete.
*
* @type {boolean}
* @default false
*/
this.autoComplete = false;
}

/**
Expand All @@ -39,14 +32,5 @@ define([
*/
GeocoderService.prototype.geocode = DeveloperError.throwInstantiationError;

/**
* A function that is called when geocoding is canceled by the user, so that the
* geocoding service can stop processing current requests.
* @function
*
* @returns {undefined}
*/
GeocoderService.prototype.cancel = DeveloperError.throwInstantiationError;

return GeocoderService;
});
1 change: 1 addition & 0 deletions Source/Widgets/Geocoder/Geocoder.css
Expand Up @@ -40,6 +40,7 @@
.cesium-viewer-geocoderContainer .search-results {
position: absolute;
background-color: #000;
color: #eee;
overflow-y: auto;
opacity: 0.8;
width: 100%;
Expand Down
3 changes: 2 additions & 1 deletion Source/Widgets/Geocoder/Geocoder.js
Expand Up @@ -32,7 +32,8 @@ define([
* @param {Object} options Object with the following properties:
* @param {Element|String} options.container The DOM element or ID that will contain the widget.
* @param {Scene} options.scene The Scene instance to use.
* @param {Object} [options.geocoderServices] The geocoder services to be used
* @param {GeocoderService[]} [options.geocoderServices] The geocoder services to be used
* @param {Boolean} [options.autoComplete = true] True if the geocoder should query as the user types to autocomplete
* @param {String} [options.url='https://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}.
Expand Down

0 comments on commit f10c75f

Please sign in to comment.