Skip to content

Commit f3f0953

Browse files
authored
Merge pull request #3512 from terrestris/geolocation
chore: refactor geolocation button
2 parents 18369fb + 163b6fa commit f3f0953

File tree

5 files changed

+87
-346
lines changed

5 files changed

+87
-346
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,61 @@
11
This demonstrates the use of the geolocation button.
22

3+
34
```jsx
45
import GeoLocationButton from '@terrestris/react-geo/dist/Button/GeoLocationButton/GeoLocationButton';
5-
import ToggleGroup from '@terrestris/react-geo/dist/Button/ToggleGroup/ToggleGroup';
6+
import MapComponent from '@terrestris/react-util/dist/Components/MapComponent/MapComponent';
7+
import MapContext from '@terrestris/react-util/dist/Context/MapContext/MapContext';
68
import OlLayerTile from 'ol/layer/Tile';
79
import OlMap from 'ol/Map';
810
import { fromLonLat } from 'ol/proj';
911
import OlSourceOSM from 'ol/source/OSM';
1012
import OlView from 'ol/View';
11-
import * as React from 'react';
12-
13-
class GeoLocationButtonExample extends React.Component {
14-
15-
constructor(props) {
13+
import React, { useEffect, useState } from 'react';
1614

17-
super(props);
15+
const GeoLocationButtonExample = () => {
1816

19-
this.mapDivId = `map-${Math.random()}`;
17+
const [map, setMap] = useState();
2018

21-
this.map = new OlMap({
19+
useEffect(() => {
20+
setMap(new OlMap({
2221
layers: [
2322
new OlLayerTile({
2423
name: 'OSM',
2524
source: new OlSourceOSM()
2625
})
2726
],
2827
view: new OlView({
29-
center: fromLonLat([37.40570, 8.81566]),
30-
zoom: 4
28+
center: fromLonLat([8, 50]),
29+
zoom: 9
3130
})
32-
});
33-
}
31+
}));
32+
}, []);
3433

35-
componentDidMount() {
36-
this.map.setTarget(this.mapDivId);
34+
if (!map) {
35+
return null;
3736
}
3837

39-
render() {
40-
return (
41-
<div>
42-
<div
43-
id={this.mapDivId}
38+
return (
39+
<MapContext.Provider
40+
value={map}
41+
>
42+
<>
43+
<MapComponent
44+
map={map}
4445
style={{
4546
height: '400px'
4647
}}
4748
/>
48-
<ToggleGroup>
49-
<GeoLocationButton
50-
onGeolocationChange={() => undefined}
51-
map={this.map}
52-
showMarker={true}
53-
follow={true}
54-
>
55-
Track location
56-
</GeoLocationButton>
57-
</ToggleGroup>
58-
</div>
59-
);
60-
}
61-
}
49+
<GeoLocationButton
50+
showMarker={true}
51+
follow={true}
52+
>
53+
Enable GeoLocation
54+
</GeoLocationButton>
55+
</>
56+
</MapContext.Provider>
57+
);
58+
};
6259

6360
<GeoLocationButtonExample />
64-
6561
```

src/Button/GeoLocationButton/GeoLocationButton.spec.tsx

+5-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
} from '@terrestris/react-util/dist/Util/geolocationMock';
66
import { render, within } from '@testing-library/react';
77
import userEvent from '@testing-library/user-event';
8-
import { transform } from 'ol/proj';
98
import * as React from 'react';
109

1110
import TestUtil from '../../Util/TestUtil';
@@ -34,17 +33,16 @@ describe('<GeoLocationButton />', () => {
3433
});
3534

3635
it('can be rendered', () => {
37-
const { container } = render(<GeoLocationButton map={map} />);
36+
const { container } = render(<GeoLocationButton />);
3837
expect(container).toBeVisible();
3938
});
4039

4140
it('can be pressed', async () => {
4241
const callback = jest.fn();
4342

4443
const { container } = render(<GeoLocationButton
45-
map={map}
4644
showMarker={false}
47-
onGeolocationChange={callback}
45+
onGeoLocationChange={callback}
4846
/>);
4947

5048
const button = within(container).getByRole('button');
@@ -58,9 +56,8 @@ describe('<GeoLocationButton />', () => {
5856
const callback = jest.fn();
5957

6058
const { container } = render(<GeoLocationButton
61-
map={map}
6259
showMarker={false}
63-
onGeolocationChange={callback}
60+
onGeoLocationChange={callback}
6461
/>);
6562

6663
fireGeolocationListeners();
@@ -85,9 +82,8 @@ describe('<GeoLocationButton />', () => {
8582
const callback = jest.fn();
8683

8784
const { container } = render(<GeoLocationButton
88-
map={map}
8985
showMarker={false}
90-
onGeolocationChange={callback}
86+
onGeoLocationChange={callback}
9187
/>);
9288

9389
const button = within(container).getByRole('button');
@@ -105,12 +101,10 @@ describe('<GeoLocationButton />', () => {
105101
}
106102
});
107103

108-
const converted = transform(coordinates, 'EPSG:4326', map.getView().getProjection());
109-
110104
expect(callback).toBeCalledWith({
111105
accuracy: 7,
112106
heading: 0,
113-
position: converted,
107+
position: coordinates,
114108
speed: 9
115109
});
116110
});

0 commit comments

Comments
 (0)