forked from gzzfw/frontEnd/gzDyh

dminyi
2024-09-03 ce7871581ad440a9869118a49746f66e850f502e
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * @Author: dminyi 1301963064@qq.com
 * @Date: 2024-08-17 14:41:57
 * @LastEditors: dminyi 1301963064@qq.com
 * @LastEditTime: 2024-08-27 10:17:49
 * @FilePath: \gzDyh\gz-customerSystem\src\views\register\visit\component\map.jsx
 * @Description: 地图
 */
import React, { useEffect, useRef, useState } from 'react';
import { Map, CustomOverlay } from 'react-bmapgl';
import { Form, Input, Button, Message, Spin } from '@arco-design/web-react';
 
import { Row, Col } from 'antd';
const FormItem = Form.Item;
 
const formItemLayout = {
  labelCol: {
    span: 4,
  },
  wrapperCol: {
    span: 17,
  },
};
 
const BMapGL = window.BMapGL
 
// 自定义Marker的icon  
let myIcon = new BMapGL.Icon(require("@/assets/images/icon/frame.png"), new BMapGL.Size(24, 24), {
  // 是否允许缩放图标  
  enableScaling: true
});
 
export default function MapView(props) {
  const mapRef = useRef()
  const formRef = useRef()
  const [addressList, setAddressList] = useState([])
  const [addressData, setAddressData] = useState({})
  const [selectData, setSelectData] = useState({})
  const [loading, setLoading] = useState(false)
 
  useEffect(() => {
    //初次进入定位在当前浏览器
    if (mapRef.current) {
      setLoading(true)
      let geolocation = new BMapGL.Geolocation();
      geolocation.getCurrentPosition((r) => {
        if (geolocation.getStatus() === 0) {
          handleAnalysis(r.point);
        } else {
          Message.warning(`Failed with status ${geolocation.getStatus()}`);
        }
        setLoading(false)
      }, '广州市');
    }
  }, [mapRef]);
 
  //查询
  const handleSubmit = (addItem) => {
    if (formRef.current) {
      formRef.current.validate(undefined, (errors, values) => {
        if (!errors) {
          var myGeo = new BMapGL.Geocoder();
          myGeo.getPoint(values.name, (point) => {
            if (point) {
              handleAnalysis(point, addItem); // 添加地点名称
            } else {
              Message.warning('您输入的地址没有解析到结果!');
            }
          }, '广州市');
        }
      });
    }
  };
 
  // 解析地址为中文
  const handleAnalysis = (pt, addItem) => {
    if (!addItem) {
      let geoc = new BMapGL.Geocoder();
      geoc.getLocation(pt, (rs) => {
        let addComp = rs.content;
        let addName = `${addComp.address}  ${addComp.poi_desc}`;
        let surroundingPois = rs.surroundingPois;
        mapRef.current?.map.centerAndZoom(pt, 15);//地图指向中心
        mapRef.current?.map.clearOverlays();//清除所有标点
        mapRef.current?.map.addOverlay(new BMapGL.Marker(pt, { icon: myIcon }));//添加标点
        setAddressList(surroundingPois);
        setAddressData({
          pt,
          addName
        })
      });
    } else {
      mapRef.current?.map.centerAndZoom(addItem.point, 15);//地图指向中心
      mapRef.current?.map.clearOverlays();//清除所有标点
      mapRef.current?.map.addOverlay(new BMapGL.Marker(addItem.point, { icon: myIcon }));//添加标点
      setAddressData({
        pt: addItem.point,
        addName: addItem.address + addItem.title
      })
    }
 
  };
 
  //点击附近地址
  const clickVicinity = (data) => {
    const address = data.address + data.title
    setSelectData({
      uid: data.uid,
      address
    })
    formRef.current.setFieldValue('name', address)
    handleSubmit(data)
  }
 
  //使用地址
  const handleUseAddress = (e) => {
    e.stopPropagation()
    props.selectAdd(addressData)
  }
 
  return (
    <Spin tip='正在获取附近地址...' loading={loading} style={{ width: '100%', height: '100%' }}>
      <Row
        gutter={[16, 0]}
        onKeyDown={(event) => {
          if (event.key === 'Enter') {
            event.preventDefault();
            handleSubmit(false)
          }
        }}
      >
        <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(false) }}
            >
              查询
            </Button>
          </Form>
          <Map
            ref={mapRef}
            zoom="15"
            enableScrollWheelZoom
            onClick={(e) => {
              let pt = e.latlng;
              handleAnalysis(pt);
            }}
            center={new BMapGL.Point(113.27142900886543, 23.13533647285972)}
          >
            {addressData.pt && <CustomOverlay
              position={addressData.pt}
              offset={{ width: -91, height: -25 }}
            >
              <div className='mapPopconfirm'>
                <div style={{marginBottom: '10px'}}>{addressData.addName}</div>
                <div style={{ width: '100%', display: 'flex', justifyContent: 'flex-end' }}>
                  <div className='mapPopconfirm-btn' onClick={handleUseAddress}>使用地址</div>
                </div>
              </div>
            </CustomOverlay>}
          </Map>
        </Col>
        <Col span={8}>
          <div style={{ color: '#86909C', marginTop: '43px' }} >附近地址</div>
          <div style={{ height: '343px', overflowY: 'scroll', paddingRight: '10px' }}>
            {addressList?.map(item => (
              <div
                key={item.uid}
                className='mapVicinity'
                onClick={() => { clickVicinity(item) }}
                style={{ color: selectData.uid === item.uid ? '#1A6FB8' : '' }}
              >
                <div style={{ marginRight: '5px', flex: 1 }}>{item.address}&nbsp;&nbsp;&nbsp;&nbsp;{item.title}</div>
                {selectData.uid === item.uid ? <div className='mapVicinityIcon'></div> : <div style={{ width: '14px' }}></div>}
              </div>
            ))}
          </div>
        </Col>
      </Row>
    </Spin>
  )
}