2015-03-01 04:46:42 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2015-03-23 07:46:11 +00:00
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule GeolocationExample
|
2015-03-01 04:46:42 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2015-03-01 04:46:42 +00:00
|
|
|
var {
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
View,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2015-03-01 04:46:42 +00:00
|
|
|
|
|
|
|
exports.framework = 'React';
|
2015-03-09 10:04:44 +00:00
|
|
|
exports.title = 'Geolocation';
|
|
|
|
exports.description = 'Examples of using the Geolocation API.';
|
2015-03-01 04:46:42 +00:00
|
|
|
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'navigator.geolocation',
|
2016-10-16 11:11:59 +00:00
|
|
|
render: function(): React.Element<any> {
|
2015-03-09 10:04:44 +00:00
|
|
|
return <GeolocationExample />;
|
2015-03-01 04:46:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class GeolocationExample extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
initialPosition: 'unknown',
|
|
|
|
lastPosition: 'unknown',
|
|
|
|
};
|
2015-03-23 07:46:11 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
watchID: ?number = null;
|
2015-03-01 04:46:42 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
componentDidMount() {
|
2015-03-01 04:46:42 +00:00
|
|
|
navigator.geolocation.getCurrentPosition(
|
2015-12-02 03:09:01 +00:00
|
|
|
(position) => {
|
|
|
|
var initialPosition = JSON.stringify(position);
|
|
|
|
this.setState({initialPosition});
|
|
|
|
},
|
2016-09-05 19:52:17 +00:00
|
|
|
(error) => alert(JSON.stringify(error)),
|
2015-06-10 00:04:55 +00:00
|
|
|
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
|
2015-03-01 04:46:42 +00:00
|
|
|
);
|
2015-12-02 03:09:01 +00:00
|
|
|
this.watchID = navigator.geolocation.watchPosition((position) => {
|
|
|
|
var lastPosition = JSON.stringify(position);
|
2015-03-01 04:46:42 +00:00
|
|
|
this.setState({lastPosition});
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-01 04:46:42 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
componentWillUnmount() {
|
2017-04-28 00:04:53 +00:00
|
|
|
this.watchID != null && navigator.geolocation.clearWatch(this.watchID);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-01 04:46:42 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2015-03-01 04:46:42 +00:00
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>
|
|
|
|
<Text style={styles.title}>Initial position: </Text>
|
2015-12-02 03:09:01 +00:00
|
|
|
{this.state.initialPosition}
|
2015-03-01 04:46:42 +00:00
|
|
|
</Text>
|
|
|
|
<Text>
|
|
|
|
<Text style={styles.title}>Current position: </Text>
|
2015-12-02 03:09:01 +00:00
|
|
|
{this.state.lastPosition}
|
2015-03-01 04:46:42 +00:00
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-01 04:46:42 +00:00
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
title: {
|
2015-03-25 23:22:59 +00:00
|
|
|
fontWeight: '500',
|
2015-03-01 04:46:42 +00:00
|
|
|
},
|
|
|
|
});
|