MapField.js
9.9 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
245
246
247
248
249
250
251
252
253
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';
import cx from 'classnames';
import s from './CustomForm.scss';
import {Row,Col,Button} from 'react-bootstrap';
import DropdownPicker from '../DropdownPicker';
import NumberField from './NumberField';
class MapField extends React.Component {
constructor(props) {
super(props);
this.state={
editCircleRang:false,
loc_name:null,
loc_nation:'中华人民共和国',
loc_province:null,
loc_city:null,
loc_longitude:null,
loc_latitude:null,
loc_float_range:100,
options:[]
}
this.localeChange=this.localeChange.bind(this);
this.setLngAndLat=this.setLngAndLat.bind(this);
this.openFloatRange=this.openFloatRange.bind(this);
this.closeFloatRange=this.closeFloatRange.bind(this);
this.changeLocal=this.changeLocal.bind(this);
this.setValues=this.setValues.bind(this);
this.updateLocal=this.updateLocal.bind(this);
this.backLocation=this.backLocation.bind(this);
}
static propTypes = {
displayName:PropTypes.string,
fields:PropTypes.object,
validateState:PropTypes.string,
inlineFlag:PropTypes.bool,
placeholder:PropTypes.string
}
static defaultProps={
inlineFlag:true,
multiselect:false
}
validationState(field){
if(field.error&&field.touched){
return 'error';
}else if(field.touched)
return '';
}
setValues(values,lnglat){
const {fields:{city,float_range,latitude,longitude,name,nation,province}}=this.props;
this.setState({
loc_name:values.formattedAddress,
loc_province:values.addressComponent.province,
loc_city:values.addressComponent.city,
loc_longitude:lnglat.lng,
loc_latitude:lnglat.lat,
});
name.onChange(values.formattedAddress);
nation.onChange(this.state.loc_nation);
province.onChange(values.addressComponent.province);
city.onChange(values.addressComponent.city);
longitude.onChange(lnglat.lng);
latitude.onChange(lnglat.lat);
float_range.onChange(this.state.loc_float_range);
}
changeLocal(e){
const {fields:{name}}=this.props;
this.setState({
loc_name:e.target.value
})
name.onChange(e.target.value);
}
closeFloatRange(){
this.setState({
editCircleRang:false
})
this.editCircle.close();
}
openFloatRange(){
this.setState({
editCircleRang:true
})
this.editCircle.open();
}
updateLocal(e,target){
const {fields:{float_range,latitude,longitude}}=this.props;
const center=e.target.getCenter();
const radius=e.target.getRadius();
this.setState({
loc_longitude:center.lng,
loc_latitude:center.lat,
loc_float_range:radius
});
longitude.onChange(center.lng);
latitude.onChange(center.lat);
float_range.onChange(radius);
}
setLngAndLat(e){
const {editCircleRang,loc_name,loc_float_range}=this.state,self=this;
const lnglat={lng:e.lnglat.getLng(),lat:e.lnglat.getLat()}
if(editCircleRang)
return;
if(this.circle){
this.mapContainer.remove(this.circle);
}
this.circle = new AMap.Circle({
center: new AMap.LngLat(e.lnglat.getLng(),e.lnglat.getLat()),// 圆心位置
radius: loc_float_range, //半径
strokeColor: "#F33", //线颜色
strokeOpacity: 1, //线透明度
strokeWeight: 1, //线粗细度
fillColor: "#ee2200", //填充颜色
fillOpacity: 0.15//填充透明度
});
this.circle.setMap(this.mapContainer);
AMap.plugin('AMap.CircleEditor',function(){//回调函数
self.editCircle=new AMap.CircleEditor(self.mapContainer,self.circle);
self.editCircle.on('end',self.updateLocal);
});
const lnglatXY=[e.lnglat.getLng(),e.lnglat.getLat()];
this.geocoder.getAddress(lnglatXY,function(status,result){
if (status === 'complete' && result.info === 'OK') {
if(!loc_name){
self.placeSearch.setCity(result.regeocode.addressComponent.adcode);
self.placeSearch.search(result.regeocode.formattedAddress);
}
self.setValues(result.regeocode,lnglat);
}
});
}
localeChange(e){
const {fields:{name}}=this.props;
this.placeSearch.setCity(e.poi.adcode);
this.placeSearch.search(e.poi.name);
this.setState({
loc_name:e.poi.name
})
name.onChange(e.target.value);
}
backLocation(){
const {fields:{float_range,latitude,longitude,name}}=this.props,self=this;
if(!longitude.value||!latitude.value){
return
}
if(this.circle){
this.mapContainer.remove(this.circle);
}
this.circle = new AMap.Circle({
center: new AMap.LngLat(longitude.value,latitude.value),// 圆心位置
radius: float_range.value, //半径
strokeColor: "#F33", //线颜色
strokeOpacity: 1, //线透明度
strokeWeight: 1, //线粗细度
fillColor: "#ee2200", //填充颜色
fillOpacity: 0.15//填充透明度
});
this.circle.setMap(this.mapContainer);
AMap.plugin('AMap.CircleEditor',function(){//回调函数
self.editCircle=new AMap.CircleEditor(self.mapContainer,self.circle);
self.editCircle.on('end',self.updateLocal);
});
this.setState({
loc_name:name.value
});
this.mapContainer.setZoom(14);
this.mapContainer.setCenter([longitude.value,latitude.value]);
}
componentWillReceiveProps(nextProps){
if(!this.state.loc_name){
this.backLocation();
}
}
componentDidMount(){
const width='100%',height=300,padding=0,self=this;
$(this.refs['mapContainer']).append(`<div id='map_container_wrap' style='width:${width-padding*2}px;height:${height-padding*2}px'></div>`);
this.mapContainer=new AMap.Map('map_container_wrap',{
resizeEnable: true
});
const autoOptions={
input:'mapSearchInput'
};
AMap.plugin('AMap.Autocomplete',function(){//回调函数
const auto= new AMap.Autocomplete(autoOptions);
AMap.event.addListener(auto, "select", self.localeChange);
});
AMap.plugin('AMap.PlaceSearch',function(){//回调函数
self.placeSearch=new AMap.PlaceSearch({
map:self.mapContainer
});
});
AMap.plugin('AMap.Geocoder',function(){//回调函数
self.geocoder=new AMap.Geocoder({
radius: 1000,
extensions: "all"
});
self.backLocation();
});
AMap.plugin('AMap.ToolBar',function(){//回调函数
self.toolBar=new AMap.ToolBar({
visible: true
});
self.mapContainer.addControl(self.toolBar);
});
this.mapContainer.on('click',self.setLngAndLat)
}
componentWillUnmount() {
this.mapContainer.destroy();
}
render(){
const {displayName,placeholder,inlineFlag,fields:{name,nation,province,city,longitude,latitude,float_range}}=this.props,self=this;
const {editCircleRang,options,loc_name,loc_nation,loc_province,loc_city,loc_longitude,loc_latitude,loc_float_range}=this.state;
let validateState=this.validationState(name);
return(
<div>
<Row>
<Col md={5}>
<div className={cx(s.field_wrap,'form-group',{
'has-success':validateState=='success',
'has-warning':validateState=='warning',
'has-error':validateState=='error',
'row':inlineFlag,
'form-horizontal':inlineFlag
})}>
<label className={cx('control-label',{
'col-sm-3':inlineFlag
})}>工作地点</label>
<div className={cx(s.input_wrap,{
'col-sm-9':inlineFlag
})} >
<input onChange={this.changeLocal} placeholder='点击输入' value={loc_name} className={cx(s.map_serach_input,'form-control')} id='mapSearchInput' ref='mapSearchInput' type="text"/>
<span className={cx(s.input_help,'help-block')}><small>{name.touched?name.error:''}</small></span>
</div>
</div>
</Col>
<Col md={5}>
<NumberField customWidth={5} inlineFlag={true} readOnly={true} defaultValue={loc_float_range} displayName='当前可打卡范围' field={float_range} units='米' ></NumberField>
</Col>
<Col md={2}>
{!editCircleRang&&<Button bsStyle="success" onClick={this.openFloatRange}>调整可打卡范围</Button>}
{editCircleRang&&<Button bsStyle="success" onClick={this.closeFloatRange}>确认</Button>}
</Col>
</Row>
<div ref='mapContainer'>
</div>
</div>
)
}
}
export default MapField;