index.tsx
7.0 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { useState, useEffect } from 'react';
import { Input, Space, Select, Divider, Cascader } from 'antd';
import AMapLoader from '@amap/amap-jsapi-loader';
import './index.less';
import areaData from '@/utils/areaData';
import { getAreaData, getAreaNamesByCodes } from '@/utils/format';
import $ from 'jquery';
// 获取平铺的地区数据
const areaDatas = getAreaData(areaData);
declare global {
interface Window {
_AMapSecurityConfig: any;
AMapUI: any;
AMap: any;
}
}
const { Search } = Input;
window._AMapSecurityConfig = {
securityJsCode: 'f4e73616d1028a16dad9556a0d493571',
};
const MapRender: React.FC = (props: any) => {
const [map, setMap] = useState<any>(null);
const [marker, setMarker] = useState<any>(null);
const [infoWindow, setInfoWindow] = useState<any>(null);
const [mapVisble, setMapVisble] = useState<boolean>(true);
const { poiPicker, setPoiPicker, open, setSelectCode, addressName } = props;
const initMap = async () => {
const AMap = await AMapLoader.load({
key: '6b7ba7695c5c2c76d10610a3be45a0f1', // 申请好的Web端开发者Key,首次调用 load 时必填
version: '1.4.15', // 指定要加载的 JS API 的版本,缺省时默认为 1.4.15
plugins: ['AMap.Scale'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
AMapUI: {
version: '1.0',
},
});
if (AMap) {
if (!map) {
const tempMap = new AMap.Map(addressName + 'map');
setMap(tempMap);
tempMap.addControl(new AMap.Scale());
}
if (!marker) {
setMarker(new AMap.Marker());
}
if (!infoWindow) {
setInfoWindow(
new AMap.InfoWindow({
offset: new AMap.Pixel(0, -20),
}),
);
}
}
window.AMapUI.setDomLibrary($);
if (window.AMapUI) {
window.AMapUI.loadUI(['misc/PoiPicker', 'misc/PositionPicker'], function (PoiPicker: any) {
if (!poiPicker) {
const tempPoiPicker = new PoiPicker({
//city:'北京',
input: addressName + 'search',
});
setPoiPicker(tempPoiPicker);
}
});
}
};
const initWindowMap = async () => {
if (!map) {
const tempMap = new window.AMap.Map(addressName + 'map', {
zoom: 10,
});
setMap(tempMap);
}
if (!marker) {
// @ts-ignore
setMarker(new window.AMap.Marker());
}
if (!infoWindow) {
setInfoWindow(
new window.AMap.InfoWindow({
offset: new window.AMap.Pixel(0, -20),
}),
);
}
if (window.AMapUI) {
window.AMapUI.loadUI(['misc/PoiPicker', 'misc/PositionPicker'], function (PoiPicker: any) {
if (!poiPicker) {
const tempPoiPicker = new PoiPicker({
//city:'北京',
input: addressName + 'search',
});
setPoiPicker(tempPoiPicker);
}
});
}
};
useEffect(() => {
const search = document.getElementById(addressName + 'search');
search?.focus();
}, [document.getElementById(addressName + 'search')]);
useEffect(() => {
if (document.getElementById(addressName + 'map')) {
if (window.AMap) {
initWindowMap();
} else {
initMap();
}
} else {
setMapVisble(!mapVisble);
}
if (poiPicker) {
//初始化poiPicker
poiPicker.on('poiPicked', function (poiResult: any) {
const { onChange, setOpen } = props;
const poi = poiResult.item;
const { adcode }: { adcode: string } = poi;
let provideCode = adcode.substring(0, 2) + '0000';
let cityCode = adcode.substring(0, 4) + '00';
let areaCode = adcode;
map.clearMap();
marker.setMap(map);
infoWindow.setMap(map);
marker.setPosition(poi.location);
infoWindow.setPosition(poi.location);
// 根据code获取省市区名称
const info = getAreaNamesByCodes([provideCode, cityCode, areaCode], areaDatas);
// infoWindow.setContent(
// 'POI信息: <pre>' + JSON.stringify(info, null, 2) + '</pre>',
// );
// infoWindow.open(map, marker.getPosition());
map.setCenter(marker.getPosition());
const { location = {}, address = '' } = poi;
const { lng, lat } = location;
const [provide, city, district] = info;
if (onChange) {
// 给表单组件赋值
onChange({
address,
province_code: provideCode,
city_code: cityCode,
district_code: areaCode,
provide,
city,
district,
lng: lng + '',
ltg: lat + '',
});
}
setSelectCode([provideCode, cityCode, areaCode]);
setOpen(false);
});
}
}, [open, poiPicker, mapVisble]);
return <div className="map" id={addressName + 'map'}></div>;
};
const MapSelect = (props: any) => {
const { value = {}, addressName = '', cascaderStyle = {}, selectStyle = {} } = props;
const [poiPicker, setPoiPicker] = useState<any>(null);
const [open, setOpen] = useState(false);
const [searchValue, setSearchValue] = useState('');
const [selectCode, setSelectCode] = useState<any>([]);
const { province_code, city_code, district_code } = value;
useEffect(() => {
if (province_code && city_code && district_code) {
setSelectCode([province_code, city_code, district_code]);
}
}, [district_code]);
useEffect(() => {
setSearchValue(value?.address);
}, [value?.address]);
return (
<Space.Compact style={{ width: '100%' }}>
<Cascader
options={areaData}
style={cascaderStyle}
value={selectCode}
placeholder="省/市/区"
disabled
/>
<Select
open={open}
value={value?.address}
dropdownMatchSelectWidth={false}
style={selectStyle}
placeholder="请输入关键字选取地点"
onDropdownVisibleChange={(isopen) => {
if (isopen) {
setOpen(true);
return;
}
setOpen(false);
}}
dropdownRender={() => (
<div style={{ overflow: 'auto' }}>
<Search
id={addressName + 'search'}
placeholder="请输入关键字选取地点"
value={searchValue}
onPressEnter={(e: any) => {
if (poiPicker && e.target.value) {
poiPicker.searchByKeyword(e.target.value);
}
setSearchValue(e.target.value);
}}
onChange={(e) => {
setSearchValue(e.target.value);
}}
// onSearch={(value) => {}}
/>
<Divider style={{ margin: '8px 0' }} />
<MapRender
{...props}
open={open}
setOpen={setOpen}
poiPicker={poiPicker}
setPoiPicker={setPoiPicker}
setSelectCode={setSelectCode}
/>
</div>
)}
/>
</Space.Compact>
);
};
export default MapSelect;