Skip to content

Commit

Permalink
Merge pull request #4723 from erikmaarten/improve-geocoder
Browse files Browse the repository at this point in the history
Improve geocoder
  • Loading branch information
mramato authored Jan 5, 2017
2 parents fe099fd + 7c8f5f4 commit 4b612dc
Show file tree
Hide file tree
Showing 13 changed files with 947 additions and 115 deletions.
90 changes: 90 additions & 0 deletions Apps/Sandcastle/gallery/Custom Geocoder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description" content="Cluster labels, billboards and points.">
<meta name="cesium-sandcastle-labels" content="Tutorials,Showcases">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
<script type="text/javascript">
require.config({
baseUrl : '../../../Source',
waitSeconds : 60
});
</script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
@import url(../templates/bucket.css);
#toolbar {
background: rgba(42, 42, 42, 0.8);
padding: 4px;
border-radius: 4px;
}
#toolbar input {
vertical-align: middle;
padding-top: 2px;
padding-bottom: 2px;
}
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
'use strict';
//Sandcastle_Begin

/**
* This class is an example of a custom geocoder. It provides geocoding through the OpenStreetMap Nominatim service.
* @alias OpenStreetMapNominatimGeocoder
* @constructor
*/
function OpenStreetMapNominatimGeocoder() {
}

/**
* The function called to geocode using this geocoder service.
*
* @param {String} input The query to be sent to the geocoder service
* @returns {Promise<GeocoderResult[]>}
*/
OpenStreetMapNominatimGeocoder.prototype.geocode = function (input) {
var endpoint = 'http://nominatim.openstreetmap.org/search?';
var query = 'format=json&q=' + input;
var requestString = endpoint + query;
return Cesium.loadJson(requestString)
.then(function (results) {
var bboxDegrees;
return results.map(function (resultObject) {
bboxDegrees = resultObject.boundingbox;
return {
displayName: resultObject.display_name,
destination: Cesium.Rectangle.fromDegrees(
bboxDegrees[2],
bboxDegrees[0],
bboxDegrees[3],
bboxDegrees[1]
)
};
});
});
};

var viewer = new Cesium.Viewer('cesiumContainer', {
geocoder: new OpenStreetMapNominatimGeocoder()
});

//Sandcastle_End
Sandcastle.finishedLoading();
}
if (typeof Cesium !== "undefined") {
startup(Cesium);
} else if (typeof require === "function") {
require(["Cesium"], startup);
}
</script>
</body>
</html>
9 changes: 7 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ Change Log
==========

### 1.30 - 2017-02-01

* Deprecated
* The properties `url` and `key` will be removed from `GeocoderViewModel` in 1.31. These properties will be available on geocoder services that support them, like `BingMapsGeocoderService`.
* Added support for custom geocoder services and autocomplete [#4723](https://github.com/AnalyticalGraphicsInc/cesium/pull/4723).
* Added [Custom Geocoder Sandcastle example](http://localhost:8080/Apps/Sandcastle/index.html?src=Custom%20Geocoder.html)
* Added `GeocoderService`, an interface for geocoders.
* Added `BingMapsGeocoderService` implementing the `GeocoderService` interface.
* Added `CartographicGeocoderService` implementing the `GeocoderService` interface.
* Updated the morph so the default view in Columbus View is now angled. [#3878](https://github.com/AnalyticalGraphicsInc/cesium/issues/3878)
* Fixed sky atmosphere from causing incorrect picking and hanging drill picking. [#4783](https://github.com/AnalyticalGraphicsInc/cesium/issues/4783) and [#4784](https://github.com/AnalyticalGraphicsInc/cesium/issues/4784)

### 1.29 - 2017-01-02

* Improved 3D Models
* Added the ability to blend a `Model` with a color/translucency. Added `color`, `colorBlendMode`, and `colorBlendAmount` properties to `Model`, `ModelGraphics`, and CZML. Also added `ColorBlendMode` enum. [#4547](https://github.com/AnalyticalGraphicsInc/cesium/pull/4547)
* Added the ability to render a `Model` with a silhouette. Added `silhouetteColor` and `silhouetteSize` properties to `Model`, `ModelGraphics`, and CZML. [#4314](https://github.com/AnalyticalGraphicsInc/cesium/pull/4314)
Expand Down
107 changes: 107 additions & 0 deletions Source/Core/BingMapsGeocoderService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*global define*/
define([
'./BingMapsApi',
'./defaultValue',
'./defined',
'./defineProperties',
'./DeveloperError',
'./loadJsonp',
'./Rectangle'
], function(
BingMapsApi,
defaultValue,
defined,
defineProperties,
DeveloperError,
loadJsonp,
Rectangle) {
'use strict';

var url = 'https://dev.virtualearth.net/REST/v1/Locations';

/**
* Provides geocoding through Bing Maps.
* @alias BingMapsGeocoderService
* @constructor
*
* @param {Object} options Object with the following properties:
* @param {String} [key] A key to use with the Bing Maps geocoding service
* @param {Boolean} autoComplete Indicates whether this service shall be used to fetch auto-complete suggestions
*/
function BingMapsGeocoderService(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
this._url = 'https://dev.virtualearth.net/REST/v1/Locations';
this._key = BingMapsApi.getKey(options.key);
}

defineProperties(BingMapsGeocoderService.prototype, {
/**
* The URL endpoint for the Bing geocoder service
* @type {String}
* @memberof {BingMapsGeocoderService.prototype}
* @readonly
*/
url : {
get : function () {
return this._url;
}
},

/**
* The key for the Bing geocoder service
* @type {String}
* @memberof {BingMapsGeocoderService.prototype}
* @readonly
*/
key : {
get : function () {
return this._key;
}
}
});

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

var key = this.key;
var promise = loadJsonp(url, {
parameters : {
query : query,
key : key
},
callbackParameterName : 'jsonp'
});

return promise.then(function(result) {
if (result.resourceSets.length === 0) {
return [];
}

var results = result.resourceSets[0].resources;

return results.map(function (resource) {
var bbox = resource.bbox;
var south = bbox[0];
var west = bbox[1];
var north = bbox[2];
var east = bbox[3];
return {
displayName: resource.name,
destination: Rectangle.fromDegrees(west, south, east, north)
};
});
});
};

return BingMapsGeocoderService;
});
59 changes: 59 additions & 0 deletions Source/Core/CartographicGeocoderService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*global define*/
define([
'./Cartesian3',
'./defaultValue',
'./defineProperties',
'./defined',
'./DeveloperError',
'../ThirdParty/when'
], function(
Cartesian3,
defaultValue,
defineProperties,
defined,
DeveloperError,
when) {
'use strict';

/**
* Geocodes queries containing longitude and latitude coordinates and an optional height.
* Query format: `longitude latitude (height)` with longitude/latitude in degrees and height in meters.
*
* @alias CartographicGeocoderService
* @constructor
*/
function CartographicGeocoderService() {
}

/**
* @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');

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)) {
var result = {
displayName: query,
destination: Cartesian3.fromDegrees(longitude, latitude, height)
};
return when.resolve([result]);
}
}
return when.resolve([]);
};

return CartographicGeocoderService;
});
36 changes: 36 additions & 0 deletions Source/Core/GeocoderService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*global define*/
define([
'./defineProperties',
'./DeveloperError'
], function(
defineProperties,
DeveloperError) {
'use strict';

/**
* @typedef {Object} GeocoderResult
* @property {String} displayName The display name for a location
* @property {Rectangle|Cartesian3} destination The bounding box for a location
*/

/**
* Provides geocoding through an external service. This type describes an interface and
* is not intended to be used.
* @alias GeocoderService
* @constructor
*
* @see BingMapsGeocoderService
*/
function GeocoderService() {
}

/**
* @function
*
* @param {String} query The query to be sent to the geocoder service
* @returns {Promise<GeocoderResult[]>}
*/
GeocoderService.prototype.geocode = DeveloperError.throwInstantiationError;

return GeocoderService;
});
27 changes: 27 additions & 0 deletions Source/Widgets/Geocoder/Geocoder.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@
width: 250px;
}

.cesium-viewer-geocoderContainer .search-results {
position: absolute;
background-color: #000;
color: #eee;
overflow-y: auto;
opacity: 0.8;
width: 100%;
}

.cesium-viewer-geocoderContainer .search-results ul {
list-style-type: none;
margin: 0;
padding: 0;
}

.cesium-viewer-geocoderContainer .search-results ul li {
font-size: 14px;
padding: 3px 10px;
}
.cesium-viewer-geocoderContainer .search-results ul li:hover {
cursor: pointer;
}

.cesium-viewer-geocoderContainer .search-results ul li.active {
background: #48b;
}

.cesium-geocoder-searchButton {
background-color: #303336;
display: inline-block;
Expand Down
Loading

0 comments on commit 4b612dc

Please sign in to comment.