forked from gzzfw/frontEnd/gzDyh

zhangyongtian
2024-08-27 33440bb15f8d7f07b68e34760129d3300cab5e88
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * @Author: dminyi 1301963064@qq.com
 * @Date: 2024-08-17 14:41:57
 * @LastEditors: dminyi 1301963064@qq.com
 * @LastEditTime: 2024-08-23 17:06:13
 * @FilePath: \gzDyh\gz-customerSystem\src\views\register\visit\component\map.jsx
 * @Description: 地图
 */
import React, { useEffect, useRef, useState } from 'react';
import { Map, Marker, NavigationControl, InfoWindow } from 'react-bmapgl';
import { Form, Input, Button, Message } from '@arco-design/web-react';
import { Row, Col } from 'antd';
const FormItem = Form.Item;
 
const formItemLayout = {
  labelCol: {
    span: 4,
  },
  wrapperCol: {
    span: 17,
  },
};
 
export default function MapView(props) {
  const mapRef = useRef()
  const formRef = useRef()
  const [addressList, setAddressList] = useState([])
 
 
  useEffect(() => {
    if (mapRef.current) {
      let geolocation = new window.BMapGL.Geolocation();
      geolocation.getCurrentPosition((r) => {
        if (geolocation.getStatus() === 0) {
          handleAnalysis(r.point);
        } else {
          Message.warning(`Failed with status ${geolocation.getStatus()}`);
        }
      });
    }
  }, [mapRef]);
 
  const handleSubmit = () => {
    if (formRef.current) {
      formRef.current.validate(undefined, (errors, values) => {
        if (!errors) {
          var myGeo = new window.BMapGL.Geocoder();
 
          myGeo.getPoint(values.name, (point) => {
            if (point) {
              mapRef.current.map.centerAndZoom(point, 15);
              mapRef.current.map.addOverlay(new window.BMapGL.Marker(point, { title: values.name }));
              handleAnalysis(point, values.name); // 添加地点名称
              // searchNearbyPOIs(point);
            } else {
              Message.warning('您输入的地址没有解析到结果!');
            }
          }, '广州市');
        }
      });
    }
  };
 
  // 解析地址为中文
  const handleAnalysis = (pt, name) => {
    let geoc = new window.BMapGL.Geocoder();
    geoc.getLocation(pt, (rs) => {
      console.log(rs, 'rsss')
      let addComp = rs.addressComponents;
      let addName = `${addComp.province}${addComp.city}${addComp.district}${addComp.street}${addComp.streetNumber}`;
      let surroundingPois = rs.surroundingPois;
      if (name) {
        addName += ` ${name}`; // 添加地点名称
      }
      mapRef.current.map.centerAndZoom(pt, 15);
      mapRef.current.map.addOverlay(new window.BMapGL.Marker(pt, { title: addName }));
      setAddressList(surroundingPois);
    });
  };
 
  // 搜索附近的POI
  const searchNearbyPOIs = (centerPoint) => {
    const radius = 10; // 半径10米
    const circle = new window.BMapGL.Circle(centerPoint, {
      strokeColor: "#FF0000",
      strokeOpacity: 0.9,
      strokeWeight: 2,
      fillColor: "#FF0000",
      fillOpacity: 0.1,
      radius: radius
    });
 
    mapRef.current.map.addOverlay(circle);
 
    const poiSearch = new window.BMapGL.PoiSearch(mapRef.current.map, {
      searchComplete: function (results) {
        if (results.status === window.BMapGL.RESULT_SUCCESS) {
          const pois = results.pois.map(poi => poi.name);
          setAddressList([...addressList, ...pois]);
        }
      }
    });
 
    poiSearch.searchInCircle('', circle);
  };
 
 
 
  console.log(addressList, 'addressList')
 
  return (
    <div>
      <Row gutter={[16, 0]}>
        <Col span={16}>
          <Form
            ref={formRef}
            layout='inline'
            {...formItemLayout}
            style={{ marginBottom: '8px' }}
          >
            <FormItem
              label='查询位置:'
              field='name'
            >
              <Input placeholder='请输入' style={{ width: '522px' }} />
            </FormItem>
            <Button style={{ marginRight: '20px' }}>
              重置
            </Button>
            <Button
              type="primary"
              onClick={handleSubmit}
            >
              查询
            </Button>
          </Form>
          <Map
            ref={mapRef}
            zoom="15"
            enableScrollWheelZoom
            onClick={(e) => {
              let pt = e.latlng;
              handleAnalysis(pt, null);
            }}
          >
          </Map>
        </Col>
        <Col span={8}>
          <div style={{ color: '#86909C', marginTop: '43px' }}>附近地址</div>
          <div style={{ height: 'calc(100% - 64px)', overflowY: 'scroll' }}>
            {addressList?.map((item, index) => (
              <div key={index}>{item.address}&nbsp;&nbsp;&nbsp;&nbsp;{item.title}</div>
            ))}
          </div>
        </Col>
      </Row>
    </div>
  )
}