Source: map-google-maps/src/main/resources/META-INF/resources/js/MapGoogleMaps.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 MapBase from 'map-common/js/MapBase.es';
  15. import GoogleMapsDialog from './GoogleMapsDialog.es';
  16. import GoogleMapsGeoJSON from './GoogleMapsGeoJSON.es';
  17. import GoogleMapsGeocoder from './GoogleMapsGeocoder.es';
  18. import GoogleMapsMarker from './GoogleMapsMarker.es';
  19. import GoogleMapsSearch from './GoogleMapsSearch.es';
  20. /**
  21. * MapGoogleMaps
  22. * @review
  23. */
  24. class MapGoogleMaps extends MapBase {
  25. /**
  26. * Creates a new map using Google Map's API
  27. * @param {Array} args List of arguments to be passed to State
  28. * @review
  29. */
  30. constructor(...args) {
  31. super(...args);
  32. this._bounds = null;
  33. }
  34. /**
  35. * @inheritDoc
  36. * @review
  37. */
  38. _createMap(location, controlsConfig) {
  39. const mapConfig = {
  40. center: location,
  41. mapTypeId: google.maps.MapTypeId.ROADMAP,
  42. zoom: this.zoom,
  43. };
  44. const map = new google.maps.Map(
  45. document.querySelector(this.boundingBox),
  46. Object.assign(mapConfig, controlsConfig)
  47. );
  48. if (this.data && this.data.features) {
  49. const bounds = new google.maps.LatLngBounds();
  50. this.data.features.forEach((feature) =>
  51. bounds.extend(
  52. new google.maps.LatLng(
  53. feature.geometry.coordinates[1],
  54. feature.geometry.coordinates[0]
  55. )
  56. )
  57. );
  58. map.fitBounds(bounds);
  59. }
  60. return map;
  61. }
  62. /**
  63. * @inheritDoc
  64. * @review
  65. */
  66. addControl(control, position) {
  67. if (this._map.controls[position]) {
  68. if (typeof control === 'string') {
  69. control = document.querySelector(control);
  70. }
  71. this._map.controls[position].push(control);
  72. }
  73. }
  74. /**
  75. * @inheritDoc
  76. * @review
  77. */
  78. getBounds() {
  79. let bounds = this._map.getBounds() || this._bounds;
  80. if (!bounds) {
  81. bounds = new google.maps.LatLngBounds();
  82. this._bounds = bounds;
  83. }
  84. return bounds;
  85. }
  86. /**
  87. * @inheritDoc
  88. * @review
  89. */
  90. setCenter(location) {
  91. if (this._map) {
  92. this._map.setCenter(location);
  93. }
  94. }
  95. }
  96. MapBase.DialogImpl = GoogleMapsDialog;
  97. MapBase.GeocoderImpl = GoogleMapsGeocoder;
  98. MapBase.GeoJSONImpl = GoogleMapsGeoJSON;
  99. MapBase.MarkerImpl = GoogleMapsMarker;
  100. MapBase.SearchImpl = GoogleMapsSearch;
  101. MapGoogleMaps.CONTROLS_MAP = {
  102. [MapBase.CONTROLS.OVERVIEW]: 'overviewMapControl',
  103. [MapBase.CONTROLS.PAN]: 'panControl',
  104. [MapBase.CONTROLS.ROTATE]: 'rotateControl',
  105. [MapBase.CONTROLS.SCALE]: 'scaleControl',
  106. [MapBase.CONTROLS.STREETVIEW]: 'streetViewControl',
  107. [MapBase.CONTROLS.TYPE]: 'mapTypeControl',
  108. [MapBase.CONTROLS.ZOOM]: 'zoomControl',
  109. };
  110. window.Liferay = window.Liferay || {};
  111. window.Liferay.GoogleMap = MapGoogleMaps;
  112. export default MapGoogleMaps;
  113. export {MapGoogleMaps};