提交 d6f3b251 编写于 作者: ItbGcthate's avatar ItbGcthate

304_change geolocation

上级 b9f198ba
......@@ -27,9 +27,8 @@ class TwoPlayerPKHome extends Component {
const data = JSON.parse(msg.data);
console.log(data);
if (false) {
const opponent = null;
//this.props.navigation.navigate('双人PK结算');
this.props.navigation.navigate('双人PK中', {opponent: opponent});
this.props.navigation.navigate('双人PK中');
this.setState({isStarted: false, isCanceling: false, opponent: null});
}
if (data.hasOwnProperty('type') && data.type === 'MATCH_CANCEL_SUCCESS') {
......
......@@ -44,10 +44,7 @@ class TwoPlayerPKStudy extends Component {
};
componentDidMount() {
const {
navigation,
route: {params},
} = this.props;
const {navigation} = this.props;
navigation.setOptions({
// 引入SettingButton组件至header的右边
// 并把this.props传给SettingButton组件以实现Navigation的跳转
......@@ -59,9 +56,6 @@ class TwoPlayerPKStudy extends Component {
this.forceUpdate();
});
// 以下不是标语风格切换相关
if (params.opponent !== undefined && params.opponent !== null) {
this.setState({opponent: params.opponent, isStarted: true});
}
this.startTimer();
}
......
global.defaultRangeID = 'notLocated'; // default value of rangeID
global.acID = -1; // a acID of range PK
global.rangeID = global.defaultRangeID; // rangeID for school buildings
global.websocket = new WebSocket('ws://localhost:8080'); // an instance of websocket
global.component = {};
......@@ -68,6 +68,20 @@ const LocationTable = {
},
};
export const DefaultRangeID = global.defaultRangeID;
export class rangeID {
static get() {
return global.rangeID;
}
static set(input) {
global.rangeID = input;
}
static setByLocation(longitude, latitude) {
this.set(parseLocation(longitude, latitude));
}
}
export class acID {
static get() {
return global.acID;
......@@ -77,6 +91,15 @@ export class acID {
}
}
export class component {
static get() {
return global.component;
}
static set(input) {
global.component = input;
}
}
export class websocket {
static get() {
return global.websocket;
......@@ -196,22 +219,22 @@ export class sno {
}
}
export function isLegalRangeID(rangeID) {
export function isLegalRangeID() {
return (
LocationTable.hasOwnProperty(rangeID) &&
rangeID !== 'notInRange' &&
rangeID !== 'notLocated'
LocationTable.hasOwnProperty(rangeID.get()) &&
rangeID.get() !== 'notInRange' &&
rangeID.get() !== 'notLocated'
);
}
export function parseRangeID(rangeID) {
// console.log('parseRangeID rangID: ' + rangeID);
if (LocationTable.hasOwnProperty(rangeID)) {
rangeID = LocationTable[rangeID].name;
export function parseRangeID() {
let rangeName;
if (LocationTable.hasOwnProperty(rangeID.get())) {
rangeName = LocationTable[rangeID.get()].name;
} else {
rangeID = 'error';
rangeName = 'error';
}
return rangeID;
return rangeName;
}
function getRadian(angle) {
......@@ -238,7 +261,7 @@ function getDistance(longitude1, latitude1, longitude2, latitude2) {
return distance;
}
export function parseLocation(longitude, latitude) {
function parseLocation(longitude, latitude) {
// console.log('longitude: ' + longitude + ' latitude: ' + latitude);
let range = {
ID: 'notInRange',
......@@ -260,7 +283,7 @@ export function parseLocation(longitude, latitude) {
range.distance = distance;
}
}
// console.log(range.ID + ' ' + range.distance);
console.log(range.ID + ' ' + range.distance);
return range.ID;
}
......
......@@ -16,8 +16,9 @@ import Geolocation from '@react-native-community/geolocation';
import {
parseRangeID,
isLegalRangeID,
parseLocation,
acID,
rangeID,
DefaultRangeID,
} from '../MyUtilities';
const {width, height} = Dimensions.get('window');
......@@ -33,18 +34,22 @@ class RangePKHome extends Component {
super();
}
componentDidMount() {
rangeID.set(DefaultRangeID);
}
componentWillUnmount() {
// this.cancelLocating();
// 卸载异步操作设置状态
this.setState = (state, callback) => {
return;
};
rangeID.set(DefaultRangeID);
}
state = {
isStarted: false,
isLocated: false,
rangeID: 'notLocated', // 初始rangeID
longitude: undefined,
latitude: undefined,
};
......@@ -62,25 +67,21 @@ class RangePKHome extends Component {
location => {
const longitude = location.coords.longitude; //经度
const latitude = location.coords.latitude; //纬度
const rangeID = parseLocation(longitude, latitude);
// this.setState({isStarted: false, isLocated: true, rangeID: rangID});
rangeID.setByLocation(longitude, latitude);
this.setState({
isStarted: false,
isLocated: true,
rangeID: rangeID,
longitude: longitude, //经度
latitude: latitude, //纬度
});
},
error => {
reject(error);
this.setState({isStarted: false});
if (error.code == 2) {
if (error.code === 2) {
ToastAndroid.show(
'定位失败,请查看手机是否开启GPS定位服务',
ToastAndroid.SHORT,
);
} else if (error.code == 3) {
} else if (error.code === 3) {
ToastAndroid.show(
'定位超时,请尝试重新获取定位',
ToastAndroid.SHORT,
......@@ -100,12 +101,12 @@ class RangePKHome extends Component {
geoLocate = async () => {
if (Platform.OS === 'ios') {
await this.getPositions().catch(error => {});
await this.getPositions().catch();
} else {
const permissions = [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION];
const granted = await PermissionsAndroid.requestMultiple(permissions);
if (granted['android.permission.ACCESS_FINE_LOCATION'] === 'granted') {
await this.getPositions().catch(error => {});
await this.getPositions().catch();
} else {
ToastAndroid.show('定位权限被禁止', ToastAndroid.SHORT);
}
......@@ -116,7 +117,7 @@ class RangePKHome extends Component {
// // eslint-disable-next-line no-undef
// this.abortController = new AbortController();
this.setState({isStarted: true});
this.geoLocate().then(r => {});
this.geoLocate().then();
};
// cancelLocating = () => {
......@@ -129,16 +130,16 @@ class RangePKHome extends Component {
startStudy = () => {
//this.props.navigation.navigate('');
this.props.navigation.navigate('区域PK中', {rangeID: this.state.rangeID});
this.props.navigation.navigate('区域PK中');
};
render() {
const {isStarted, isLocated, rangeID} = this.state;
const {isStarted, isLocated} = this.state;
return (
<View style={styles.container}>
<Image source={isStarted ? Locating : Located} style={styles.image} />
<Text numberOfLines={1} style={styles.text}>
当前所在区域{parseRangeID(rangeID)}
当前所在区域{parseRangeID()}
</Text>
{isStarted ? (
<MyButton
......@@ -155,7 +156,7 @@ class RangePKHome extends Component {
/>
)}
<MyButton
disabled={!isLegalRangeID(rangeID)}
disabled={!isLegalRangeID()}
containerStyle={styles.myButton.container}
title="开始自习"
onPress={this.startStudy}
......
......@@ -12,7 +12,14 @@ import MyTitle from '../MyTitle';
import SettingButton from '../SettingButton';
import MyBanner from '../MyBanner';
import RangePKState from '../RangePKState';
import {parseMinute, parseRangeID, parseTime, acID} from '../MyUtilities';
import {
parseMinute,
parseRangeID,
parseTime,
acID,
rangeID,
DefaultRangeID,
} from '../MyUtilities';
const {width, height} = Dimensions.get('window');
/**
......@@ -34,7 +41,6 @@ class RangePKStudy extends Component {
}
state = {
rangeID: undefined,
user: {
name: 'username',
rank: 'error',
......@@ -57,10 +63,7 @@ class RangePKStudy extends Component {
};
componentDidMount() {
const {
navigation,
route: {params},
} = this.props;
const {navigation} = this.props;
navigation.setOptions({
// 引入SettingButton组件至header的右边
// 并把this.props传给SettingButton组件以实现Navigation的跳转
......@@ -72,9 +75,6 @@ class RangePKStudy extends Component {
this.forceUpdate();
});
// 以下不是标语风格切换相关
if (params.rangeID !== undefined) {
this.setState({rangeID: params.rangeID});
}
this.startTimer();
}
......@@ -140,8 +140,10 @@ class RangePKStudy extends Component {
numberTop: parseInt(this.state.timeInTheMatch / 60, 10),
numberCentre: 999,
numberBottom: 9999,
range: parseRangeID(this.state.rangeID),
range: parseRangeID(),
});
rangeID.set(DefaultRangeID);
// console.log(rangeID.get());
},
},
]);
......@@ -184,7 +186,7 @@ class RangePKStudy extends Component {
},
}}
/>
<RangePKState range={parseRangeID(this.state.rangeID)} />
<RangePKState range={parseRangeID()} />
<MyStatistics
containerStyle={styles.containerOnRight}
insideStyle={styles.containerInRightColumn}
......@@ -192,7 +194,7 @@ class RangePKStudy extends Component {
onPress={this.openRankingList}
text={{
first: {
top: parseRangeID(this.state.rangeID) + '区域',
top: parseRangeID() + '区域',
bottom: 'Top 3',
},
second: {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册