|
@@ -0,0 +1,128 @@
|
|
1
|
+import React from 'react';
|
|
2
|
+import ReactDOM from 'react-dom';
|
|
3
|
+import { Map, Marker } from 'react-amap'
|
|
4
|
+import debounce from 'lodash.debounce'
|
|
5
|
+import styles from './style.less'
|
|
6
|
+
|
|
7
|
+const getPosFromProps = props => {
|
|
8
|
+ if (props.value) {
|
|
9
|
+ const temp = props.value.split(',')
|
|
10
|
+ return { longitude: temp[0], latitude: temp[1] }
|
|
11
|
+ } else {
|
|
12
|
+ return null
|
|
13
|
+ }
|
|
14
|
+}
|
|
15
|
+
|
|
16
|
+class AMapSearch extends React.PureComponent {
|
|
17
|
+
|
|
18
|
+ constructor(props) {
|
|
19
|
+ super(props);
|
|
20
|
+ this.state = {
|
|
21
|
+ markerPosition: getPosFromProps(props),
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ // 高德地图 Marker 实例
|
|
25
|
+ this.markerInstance = undefined
|
|
26
|
+ // 高德地图 Map 实例
|
|
27
|
+ this.mapInstance = undefined
|
|
28
|
+
|
|
29
|
+ this.amapEvents = {
|
|
30
|
+ created: mapInstance => {
|
|
31
|
+ this.mapInstance = mapInstance
|
|
32
|
+
|
|
33
|
+ AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.CitySearch'], () => {
|
|
34
|
+ // 实例化Autocomplete
|
|
35
|
+ const autoOptions = { input: 'amapInput' }
|
|
36
|
+ const autoComplete = new AMap.Autocomplete(autoOptions)
|
|
37
|
+ const placeSearch = new AMap.PlaceSearch({ map: mapInstance })
|
|
38
|
+
|
|
39
|
+ // 监听下拉框选中事件
|
|
40
|
+ AMap.event.addListener(autoComplete, 'select', e => {
|
|
41
|
+ // TODO 针对选中的poi实现自己的功能
|
|
42
|
+ placeSearch.setCity(e.poi.adcode)
|
|
43
|
+ placeSearch.search(e.poi.name)
|
|
44
|
+ })
|
|
45
|
+
|
|
46
|
+ const citySearch = new AMap.CitySearch()
|
|
47
|
+ citySearch.getLocalCity((status, result) => {
|
|
48
|
+ if (status === 'complete' && result.info === 'OK') {
|
|
49
|
+ // 查询成功,result即为当前所在城市信息
|
|
50
|
+ console.log('当前所在城市:', result)
|
|
51
|
+ if (result && result.city && result.bounds) {
|
|
52
|
+ // 当前城市位置信息
|
|
53
|
+ const citybounds = result.bounds;
|
|
54
|
+ // 地图显示当前城市
|
|
55
|
+ mapInstance.setBounds(citybounds);
|
|
56
|
+ // 需要在设置坐标成功后,重新设置 缩放级别
|
|
57
|
+ // mapInstance.setZoom(15)
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ })
|
|
61
|
+
|
|
62
|
+ // 搜索结果点击
|
|
63
|
+ placeSearch.on('markerClick', debounce(e => {
|
|
64
|
+ const { location: loc } = e.data
|
|
65
|
+ const lngLat = `${loc.lng},${loc.lat}`
|
|
66
|
+ this.props.onChange(lngLat, e)
|
|
67
|
+ }, 300, { leading: true }))
|
|
68
|
+ })
|
|
69
|
+
|
|
70
|
+ // 实例点击事件
|
|
71
|
+ mapInstance.on('click', debounce(e => {
|
|
72
|
+ const lngLat = `${e.lnglat.getLng()},${e.lnglat.getLat()}`
|
|
73
|
+ this.props.onChange(lngLat, e)
|
|
74
|
+ }, 300, { leading: true }));
|
|
75
|
+ },
|
|
76
|
+ };
|
|
77
|
+
|
|
78
|
+ this.markerEvents = {
|
|
79
|
+ created: markerInstance => {
|
|
80
|
+ this.markerInstance = markerInstance
|
|
81
|
+ console.log('--->',markerInstance)
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+ // this.markerPosition = { longitude: 120, latitude: 30 };
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ static getDerivedStateFromProps(props, state) {
|
|
88
|
+ if (props.value && !state.markerPosition) {
|
|
89
|
+ return { markerPosition: getPosFromProps(props) }
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ return null
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ componentDidUpdate(prevProps) {
|
|
96
|
+ if (this.props.value !== prevProps.value) {
|
|
97
|
+ if (this.props.value) {
|
|
98
|
+ this.setState({ markerPosition: getPosFromProps(this.props) }, () => {
|
|
99
|
+ if (this.mapInstance) {
|
|
100
|
+ // 需要在设置坐标成功后,重新设置 缩放级别
|
|
101
|
+ // this.mapInstance.setZoom(15)
|
|
102
|
+ }
|
|
103
|
+ })
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ render() {
|
|
109
|
+ return (
|
|
110
|
+ <>
|
|
111
|
+ <div style={{ width: '100%', height: '100%', minHeight: '400px', position: 'relative' }}>
|
|
112
|
+ {/* zoom={15} 设置后,无效,不知道什么原因,必须手动设置 */}
|
|
113
|
+ <Map plugins={['ToolBar']} events={this.amapEvents} amapkey={this.props.amapkey} center={this.state.markerPosition}>
|
|
114
|
+ <Marker position={this.state.markerPosition} events={this.markerEvents} clickable />
|
|
115
|
+ </Map>
|
|
116
|
+ {
|
|
117
|
+ <div className={styles.infoBox}>
|
|
118
|
+ <span className={styles.inputText}>请输入关键字</span>
|
|
119
|
+ <input id="amapInput" className={styles.input} type="text" />
|
|
120
|
+ </div>
|
|
121
|
+ }
|
|
122
|
+ </div>
|
|
123
|
+ </>
|
|
124
|
+ )
|
|
125
|
+ }
|
|
126
|
+}
|
|
127
|
+
|
|
128
|
+export default AMapSearch
|