Last active
May 20, 2017 15:15
-
-
Save jamesholcomb/fca048a35b2102debe57e5cc28904df2 to your computer and use it in GitHub Desktop.
Issue with getCurrentPosition location request timed out on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { StyleSheet, Alert, Text, View, PermissionsAndroid } from 'react-native' | |
function currentPosition() { | |
return new Promise((resolve, reject) => | |
navigator.geolocation.getCurrentPosition( | |
({ coords }) => resolve(coords), | |
(err) => reject(err), { | |
enableHighAccuracy: false, | |
timeout: 1000, | |
maximumAge: 1000 | |
} | |
) | |
) | |
} | |
/** | |
* Issue with getCurrentPosition location request timed out on Android | |
* https://github.com/facebook/react-native/issues/7495 | |
* Open extended controls in emulator, go to Location, hit Send | |
* getCurrentPosition() -> | |
* must specify enableHighAccuracy: true / false | |
* corresponding to Location settings in Android | |
* 'High Accuracy' 'Battery Saving' 'Device Only' | |
* and the PermissionsAndroid.PERMISSIONS request() | |
*/ | |
export default class AndroidTest extends Component { | |
state = { | |
coords: { | |
latitude: 0, | |
longitude: 0 | |
} | |
} | |
async componentDidMount() { | |
try { | |
await PermissionsAndroid | |
.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION) | |
const coords = await currentPosition() | |
console.log(coords) | |
this.setState({ coords }) | |
} | |
catch (e) { | |
Alert.alert('', JSON.stringify(e, undefined, 2)) | |
} | |
} | |
render() { | |
return ( | |
<View style={styles.container}> | |
<Text> | |
[{this.state.coords.longitude}, {this.state.coords.latitude}] | |
</Text> | |
</View> | |
) | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: 'center', | |
alignItems: 'center', | |
backgroundColor: '#F5FCFF', | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment