Source: map-google-maps/src/main/resources/META-INF/resources/js/GoogleMapsGeocoder.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. * GoogleMapsGeocoder
  17. * @review
  18. */
  19. class GoogleMapsGeocoder extends State {
  20. /**
  21. * Creates a new geocoder using Google Map's API
  22. * @param {Array} args List of arguments to be passed to State
  23. * @review
  24. */
  25. constructor(...args) {
  26. super(...args);
  27. this._geocoder = new google.maps.Geocoder();
  28. }
  29. /**
  30. * Handles the server response of a successfull address/location resolution
  31. * @param {function} callback Callback that will be executed on success
  32. * @param {Object} location Raw location information
  33. * @param {Object} response Server response
  34. * @param {Object} status Server response status
  35. * @protected
  36. * @review
  37. */
  38. _handleGeocoderResponse(callback, location, response, status) {
  39. const result = {
  40. data: {},
  41. err: status === google.maps.GeocoderStatus.OK ? null : status,
  42. };
  43. if (!result.err) {
  44. const geocoderResult = response[0];
  45. const geolocation = geocoderResult.geometry.location;
  46. result.data = {
  47. address: geocoderResult.formatted_address,
  48. location: {
  49. lat: geolocation.lat(),
  50. lng: geolocation.lng(),
  51. },
  52. };
  53. }
  54. else {
  55. result.data = {
  56. address: '',
  57. location,
  58. };
  59. }
  60. callback(result);
  61. }
  62. /**
  63. * Transforms a given address into valid latitude and longitude
  64. * @param {string} query Address to be transformed into latitude and longitude
  65. * @param {function} callback Callback that will be executed on success
  66. * @review
  67. */
  68. forward(query, callback) {
  69. const payload = {
  70. address: query,
  71. };
  72. this._geocoder.geocode(
  73. payload,
  74. this._handleGeocoderResponse.bind(this, callback)
  75. );
  76. }
  77. /**
  78. * Transforms a given location object (lat, lng) into a valid address
  79. * @param {string} location Location information to be sent to the server
  80. * @param {function} callback Callback that will be executed on success
  81. * @review
  82. */
  83. reverse(location, callback) {
  84. const payload = {
  85. location,
  86. };
  87. this._geocoder.geocode(
  88. payload,
  89. this._handleGeocoderResponse.bind(this, callback, location)
  90. );
  91. }
  92. }
  93. export default GoogleMapsGeocoder;
  94. export {GoogleMapsGeocoder};