Source: map-openstreetmap/src/main/resources/META-INF/resources/js/OpenStreetMapGeocoder.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import State from 'metal-state';
  15. /**
  16. * OpenStreetMapGeocoder
  17. */
  18. class OpenStreetMapGeocoder extends State {
  19. /**
  20. * Handles the server response of a successfull address forward
  21. * @param {Object} response Server response
  22. * @param {function} callback Callback that will be executed on success
  23. * @protected
  24. * @review
  25. */
  26. _handleForwardJSONP(response, callback) {
  27. callback(response);
  28. }
  29. /**
  30. * Handles the server response of a successfull location reverse
  31. * @param {Object} response Server response
  32. * @param {function} callback Callback that will be executed on success
  33. * @protected
  34. * @review
  35. */
  36. _handleReverseJSONP({display_name, error, lat, lon}, callback) {
  37. const result = {
  38. data: {},
  39. error,
  40. };
  41. if (!result.err) {
  42. result.data = {
  43. address: display_name,
  44. location: {
  45. lat: parseFloat(lat) || 0,
  46. lng: parseFloat(lon) || 0,
  47. },
  48. };
  49. }
  50. callback(result);
  51. }
  52. /**
  53. * Transforms a given address into valid latitude and longitude
  54. * @param {string} query Address to be transformed into latitude and longitude
  55. * @param {function} callback Callback that will be executed on success
  56. * @review
  57. */
  58. forward(query, callback) {
  59. AUI().use('jsonp', (A) => {
  60. const forwardUrl = OpenStreetMapGeocoder.TPL_FORWARD_GEOCODING_URL.replace(
  61. '{query}',
  62. query
  63. );
  64. A.jsonp(forwardUrl, {
  65. context: this,
  66. on: {
  67. success: A.rbind('_handleForwardJSONP', this, callback),
  68. },
  69. });
  70. });
  71. }
  72. /**
  73. * Transforms a given location object (lat, lng) into a valid address
  74. * @param {string} location Location information to be sent to the server
  75. * @param {function} callback Callback that will be executed on success
  76. * @review
  77. */
  78. reverse(location, callback) {
  79. AUI().use('jsonp', (A) => {
  80. const reverseUrl = OpenStreetMapGeocoder.TPL_REVERSE_GEOCODING_URL.replace(
  81. '{lat}',
  82. location.lat
  83. ).replace('{lng}', location.lng);
  84. A.jsonp(reverseUrl, {
  85. context: this,
  86. on: {
  87. success: A.rbind('_handleReverseJSONP', this, callback),
  88. },
  89. });
  90. });
  91. }
  92. }
  93. /**
  94. * Url template used for OpenStreetMapGeocoder.forward() method
  95. * @review
  96. * @see OpenStreetMapGeocoder.forward()
  97. * @type {string}
  98. */
  99. OpenStreetMapGeocoder.TPL_FORWARD_GEOCODING_URL =
  100. '//nominatim.openstreetmap.org/search?format=json&json_callback={callback}&q={query}';
  101. /**
  102. * Url template used for OpenStreetMapGeocoder.reverse() method
  103. * @review
  104. * @see OpenStreetMapGeocoder.reverse()
  105. * @type {string}
  106. */
  107. OpenStreetMapGeocoder.TPL_REVERSE_GEOCODING_URL =
  108. '//nominatim.openstreetmap.org/reverse?format=json&json_callback={callback}&lat={lat}&lon={lng}';
  109. export default OpenStreetMapGeocoder;
  110. export {OpenStreetMapGeocoder};