Skip to content

Commit

Permalink
Closest feature to the center
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Sep 15, 2020
1 parent 4265326 commit e2fbfe7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
29 changes: 26 additions & 3 deletions src/components/manage/Blocks/DiscodataOpenlayersMapBlock/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ const initialExtent = [
10421410.9999871,
];
let renderExtent = [];

const getDistance = (P1, P2) => {
const cathetus_1 = Math.pow(P1[0] - P2[0], 2);
const cathetus_2 = Math.pow(P1[1] - P2[1], 2);
return Math.sqrt(cathetus_1 + cathetus_2);
};

const OpenlayersMapView = (props) => {
const stateRef = useRef({
map: {
Expand Down Expand Up @@ -899,12 +906,28 @@ const OpenlayersMapView = (props) => {
}
map.on('moveend', function (e) {
if (hasSidebar && document.getElementById('dynamic-filter')) {
const closestFeature = {
feature: null,
distance: Infinity,
};
const features = stateRef.current.map.sitesSourceLayer
.getSource()
.getFeatures();

features.forEach((feature, index) => {
const distance = getDistance(
feature.getGeometry().flatCoordinates,
stateRef.current.map.element.getView().getCenter(),
);
if (closestFeature.distance > distance) {
closestFeature.feature = feature;
closestFeature.distance = distance;
}
});
document.getElementById('dynamic-filter').dispatchEvent(
new CustomEvent('featurechange', {
detail: {
features: sitesSource.getFeaturesInExtent(
map.getView().calculateExtent(),
),
feature: closestFeature.feature,
},
}),
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/manage/Blocks/FiltersBlock/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ const View = ({ content, ...props }) => {

const onFeaturechange = (e) => {
if (
e.detail.features?.[0]?.getProperties?.()?.countryCode !==
e.detail.feature?.getProperties?.()?.countryCode !==
alphaFeatureRef.current?.getProperties?.()?.countryCode
) {
setAlphaFeature(e.detail.features?.[0]);
setAlphaFeature(e.detail.feature);
}
};

Expand Down

0 comments on commit e2fbfe7

Please sign in to comment.