React Map - Customize the OpenStreetMap Provider

If provider is "osm", the Map uses OpenLayers to display maps. This provider displays marker tooltips in DevExtreme Popover instances. Use OpenLayers and dxPopover APIs to customize the map and its marker tooltips.

Customize Marker Tooltips

The markers[].onClick handler receives the clicked marker's Popover instance in e.tooltip. The Map calls this handler before it displays or hides the tooltip. When a user clicks a marker with a hidden tooltip, the tooltip appears with the settings that you specify in the handler. To keep the tooltip hidden, assign a Popover onShowing handler that sets e.cancel to true.

Display a Title and Close Button

Specify Popover title, showTitle, and showCloseButton properties to display a title and a Close button:

JavaScript
  • function onMarkerClick(e) {
  • e.tooltip?.option({
  • title: 'Empire State Building',
  • showTitle: true,
  • showCloseButton: true
  • });
  • }

Hide Tooltips on Outside Click

The following code snippet hides all tooltips when a user clicks outside markers and tooltips:

JavaScript
  • const tooltips = new Set();
  •  
  • function hideTooltips() {
  • tooltips.forEach((tooltip) => tooltip.hide());
  • }
  •  
  • function handleMarkerClick(e) {
  • const { tooltip } = e;
  • if (tooltip && !tooltips.has(tooltip)) {
  • tooltips.add(tooltip);
  • // Hides tooltips when a user clicks outside the Map component
  • tooltip.option('hideOnOutsideClick', () => {
  • hideTooltips();
  • return true;
  • });
  • tooltip.on('disposing', () => tooltips.delete(tooltip));
  • }
  • }
  •  
  • // Hides tooltips when a user clicks the map
  • function handleMapClick() {
  • hideTooltips();
  • }

This code does not hide tooltips that appear at marker creation (isShown is true) until users click corresponding markers.

Customize the OpenLayers Map

The onReady handler receives the OpenLayers Map instance in e.originalMap. The Map calls onReady each time it creates an OpenLayers map (for instance, after you change providerConfig), so customizations in this handler apply to each new map.

NOTE
Code examples in this section import OpenLayers classes from the ol package. Non-modular applications can use the global ol object instead (for instance, ol.View).

Specify Zoom Levels

Call setMinZoom and setMaxZoom on the OpenLayers view to limit zoom levels:

JavaScript
  • function onReady(e) {
  • const view = e.originalMap.getView();
  • view.setMinZoom(10);
  • view.setMaxZoom(16);
  • }

The Map also applies these limits to zoom values.

Restrict the Visible Area

Create an OpenLayers View with an extent (the area that users can view) and assign this view to the map:

JavaScript
  • import View from 'ol/View.js';
  • import { transformExtent } from 'ol/proj.js';
  •  
  • function onReady(e) {
  • const map = e.originalMap;
  • const view = map.getView();
  • const projection = view.getProjection();
  • map.setView(new View({
  • projection,
  • center: view.getCenter(),
  • zoom: view.getZoom(),
  • // [west, south, east, north] in degrees, converted to the map projection
  • extent: transformExtent([-74.03, 40.68, -73.90, 40.88], 'EPSG:4326', projection)
  • }));
  • }

The Map also applies this restriction to center and zoom values.

Customize the Attribution Control

OpenLayers displays OsmTileServerConfig.attribution in a collapsible Attribution control. To change display settings, replace this control with a new instance. The following code snippet displays an attribution that users cannot collapse:

JavaScript
  • import Attribution from 'ol/control/Attribution.js';
  •  
  • function onReady(e) {
  • const map = e.originalMap;
  • map.getControls().getArray()
  • .filter((control) => control instanceof Attribution)
  • .forEach((control) => map.removeControl(control));
  • map.addControl(new Attribution({ collapsible: false }));
  • }