CartographicGeocoderService.js 1.9 KB

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