antv-l7/examples/react/layer/demo/popup.tsx

111 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-11-21 03:51:18 +08:00
import {
AMapScene,
LayerEvent,
LineLayer,
MapboxScene,
PolygonLayer,
Popup,
} from '@antv/l7-react';
import * as React from 'react';
import ReactDOM from 'react-dom';
const World = React.memo(function Map() {
2020-11-21 14:05:59 +08:00
const [popupInfo, setPopInfo] = React.useState();
2020-11-21 03:51:18 +08:00
const hoverHandle = (e) => {
2020-11-21 14:05:59 +08:00
console.log(e);
setPopInfo(e);
2020-11-21 03:51:18 +08:00
};
const hoverOutHandle = () => {
setPopInfo(undefined);
};
const data = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[111.9287109375, 28.22697003891834],
[115.6640625, 28.22697003891834],
[115.6640625, 31.015278981711266],
[111.9287109375, 31.015278981711266],
[111.9287109375, 28.22697003891834],
],
],
},
},
],
};
return (
<AMapScene
map={{
2020-11-21 14:05:59 +08:00
center: [111.9287109375, 28.22697003891834],
2020-11-21 03:51:18 +08:00
pitch: 0,
style: 'dark',
zoom: 6,
}}
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
>
{data && [
<PolygonLayer
key={'2'}
options={{
2020-11-21 14:05:59 +08:00
autoFit: false,
2020-11-21 03:51:18 +08:00
}}
source={{
data,
}}
color={{
values: '#2E8AE6',
}}
shape={{
values: 'fill',
}}
style={{
opacity: 0.5,
}}
>
<LayerEvent type="mousemove" handler={hoverHandle} />
<LayerEvent type="mouseout" handler={hoverOutHandle} />
</PolygonLayer>,
<LineLayer
key={'21'}
source={{
data,
}}
color={{
values: '#2E8AE6',
}}
shape={{
values: 'line',
}}
style={{
opacity: 1,
}}
/>,
]}
2020-11-21 14:05:59 +08:00
{popupInfo && (
<Popup
key="popup"
2021-05-27 19:53:22 +08:00
// @ts-ignore
2020-11-21 14:05:59 +08:00
lnglat={popupInfo.lngLat}
option={{ closeButton: false, offsets: [0, 10] }}
>
<span></span>
</Popup>
)}
2020-11-21 03:51:18 +08:00
</AMapScene>
);
});
ReactDOM.render(<World />, document.getElementById('map'));