Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxiang committed May 7, 2019
1 parent 2ba036d commit b9ef7b5
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {
} from "react-native";
import {
init,
addLocationListener,
start,
stop,
Geolocation,
setInterval,
setNeedAddress,
setLocatingWithReGeocode
Expand Down Expand Up @@ -51,7 +49,6 @@ class App extends React.Component {
ios: "9bd6c82e77583020a73ef1af59d0c759",
android: "043b24fe18785f33c491705ffe5b6935"
});
addLocationListener(location => this.updateLocationState(location));
}

componentWillUnmount() {
Expand All @@ -66,9 +63,27 @@ class App extends React.Component {
}
}

startLocation = () => start();
stopLocation = () => {
stop();
getCurrentPosition = () => {
Geolocation.getCurrentPosition(
position => this.updateLocationState(position),
error => this.updateLocationState(error)
);
};

watchPosition = () => {
if (!this.watchId) {
this.watchId = Geolocation.watchPosition(
position => this.updateLocationState(position),
error => this.updateLocationState(error)
);
}
};

clearWatch = () => {
if (this.watchId) {
Geolocation.clearWatch(this.watchId);
this.watchId = null;
}
this.setState({ location: null });
};

Expand All @@ -82,13 +97,16 @@ class App extends React.Component {
render() {
const { location } = this.state;
return (
<ScrollView style={style.body}>
<ScrollView contentContainerStyle={style.body}>
<View style={style.controls}>
<View style={style.button}>
<Button onPress={this.startLocation} title="start" />
<Button onPress={this.getCurrentPosition} title="Geolocation.getCurrentPosition" />
</View>
<View style={style.button}>
<Button onPress={this.stopLocation} title="stop" />
<Button onPress={this.watchPosition} title="Geolocation.watchPosition" />
</View>
<View style={style.button}>
<Button onPress={this.clearWatch} title="Geolocation.clearWatch" />
</View>
<View style={style.button}>
<Button onPress={this.setInterval2000} title="setInterval(2000)" />
Expand All @@ -115,7 +133,9 @@ class App extends React.Component {
/>
</View>
</View>
<Text style={style.result}>{JSON.stringify(location, null, 2)}</Text>
<Text style={style.result}>{`${JSON.stringify(location, null, 2)}
`}</Text>
</ScrollView>
);
}
Expand Down

0 comments on commit b9ef7b5

Please sign in to comment.