Source: map-openstreetmap/src/main/resources/META-INF/resources/js/MapOpenStreetMap.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 '@liferay/map-common/js/MapBase.es';
  15. import {Config} from 'metal-state';
  16. import OpenStreetMapDialog from './OpenStreetMapDialog.es';
  17. import OpenStreetMapGeoJSON from './OpenStreetMapGeoJSON.es';
  18. import OpenStreetMapGeocoder from './OpenStreetMapGeocoder.es';
  19. import OpenStreetMapMarker from './OpenStreetMapMarker.es';
  20. /**
  21. * MapOpenStreetMap
  22. * @review
  23. */
  24. class MapOpenStreetMap extends MapBase {
  25. /**
  26. * Creates a new map using OpenStreetMap's API
  27. * @param {Array} args List of arguments to be passed to State
  28. * @review
  29. */
  30. constructor(...args) {
  31. super(...args);
  32. MapBase.DialogImpl = OpenStreetMapDialog;
  33. MapBase.GeocoderImpl = OpenStreetMapGeocoder;
  34. MapBase.GeoJSONImpl = OpenStreetMapGeoJSON;
  35. MapBase.MarkerImpl = OpenStreetMapMarker;
  36. MapBase.SearchImpl = null;
  37. this._map = null;
  38. }
  39. /**
  40. * @inheritDoc
  41. * @review
  42. */
  43. _createMap(location, controlsConfig) {
  44. const mapConfig = {
  45. center: location,
  46. layers: [L.tileLayer(this.tileURI)],
  47. zoom: this.zoom,
  48. };
  49. const map = L.map(
  50. document.querySelector(this.boundingBox),
  51. Object.assign(mapConfig, controlsConfig)
  52. );
  53. if (this.data && this.data.features) {
  54. const bounds = new L.LatLngBounds();
  55. this.data.features.forEach((feature) =>
  56. bounds.extend(
  57. new L.LatLng(
  58. feature.geometry.coordinates[1],
  59. feature.geometry.coordinates[0]
  60. )
  61. )
  62. );
  63. map.fitBounds(bounds);
  64. }
  65. return map;
  66. }
  67. /**
  68. * @inheritDoc
  69. * @review
  70. */
  71. addControl(control, position) {
  72. const LeafLetControl = L.Control.extend({
  73. onAdd() {
  74. if (typeof control === 'string') {
  75. control = document.querySelector(control);
  76. }
  77. return control;
  78. },
  79. options: {
  80. position: MapOpenStreetMap.POSITION_MAP[position],
  81. },
  82. });
  83. this._map.addControl(new LeafLetControl());
  84. }
  85. /**
  86. * @inheritDoc
  87. * @review
  88. */
  89. getBounds() {
  90. return this._map.getBounds();
  91. }
  92. /**
  93. * @inheritDoc
  94. * @review
  95. */
  96. setCenter(location) {
  97. if (this._map) {
  98. this._map.panTo(location);
  99. }
  100. if (this._geolocationMarker) {
  101. this._geolocationMarker.setPosition(location);
  102. }
  103. }
  104. }
  105. MapOpenStreetMap.CONTROLS_MAP = {
  106. [MapBase.CONTROLS.ATTRIBUTION]: 'attributionControl',
  107. [MapBase.CONTROLS.ZOOM]: 'zoomControl',
  108. };
  109. MapOpenStreetMap.POSITION_MAP = {
  110. [MapBase.POSITION.BOTTOM]: 'bottomleft',
  111. [MapBase.POSITION.BOTTOM_CENTER]: 'bottomleft',
  112. [MapBase.POSITION.BOTTOM_LEFT]: 'bottomleft',
  113. [MapBase.POSITION.BOTTOM_RIGHT]: 'bottomright',
  114. [MapBase.POSITION.CENTER]: 'topleft',
  115. [MapBase.POSITION.LEFT]: 'topleft',
  116. [MapBase.POSITION.LEFT_BOTTOM]: 'bottomleft',
  117. [MapBase.POSITION.LEFT_CENTER]: 'topleft',
  118. [MapBase.POSITION.LEFT_TOP]: 'topleft',
  119. [MapBase.POSITION.RIGHT]: 'bottomright',
  120. [MapBase.POSITION.RIGHT_BOTTOM]: 'bottomright',
  121. [MapBase.POSITION.RIGHT_CENTER]: 'bottomright',
  122. [MapBase.POSITION.RIGHT_TOP]: 'topright',
  123. [MapBase.POSITION.TOP]: 'topright',
  124. [MapBase.POSITION.TOP_CENTER]: 'topright',
  125. [MapBase.POSITION.TOP_LEFT]: 'topleft',
  126. [MapBase.POSITION.TOP_RIGHT]: 'topright',
  127. };
  128. /**
  129. * State definition.
  130. * @type {!Object}
  131. * @static
  132. */
  133. MapOpenStreetMap.STATE = {
  134. ...MapBase.STATE,
  135. /**
  136. * Url used for fetching map tile information
  137. * @type {string}
  138. */
  139. tileURI: Config.string().value(
  140. '//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
  141. ),
  142. };
  143. export default MapOpenStreetMap;
  144. export {MapOpenStreetMap};