CartographicGeocoderService.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. /**
  12. * @function
  13. *
  14. * @param {String} query The query to be sent to the geocoder service
  15. * @returns {Promise<GeocoderService.Result[]>}
  16. */
  17. CartographicGeocoderService.prototype.geocode = function (query) {
  18. //>>includeStart('debug', pragmas.debug);
  19. Check.typeOf.string("query", query);
  20. //>>includeEnd('debug');
  21. const splitQuery = query.match(/[^\s,\n]+/g);
  22. if (splitQuery.length === 2 || splitQuery.length === 3) {
  23. let longitude = +splitQuery[0];
  24. let latitude = +splitQuery[1];
  25. const height = splitQuery.length === 3 ? +splitQuery[2] : 300.0;
  26. if (isNaN(longitude) && isNaN(latitude)) {
  27. const coordTest = /^(\d+.?\d*)([nsew])/i;
  28. for (let i = 0; i < splitQuery.length; ++i) {
  29. const splitCoord = splitQuery[i].match(coordTest);
  30. if (coordTest.test(splitQuery[i]) && splitCoord.length === 3) {
  31. if (/^[ns]/i.test(splitCoord[2])) {
  32. latitude = /^[n]/i.test(splitCoord[2])
  33. ? +splitCoord[1]
  34. : -splitCoord[1];
  35. } else if (/^[ew]/i.test(splitCoord[2])) {
  36. longitude = /^[e]/i.test(splitCoord[2])
  37. ? +splitCoord[1]
  38. : -splitCoord[1];
  39. }
  40. }
  41. }
  42. }
  43. if (!isNaN(longitude) && !isNaN(latitude) && !isNaN(height)) {
  44. const result = {
  45. displayName: query,
  46. destination: Cartesian3.fromDegrees(longitude, latitude, height),
  47. };
  48. return Promise.resolve([result]);
  49. }
  50. }
  51. return Promise.resolve([]);
  52. };
  53. export default CartographicGeocoderService;