Skip to content

Commit 6f1787f

Browse files
bors[bot]bidoubiwa
andauthored
Merge #567
567: Partial fix of geo search in full dezoom r=bidoubiwa a=bidoubiwa ![Large GIF (542x524)](https://user-images.githubusercontent.com/33010418/138096055-68691c7d-49b6-4910-b097-5bd0fe3b72b3.gif) Fixes partially #551 . It now works great until you decide to slide to the east or the west! Thanks `@ManyTheFish` :) Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 27fa7a1 + e05ee27 commit 6f1787f

File tree

5 files changed

+21
-31
lines changed

5 files changed

+21
-31
lines changed

playgrounds/geo-javascript/setup/settings.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
{
2-
"rankingRules": [
3-
"words",
4-
"typo",
5-
"proximity",
6-
"attribute",
7-
"sort",
8-
"exactness"
9-
],
102
"filterableAttributes": [
113
"_geo"
124
],

src/adapter/search-request-adapter/__tests__/geo-rules.tests.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ test('Adapt geoPoints rules with same 0 lat and 0 lng geo points', () => {
1010
insideBoundingBox: '0,0,0,0',
1111
})
1212

13-
expect(rules?.filter).toBe('_geoRadius(0, 0, 0)')
13+
expect(rules?.filter).toBe('_geoRadius(0.00000, 0.00000, 0)')
1414
})
1515

1616
test('Adapt geoPoints rules with integer geo points', () => {
1717
const rules = adaptGeoPointsRules({
1818
insideBoundingBox: '1,2,3,4',
1919
})
20-
expect(rules?.filter).toBe(
21-
'_geoRadius(2.0003044085023727, 2.999390393801055, 157201.47551181243)'
22-
)
20+
expect(rules?.filter).toBe('_geoRadius(3.17650, 3.19394, 157201.47551181243)')
2321
})
2422

2523
test('Try geoContext with only a radius', () => {
@@ -41,15 +39,15 @@ test('Try geoContext with an aroundLatLng and a radius', () => {
4139
aroundLatLng: '51.1241999, 9.662499900000057',
4240
aroundRadius: 1,
4341
})
44-
expect(rules?.filter).toBe('_geoRadius(51.1241999, 9.662499900000057, 1)')
42+
expect(rules?.filter).toBe('_geoRadius(51.12420, 9.66250, 1)')
4543
})
4644

4745
test('Try geoContext with an aroundLatLng and a 0 radius', () => {
4846
const rules = adaptGeoPointsRules({
4947
aroundLatLng: '51.1241999, 9.662499900000057',
5048
aroundRadius: 0,
5149
})
52-
expect(rules?.filter).toBe('_geoRadius(51.1241999, 9.662499900000057, 0)')
50+
expect(rules?.filter).toBe('_geoRadius(51.12420, 9.66250, 0)')
5351
})
5452

5553
test('Try geoContext with aroundLatLng, radius and insideBoundingBox', () => {
@@ -58,25 +56,19 @@ test('Try geoContext with aroundLatLng, radius and insideBoundingBox', () => {
5856
aroundRadius: 1,
5957
insideBoundingBox: '1,2,3,4',
6058
})
61-
expect(rules?.filter).toBe(
62-
'_geoRadius(2.0003044085023727, 2.999390393801055, 157201.47551181243)'
63-
)
59+
expect(rules?.filter).toBe('_geoRadius(3.17650, 3.19394, 157201.47551181243)')
6460
})
6561
test('Try geoContext with a radius and insideBoundingBox', () => {
6662
const rules = adaptGeoPointsRules({
6763
aroundRadius: 1,
6864
insideBoundingBox: '1,2,3,4',
6965
})
70-
expect(rules?.filter).toBe(
71-
'_geoRadius(2.0003044085023727, 2.999390393801055, 157201.47551181243)'
72-
)
66+
expect(rules?.filter).toBe('_geoRadius(3.17650, 3.19394, 157201.47551181243)')
7367
})
7468
test('Try geoContext with aroundLatLng and insideBoundingBox', () => {
7569
const rules = adaptGeoPointsRules({
7670
aroundLatLng: '51.1241999, 9.662499900000057',
7771
insideBoundingBox: '1,2,3,4',
7872
})
79-
expect(rules?.filter).toBe(
80-
'_geoRadius(2.0003044085023727, 2.999390393801055, 157201.47551181243)'
81-
)
73+
expect(rules?.filter).toBe('_geoRadius(3.17650, 3.19394, 157201.47551181243)')
8274
})

src/adapter/search-request-adapter/__tests__/search-params.tests.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test('Adapt SearchContext with filters, sort and geo rules ', () => {
3636
})
3737

3838
expect(searchParams.filter).toStrictEqual([
39-
'_geoRadius(0, 0, 0)',
39+
'_geoRadius(0.00000, 0.00000, 0)',
4040
['genres="Drama"', 'genres="Thriller"'],
4141
['title="Ariel"'],
4242
])
@@ -54,7 +54,7 @@ test('Adapt SearchContext with only facetFilters and geo rules ', () => {
5454
})
5555

5656
expect(searchParams.filter).toEqual([
57-
'_geoRadius(0, 0, 0)',
57+
'_geoRadius(0.00000, 0.00000, 0)',
5858
['genres="Drama"', 'genres="Thriller"'],
5959
['title="Ariel"'],
6060
])
@@ -70,7 +70,7 @@ test('Adapt SearchContext with only sort and geo rules ', () => {
7070
sort: 'id < 1',
7171
})
7272

73-
expect(searchParams.filter).toEqual(['_geoRadius(0, 0, 0)'])
73+
expect(searchParams.filter).toEqual(['_geoRadius(0.00000, 0.00000, 0)'])
7474
expect(searchParams.sort).toStrictEqual(['id < 1'])
7575
expect(searchParams.attributesToHighlight).toContain('*')
7676
expect(searchParams.attributesToHighlight?.length).toBe(1)
@@ -83,7 +83,7 @@ test('Adapt SearchContext with no sort abd no filters and geo rules ', () => {
8383
insideBoundingBox: '0,0,0,0',
8484
})
8585

86-
expect(searchParams.filter).toEqual(['_geoRadius(0, 0, 0)'])
86+
expect(searchParams.filter).toEqual(['_geoRadius(0.00000, 0.00000, 0)'])
8787
expect(searchParams.attributesToHighlight).toContain('*')
8888
expect(searchParams.attributesToHighlight?.length).toBe(1)
8989
})

src/adapter/search-request-adapter/geo-rules-adapter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export function adaptGeoPointsRules(
4040
}
4141

4242
if (middlePoint != null && radius != null) {
43-
const [lat3, lng3] = middlePoint.split(',')
44-
43+
let [lat3, lng3] = middlePoint.split(',')
44+
lat3 = Number.parseFloat(lat3).toFixed(5)
45+
lng3 = Number.parseFloat(lng3).toFixed(5)
4546
const filter = `_geoRadius(${lat3}, ${lng3}, ${radius})`
4647

4748
return { filter }

src/utils/geographic.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ export function middleGeoPoints(
5151
let lng3 = Math.atan2(y, x)
5252
let lat3 = Math.atan2(z, Hyp)
5353

54-
lat3 = rad2degr(lat3)
55-
lng3 = rad2degr(lng3)
54+
if (lng1 < lng2 || (lng1 > lng2 && lng1 > Math.PI && lng2 < -Math.PI)) {
55+
lat3 = lat3 + Math.PI
56+
lng3 = lng3 + Math.PI
57+
} else {
58+
lat3 = rad2degr(lat3)
59+
lng3 = rad2degr(lng3)
60+
}
5661

5762
if (
5863
Math.abs(x) < Math.pow(10, -9) &&

0 commit comments

Comments
 (0)