CartographicGeocoderService.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Check from "./Check.js";
  3. /**
  4. * Geocodes queries containing longitude and latitude coordinates and an optional height.
  5. * Query format: `longitude latitude (height)` with longitude/latitude in degrees and height in meters.
  6. *
  7. * @alias CartographicGeocoderService
  8. * @constructor
  9. */
  10. function CartographicGeocoderService() {}
  11. Object.defineProperties(CartographicGeocoderService.prototype, {
  12. /**
  13. * Gets the credit to display after a geocode is performed. Typically this is used to credit
  14. * the geocoder service.
  15. * @memberof CartographicGeocoderService.prototype
  16. * @type {Credit|undefined}
  17. * @readonly
  18. */
  19. credit: {
  20. get: function () {
  21. return undefined;
  22. },
  23. },
  24. });
  25. /**
  26. * @function
  27. *
  28. * @param {string} query The query to be sent to the geocoder service
  29. * @returns {Promise<GeocoderService.Result[]>}
  30. */
  31. CartographicGeocoderService.prototype.geocode = function (query) {
  32. //>>includeStart('debug', pragmas.debug);
  33. Check.typeOf.string("query", query);
  34. //>>includeEnd('debug');
  35. const splitQuery = query.match(/[^\s,\n]+/g);
  36. if (splitQuery.length === 2 || splitQuery.length === 3) {
  37. let longitude = +splitQuery[0];
  38. let latitude = +splitQuery[1];
  39. const height = splitQuery.length === 3 ? +splitQuery[2] : 300.0;
  40. if (isNaN(longitude) && isNaN(latitude)) {
  41. const coordTest = /^(\d+.?\d*)([nsew])/i;
  42. for (let i = 0; i < splitQuery.length; ++i) {
  43. const splitCoord = splitQuery[i].match(coordTest);
  44. if (coordTest.test(splitQuery[i]) && splitCoord.length === 3) {
  45. if (/^[ns]/i.test(splitCoord[2])) {
  46. latitude = /^[n]/i.test(splitCoord[2])
  47. ? +splitCoord[1]
  48. : -splitCoord[1];
  49. } else if (/^[ew]/i.test(splitCoord[2])) {
  50. longitude = /^[e]/i.test(splitCoord[2])
  51. ? +splitCoord[1]
  52. : -splitCoord[1];
  53. }
  54. }
  55. }
  56. }
  57. if (!isNaN(longitude) && !isNaN(latitude) && !isNaN(height)) {
  58. const result = {
  59. displayName: query,
  60. destination: Cartesian3.fromDegrees(longitude, latitude, height),
  61. };
  62. return Promise.resolve([result]);
  63. }
  64. }
  65. return Promise.resolve([]);
  66. };
  67. export default CartographicGeocoderService;