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 '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. this._map = null;
  33. }
  34. /**
  35. * @inheritDoc
  36. * @review
  37. */
  38. _createMap(location, controlsConfig) {
  39. const mapConfig = {
  40. center: location,
  41. layers: [L.tileLayer(this.tileURI)],
  42. zoom: this.zoom,
  43. };
  44. const map = L.map(
  45. document.querySelector(this.boundingBox),
  46. Object.assign(mapConfig, controlsConfig)
  47. );
  48. if (this.data && this.data.features) {
  49. const bounds = new L.LatLngBounds();
  50. this.data.features.forEach((feature) =>
  51. bounds.extend(
  52. new L.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. const LeafLetControl = L.Control.extend({
  68. onAdd() {
  69. if (typeof control === 'string') {
  70. control = document.querySelector(control);
  71. }
  72. return control;
  73. },
  74. options: {
  75. position: MapOpenStreetMap.POSITION_MAP[position],
  76. },
  77. });
  78. this._map.addControl(new LeafLetControl());
  79. }
  80. /**
  81. * @inheritDoc
  82. * @review
  83. */
  84. getBounds() {
  85. return this._map.getBounds();
  86. }
  87. /**
  88. * @inheritDoc
  89. * @review
  90. */
  91. setCenter(location) {
  92. if (this._map) {
  93. this._map.panTo(location);
  94. }
  95. if (this._geolocationMarker) {
  96. this._geolocationMarker.setPosition(location);
  97. }
  98. }
  99. }
  100. MapBase.DialogImpl = OpenStreetMapDialog;
  101. MapBase.GeocoderImpl = OpenStreetMapGeocoder;
  102. MapBase.GeoJSONImpl = OpenStreetMapGeoJSON;
  103. MapBase.MarkerImpl = OpenStreetMapMarker;
  104. MapBase.SearchImpl = null;
  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};