react-native-macos/Examples/UIExplorer/GeolocationExample.js

74 строки
1.5 KiB
JavaScript
Исходник Обычный вид История

/**
* Copyright 2004-present Facebook. All Rights Reserved.
2015-03-23 10:46:11 +03:00
* @flow
*/
/* eslint no-console: 0 */
'use strict';
var React = require('react-native');
var {
StyleSheet,
Text,
View,
} = React;
exports.framework = 'React';
2015-03-09 13:04:44 +03:00
exports.title = 'Geolocation';
exports.description = 'Examples of using the Geolocation API.';
exports.examples = [
{
title: 'navigator.geolocation',
2015-03-23 10:46:11 +03:00
render: function(): ReactElement {
2015-03-09 13:04:44 +03:00
return <GeolocationExample />;
},
}
];
2015-03-09 13:04:44 +03:00
var GeolocationExample = React.createClass({
2015-03-23 10:46:11 +03:00
watchID: (null: ?number),
getInitialState: function() {
return {
initialPosition: 'unknown',
lastPosition: 'unknown',
};
},
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(initialPosition) => this.setState({initialPosition}),
(error) => console.error(error)
);
this.watchID = navigator.geolocation.watchPosition((lastPosition) => {
this.setState({lastPosition});
});
},
componentWillUnmount: function() {
navigator.geolocation.clearWatch(this.watchID);
},
render: function() {
return (
<View>
<Text>
<Text style={styles.title}>Initial position: </Text>
{JSON.stringify(this.state.initialPosition)}
</Text>
<Text>
<Text style={styles.title}>Current position: </Text>
{JSON.stringify(this.state.lastPosition)}
</Text>
</View>
);
}
});
var styles = StyleSheet.create({
title: {
fontWeight: 'bold',
},
});