export class GetLocation extends React.Component{
_geolocation(){
const options = {
enableHighAccuracy: true,
timeout : 8000,
maximumAge: 1000
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setCurrentAdress(position.coords.longitude,position.coords.latitude);
},
(error) => {
const errorTypes={1:"位置服务被拒绝", 2:"获取不到位置信息", 3:"获取位置信息超时"};
this.props.getAddress(errorTypes[error.code])
}
);
}
else {
alert("你的浏览器不支持!");
}
}
setCurrentAdress(lon,lat){
let gc = new BMap.Geocoder();
let point = new BMap.Point(lon,lat)
gc.getLocation(point, (rs) => {
this.props.getAddress(rs.address)
});
}
render(){
return(
<BaiduMap id="getlocation" style={{display:'none'}} />
)
}
}
export default class MobilefieldSignInfo extends React.Component{
constructor(){
super();
this.state = {
address : ''
};
this.getLocation = this.getLocation.bind(this);
this.onAddressChanged = this.onAddressChanged.bind(this);
}
getLocation(){
this.setState({
address : ''
},this.setState({
address : this.refs.location._geolocation()
}))
}
componentDidMount(){
this.getLocation();
}
onAddressChanged(add){
this.setState({
address : add
})
}
render() {
return (
<Container scrollable>
<MobilefieldSign currentLocation={this.state.address} />
<GetLocation ref="location" getAddress={this.onAddressChanged} />
<Button block radius>签 到</Button>
<Button block radius onClick={this.getLocation} theme="white"><Icon name="refresh" />重新定位</Button>
</Container>
);
}
};
调用百度地图获取当前的地理位置,还没获取到地址就退出当前页面,就会报错
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.
该怎么卸载呢
Just set a _isMounted property to true in componentDidMount and set it to false in componentWillUnmount, and use this variable to check your component's status.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。