Viktoriia 2 недель назад
Родитель
Сommit
0ea47be88e
1 измененных файлов с 26 добавлено и 1 удалено
  1. 26 1
      src/screens/InAppScreens/MapScreen/index.tsx

+ 26 - 1
src/screens/InAppScreens/MapScreen/index.tsx

@@ -95,6 +95,9 @@ import {
 import { SheetManager } from 'react-native-actions-sheet';
 import MultipleSeriesModal from './MultipleSeriesModal';
 import { useSubscription } from 'src/screens/OfflineMapsScreen/useSubscription';
+import { point } from '@turf/turf';
+import { booleanPointInPolygon } from '@turf/turf';
+import { area } from '@turf/turf';
 
 configureReanimatedLogger({
   strict: false
@@ -1194,6 +1197,7 @@ const MapScreen: any = ({ navigation, route }: { navigation: any; route: any })
     if (type === 'blank') return;
     try {
       const { screenPointX, screenPointY } = event.properties;
+      const [lng, lat] = event.geometry?.coordinates;
 
       const { features } = await mapRef.current.queryRenderedFeaturesAtPoint(
         [screenPointX, screenPointY],
@@ -1202,7 +1206,28 @@ const MapScreen: any = ({ navigation, route }: { navigation: any; route: any })
       );
 
       if (features?.length) {
-        const region = features[0];
+        let region = features[0];
+
+        try {
+          if (typeof lng === 'number' && typeof lat === 'number') {
+            const p = point([lng, lat]);
+
+            const inside = features
+              .filter(
+                (f) => f?.geometry?.type === 'Polygon' || f?.geometry?.type === 'MultiPolygon'
+              )
+              .filter((f) => booleanPointInPolygon(p, f as any));
+
+            const best = inside.slice().sort((a, b) => area(a as any) - area(b as any))[0];
+
+            if (best) {
+              region = best;
+            }
+          }
+        } catch (e) {
+          console.warn('region detection failed', e);
+        }
+
         if (selectedRegion === region.properties?.id) return;
 
         let db = type === 'regions' ? db1 : type === 'countries' ? db3 : db2;