Source: map-google-maps/src/main/resources/META-INF/resources/js/GoogleMapsGeoJSON.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 GeoJSONBase from 'map-common/js/GeoJSONBase.es';
  15. /**
  16. * GoogleMapsGeoJSON
  17. * @review
  18. */
  19. class GoogleMapsGeoJSON extends GeoJSONBase {
  20. /**
  21. * Creates a new geojson parser 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.eventHandlers = [];
  28. this._getFeatureStyle = this._getFeatureStyle.bind(this);
  29. this._handleFeatureClicked = this._handleFeatureClicked.bind(this);
  30. this.map.data.setStyle(this._getFeatureStyle);
  31. this._bindUI();
  32. }
  33. /**
  34. * Removes the listeners that have been added to the map object.
  35. * @review
  36. */
  37. destructor() {
  38. this._eventHandlers.forEach((item) => {
  39. google.maps.event.removeListener(item);
  40. });
  41. }
  42. /**
  43. * Adds listeners for the created map object.
  44. * It listens for click events and executes
  45. * GoogleMapsGeoJSON._handleFeatureClicked.
  46. * @protected
  47. * @review
  48. */
  49. _bindUI() {
  50. this._eventHandlers = [
  51. google.maps.event.addListener(
  52. this.map.data,
  53. 'click',
  54. this._handleFeatureClicked
  55. ),
  56. ];
  57. }
  58. /**
  59. * Gets the internal style of the given feature. Both the feature and the
  60. * style are native Google Maps objects.
  61. * @param {Object} feature Google Maps native feature to be parsed.
  62. * @protected
  63. * @return {Object} Obtained style
  64. * @review
  65. */
  66. _getFeatureStyle(feature) {
  67. return {
  68. icon: feature.getProperty('icon'),
  69. };
  70. }
  71. /**
  72. * @inheritDoc
  73. * @review
  74. */
  75. _getNativeFeatures(geoJSONData) {
  76. return this.map.data.addGeoJson(geoJSONData);
  77. }
  78. /**
  79. * @inheritDoc
  80. * @review
  81. */
  82. _wrapNativeFeature(nativeFeature) {
  83. const feature = nativeFeature.getGeometry
  84. ? nativeFeature
  85. : nativeFeature.feature;
  86. feature.getMarker = () => {
  87. if (!feature._marker) {
  88. const marker = new google.maps.Marker({
  89. icon: feature.getProperty('icon'),
  90. map: this.map,
  91. opacity: 0,
  92. position: feature.getGeometry().get('location'),
  93. zIndex: -1,
  94. });
  95. feature._marker = marker;
  96. }
  97. return feature.marker;
  98. };
  99. return feature;
  100. }
  101. }
  102. export default GoogleMapsGeoJSON;
  103. export {GoogleMapsGeoJSON};