CartographicGeocoderService.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import when from '../ThirdParty/when.js';
  2. import Cartesian3 from './Cartesian3.js';
  3. import Check from './Check.js';
  4. /**
  5. * Geocodes queries containing longitude and latitude coordinates and an optional height.
  6. * Query format: `longitude latitude (height)` with longitude/latitude in degrees and height in meters.
  7. *
  8. * @alias CartographicGeocoderService
  9. * @constructor
  10. */
  11. function CartographicGeocoderService() {
  12. }
  13. /**
  14. * @function
  15. *
  16. * @param {String} query The query to be sent to the geocoder service
  17. * @returns {Promise<GeocoderService~Result[]>}
  18. */
  19. CartographicGeocoderService.prototype.geocode = function(query) {
  20. //>>includeStart('debug', pragmas.debug);
  21. Check.typeOf.string('query', query);
  22. //>>includeEnd('debug');
  23. var splitQuery = query.match(/[^\s,\n]+/g);
  24. if ((splitQuery.length === 2) || (splitQuery.length === 3)) {
  25. var longitude = +splitQuery[0];
  26. var latitude = +splitQuery[1];
  27. var height = (splitQuery.length === 3) ? +splitQuery[2] : 300.0;
  28. if (isNaN(longitude) && isNaN(latitude)) {
  29. var coordTest = /^(\d+.?\d*)([nsew])/i;
  30. for (var i = 0; i < splitQuery.length; ++i) {
  31. var splitCoord = splitQuery[i].match(coordTest);
  32. if (coordTest.test(splitQuery[i]) && splitCoord.length === 3) {
  33. if (/^[ns]/i.test(splitCoord[2])) {
  34. latitude = (/^[n]/i.test(splitCoord[2])) ? +splitCoord[1] : -splitCoord[1];
  35. } else if (/^[ew]/i.test(splitCoord[2])) {
  36. longitude = (/^[e]/i.test(splitCoord[2])) ? +splitCoord[1] : -splitCoord[1];
  37. }
  38. }
  39. }
  40. }
  41. if (!isNaN(longitude) && !isNaN(latitude) && !isNaN(height)) {
  42. var result = {
  43. displayName: query,
  44. destination: Cartesian3.fromDegrees(longitude, latitude, height)
  45. };
  46. return when.resolve([result]);
  47. }
  48. }
  49. return when.resolve([]);
  50. };
  51. export default CartographicGeocoderService;