Skip to content

Commit

Permalink
fix(CoordinateInfo): update docs and react-util
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock committed Jun 19, 2024
1 parent 4a0dd06 commit 790e91b
Showing 1 changed file with 198 additions and 1 deletion.
199 changes: 198 additions & 1 deletion src/CoordinateInfo/CoordinateInfo.example.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
The `CoordinateInfo` component is only a very shallow wrapper around the `useCoordinateInfo` hook of react-util.
Often it might be easier to use the hook directly, see the second example on how it's done.

```jsx
import { faCopy } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
Expand Down Expand Up @@ -94,7 +97,6 @@ const CoordinateInfoExample = () => {
}}
/>
<CoordinateInfo
map={this.map}
queryLayers={[wmsLayer, vectorLayer]}
resultRenderer={(opts) => {
const features = opts.features;
Expand Down Expand Up @@ -189,3 +191,198 @@ const CoordinateInfoExample = () => {

<CoordinateInfoExample />;
```

Here is the same example, but using `useCoordinateInfo` directly:

```jsx
import {faCopy} from '@fortawesome/free-solid-svg-icons';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import CoordinateInfo from '@terrestris/react-geo/dist/CoordinateInfo/CoordinateInfo';
import MapComponent from '@terrestris/react-geo/dist/Map/MapComponent/MapComponent';
import MapContext from '@terrestris/react-util/dist/Context/MapContext/MapContext';
import {
Button,
Spin,
Statistic,
Tooltip
} from 'antd';
import * as copy from 'copy-to-clipboard';
import GeoJSON from 'ol/format/GeoJSON.js';
import {Vector as VectorLayer} from 'ol/layer.js';
import OlLayerTile from 'ol/layer/Tile';
import {bbox as bboxStrategy} from 'ol/loadingstrategy.js';
import OlMap from 'ol/Map';
import {fromLonLat} from 'ol/proj';
import OlSourceOSM from 'ol/source/OSM';
import OlSourceTileWMS from 'ol/source/TileWMS';
import VectorSource from 'ol/source/Vector.js';
import OlView from 'ol/View';
import React, {useEffect, useMemo, useState} from 'react';
import useCoordinateInfo from "@terrestris/react-util/dist/Hooks/useCoordinateInfo/useCoordinateInfo";

const wmsLayer = new OlLayerTile({
name: 'States (USA)',
source: new OlSourceTileWMS({
url: 'https://ahocevar.com/geoserver/wms',
params: {
LAYERS: 'usa:states',
TILED: true
},
serverType: 'geoserver',
crossOrigin: 'anonymous'
})
});

const vectorSource = new VectorSource({
format: new GeoJSON(),
url: function (extent) {
return (
'https://ows-demo.terrestris.de/geoserver/osm/wfs?service=WFS&' +
'version=1.1.0&request=GetFeature&typename=osm:osm-country-borders&' +
'outputFormat=application/json&srsname=EPSG:3857&' +
'bbox=' +
extent.join(',') +
',EPSG:3857'
);
},
strategy: bboxStrategy,
});

const vectorLayer = new VectorLayer({
source: vectorSource,
style: {
'stroke-width': 0.75,
'stroke-color': 'white',
'fill-color': 'rgba(100,100,100,0.25)',
},
});

const queryLayers = [wmsLayer, vectorLayer];

const getValue = (feature, key) => {
if (feature.getProperties().hasOwnProperty(key)) {
return feature.get(key);
}
return null;
};

const CoordinateInfoExample = () => {

const [map, setMap] = useState();

const {
features,
loading,
clickCoordinate
} = useCoordinateInfo({
queryLayers
});

useEffect(() => {
setMap(new OlMap({
layers: [
new OlLayerTile({
name: 'OSM',
source: new OlSourceOSM()
}),
vectorLayer,
wmsLayer
],
view: new OlView({
center: fromLonLat([-99.4031637, 38.3025695]),
zoom: 4
})
}));
}, []);

if (!map) {
return null;
}

return (
<MapContext.Provider value={map}>
<MapComponent
map={map}
style={{
height: '400px'
}}
/>
{
features.length > 0 ?
<div>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between'
}}
>
<Spin
spinning={loading}
>
<Statistic
title="Coordinate"
value={clickCoordinate.join(', ')}
/>
</Spin>
<Tooltip
title="Copy to clipboard"
>
<Button
style={{marginTop: 16}}
type="primary"
onClick={() => {
copy(clickCoordinate.join(', '));
}}
icon={
<FontAwesomeIcon
icon={faCopy}
/>
}
/>
</Tooltip>
</div>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between'
}}
>
<Spin
spinning={loading}
>
<Statistic
title="State"
value={getValue(features[0].feature, 'STATE_NAME')
|| getValue(features[0].feature, 'name')}
/>
</Spin>
<Tooltip
title="Copy to clipboard"
>
<Button
style={{marginTop: 16}}
type="primary"
onClick={() => {
copy(features[0].feature.get('STATE_NAME'));
}}
icon={
<FontAwesomeIcon
icon={faCopy}
/>
}
/>
</Tooltip>
</div>
</div> :
<span>
Click on a state to get details about the clicked coordinate.
</span>
}
</MapContext.Provider>
);
};

<CoordinateInfoExample/>;
```

0 comments on commit 790e91b

Please sign in to comment.