Last active
July 31, 2018 09:02
-
-
Save allpwrfulroot/5b95057e97fa09c0f6afd0964e724d29 to your computer and use it in GitHub Desktop.
Funky error, async issue or geolocation bug? watchPositionAsync callback EXPLODES (counter goes to hundreds) if getCurrentPosition called?
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 { | |
ActivityIndicator, | |
View, | |
Text, | |
Alert | |
} from 'react-native' | |
import { Permissions, Location, Notifications, Constants } from 'expo' | |
import { graphql, gql, compose } from 'react-apollo' | |
import update from 'immutability-helper' | |
import moment from 'moment' | |
import Rules from './thingtypes' | |
import config from '../config' | |
const deviceId = Constants.deviceId | |
class Geolocation extends Component { | |
state = { | |
isLoading: true, | |
location: null, | |
approx: null, | |
processing: 'Nothing yet' | |
} | |
watchId = (null: ?number) | |
componentWillMount() { | |
let counter = 0 | |
Permissions.askAsync(Permissions.LOCATION) | |
.then( response => { | |
if(response.status === 'granted') { | |
console.log('Starting watchPositionAsync') | |
this.watchId = Location.watchPositionAsync({ | |
enableHighAccuracy: false, | |
distanceInterval: 2000, | |
timeInterval: 200000 | |
}, newLoc => { | |
if(newLoc.timestamp && !!this.props.currentRecords) { | |
console.log('newLoc ', counter) | |
this.updateLoc(newLoc) | |
counter++ | |
} else { | |
console.log('ignored newLoc') | |
} | |
}) | |
} else { | |
console.log('Error in getLocationAsync: Permission to access location was denied') | |
} | |
}) | |
.catch( error => console.log('Error in componentWillMount: ', error.message)) | |
.done() | |
} | |
componentWillUnmount() { | |
Alert.alert('Component unmounting!') | |
this.watchId.remove() | |
} | |
getGpsLocation = () => { | |
navigator.geolocation.getCurrentPosition( | |
(position) => { | |
console.log('gps location: ', position) | |
this.updateGps(position) | |
}, | |
(err) => { | |
console.log('Error in getCurrentPosition: ', err) | |
}, | |
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 } | |
) | |
} | |
updateGps = async(gps) => { | |
try { | |
let url = `https://maps.google.com/maps/api/geocode/json?key=${config.googleAPIkey}&result_type=country|administrative_area_level_1|administrative_area_level_2&latlng=${gps.coords.latitude},${gps.coords.longitude}` | |
let result = await fetch(url) | |
let precise = JSON.parse(result._bodyInit).results[0].formatted_address.split(',').map(item => item.trim()).reverse().concat(['precise']) | |
this.setState({location: gps, approx: precise}) | |
// Only proceed if the location accuracy is reasonable: | |
if(gps.coords.accuracy < 200) { | |
// do a thing | |
} else { | |
// do another thing | |
} | |
return null | |
} catch (error) { | |
console.log('Error in updateGps: ', error.message) | |
} | |
} | |
updateLoc = async (location) => { | |
let url = `https://maps.google.com/maps/api/geocode/json?key=${config.googleAPIkey}&result_type=country|administrative_area_level_1|administrative_area_level_2&latlng=${location.coords.latitude},${location.coords.longitude}` | |
try { | |
let result = await fetch(url) | |
let approx = JSON.parse(result._bodyInit).results[0].formatted_address.split(',').map(item => item.trim()).reverse().concat(['approx']) | |
this.setState({location: location, approx: approx, isLoading: false}) | |
// If no recent data or location has changed, go to GPS: | |
if (this.props.currentRecords.edges.length === 0 || this.props.currentRecords.edges[0].node.trip.jurisdiction !== approx[2]) { | |
this.getGpsLocation() | |
} else if ( otherstuff ) { | |
this.newThing(location, approx, this.props.currentRecords.edges[0].node.id) | |
} else { | |
this.determineThing(approx[1]) | |
.then( thing => { | |
console.log('new Date ', moment(location.timestamp).format('MMM d'), ' low Accuracy determineThing: ', thing) | |
this.newRecord(location, approx, this.props.currentRecords.edges[0].node.trip.id, thing) | |
}) | |
.catch( err => console.log('Error in determineTaxed: ', err)) | |
} | |
return null | |
} catch (error) { | |
console.log('Error in updateLoc: ', error.message) | |
} | |
} | |
render() { | |
if(this.state.isLoading || !!this.state.location) { | |
return ( | |
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}> | |
<ActivityIndicator | |
animating={true} | |
size="large" | |
/> | |
</View> | |
) | |
} else { | |
// console.log(this.props) | |
return ( | |
<View style={{flex: 1}}> | |
<Text>Location: {this.state.location.coords.latitude}, {this.state.location.coords.longitude}</Text> | |
<Text>Timestamp: {moment(this.state.location.timestamp).format()}</Text> | |
<Text>Google Maps API result: {this.state.approx}</Text> | |
</View> | |
) | |
} | |
} | |
} | |
export default compose( | |
graphql(GetRecordsQuery, { | |
grahpql stuff | |
)(Geolocation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment