Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix popup display while zoomed out #226

Merged
merged 4 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ import {
LAYER_PANEL_SHOW_LAYER_ICON,
LAYER_VISIBILITY,
} from '../../../common';
import {
LayerActions,
layersFunctionMap,
referenceLayerTypeLookup,
} from '../../model/layersFunctions';
import { referenceLayerTypeLookup } from '../../model/layersFunctions';
import { useOpenSearchDashboards } from '../../../../../src/plugins/opensearch_dashboards_react/public';
import { MapServices } from '../../types';
import {
Expand All @@ -48,7 +44,7 @@ import {
} from '../../model/layerRenderController';
import { MapState } from '../../model/mapState';
import { ConfigSchema } from '../../../common/config';
import {moveLayers, removeLayers, updateLayerVisibility} from "../../model/map/layer_operations";
import { moveLayers, removeLayers, updateLayerVisibility } from '../../model/map/layer_operations';

interface MaplibreRef {
current: Maplibre | null;
Expand Down
15 changes: 11 additions & 4 deletions public/components/map_container/map_container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { MAP_INITIAL_STATE, DASHBOARDS_MAPS_LAYER_TYPE } from '../../../common';
import { MapLayerSpecification } from '../../model/mapLayerType';
import { IndexPattern } from '../../../../../src/plugins/data/public';
import { MapState } from '../../model/mapState';
import { createPopup, getPopupLngLat, isTooltipEnabledLayer } from '../tooltip/create_tooltip';
import { createPopup, getPopupLocation, isTooltipEnabledLayer } from '../tooltip/create_tooltip';
import { handleDataLayerRender } from '../../model/layerRenderController';
import { useOpenSearchDashboards } from '../../../../../src/plugins/opensearch_dashboards_react/public';
import { MapServices } from '../../types';
Expand Down Expand Up @@ -86,12 +86,13 @@ export const MapContainer = ({
if (features && maplibreRef.current) {
clickPopup = createPopup({ features, layers: tooltipEnabledLayers });
clickPopup
?.setLngLat(getPopupLngLat(features[0].geometry) ?? e.lngLat)
?.setLngLat(getPopupLocation(features[0].geometry, e.lngLat))
.addTo(maplibreRef.current);
}
}

function onMouseMoveMap(e: MapEventType['mousemove']) {
// This is required to update coordinates on map only on mouse move
setCoordinates(e.lngLat.wrap());

// remove previous popup
Expand All @@ -107,7 +108,7 @@ export const MapContainer = ({
showLayerSelection: false,
});
hoverPopup
?.setLngLat(getPopupLngLat(features[0].geometry) ?? e.lngLat)
?.setLngLat(getPopupLocation(features[0].geometry, e.lngLat))
.addTo(maplibreRef.current);
}
}
Expand Down Expand Up @@ -166,7 +167,13 @@ export const MapContainer = ({

return (
<div>
<EuiPanel hasShadow={false} hasBorder={false} color="transparent" className="zoombar" data-test-subj="mapStatusBar">
<EuiPanel
hasShadow={false}
hasBorder={false}
color="transparent"
className="zoombar"
data-test-subj="mapStatusBar"
>
<small>
{coordinates &&
`lat: ${coordinates.lat.toFixed(4)}, lon: ${coordinates.lng.toFixed(4)}, `}
Expand Down
27 changes: 18 additions & 9 deletions public/components/tooltip/create_tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Popup, MapGeoJSONFeature } from 'maplibre-gl';
import { Popup, MapGeoJSONFeature, LngLat } from 'maplibre-gl';

import { MapLayerSpecification, DocumentLayerSpecification } from '../../model/mapLayerType';
import { FeatureGroupItem, TooltipContainer } from './tooltipContainer';
import {MAX_LONGITUDE} from "../../../common";

type Options = {
interface Options {
features: MapGeoJSONFeature[];
layers: DocumentLayerSpecification[];
showCloseButton?: boolean;
showPagination?: boolean;
showLayerSelection?: boolean;
};
}

export function isTooltipEnabledLayer(
layer: MapLayerSpecification
): layer is DocumentLayerSpecification {
return (
layer.type !== 'opensearch_vector_tile_map' &&
layer.type !== 'custom_map' &&
layer.source.showTooltips === true
layer.source.showTooltips === true &&
!!layer.source.tooltipFields?.length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the last condition !!layer.source.tooltipFields?.length returns false if tooltipFields are not provided after enabling toolTips?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showTooltips is a flag where user controls whether to show toolTip or not, however, tooltipFields contains list of tooltip fields. The last condition checks whether length of tooltipFields is > 0 or not

Copy link
Member

@naveentatikonda naveentatikonda Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I got it. But, I'm trying to understand what will be the value of !!layer.source.tooltipFields?.length if no tooltipFields are provided. If it is not an optional field it's value will be false. But, what will be the value when it is optional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false

);
}

Expand All @@ -39,15 +41,22 @@ export function groupFeaturesByLayers(
return featureGroups;
}

export function getPopupLngLat(geometry: GeoJSON.Geometry) {
export function getPopupLocation(geometry: GeoJSON.Geometry, mousePoint: LngLat) {
// geometry.coordinates is different for different geometry.type, here we use the geometry.coordinates
// of a Point as the position of the popup. For other types, such as Polygon, MultiPolygon, etc,
// use mouse position should be better
if (geometry.type === 'Point') {
return [geometry.coordinates[0], geometry.coordinates[1]] as [number, number];
} else {
return null;
if (geometry.type !== 'Point') {
return mousePoint;
}
const coordinates = geometry.coordinates;
// Copied from https://maplibre.org/maplibre-gl-js-docs/example/popup-on-click/
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(mousePoint.lng - coordinates[0]) > MAX_LONGITUDE) {
coordinates[0] += mousePoint.lng > coordinates[0] ? 360 : -360;
}
return [coordinates[0], coordinates[1]] as [number, number];
}

export function createPopup({
Expand Down