Fix more issues with getting geolocation

This commit is contained in:
John Lyon-Smith
2018-04-29 16:29:49 -07:00
parent 7b20b18aeb
commit edb078b38a
2 changed files with 39 additions and 53 deletions

View File

@@ -47,7 +47,6 @@ export class Home extends React.Component {
workItemDistance: -1, workItemDistance: -1,
} }
this.positionInfo = null
this.watchId = null this.watchId = null
if (Platform.OS !== "ios") { if (Platform.OS !== "ios") {
@@ -69,7 +68,6 @@ export class Home extends React.Component {
PermissionsAndroid.RESULTS.GRANTED PermissionsAndroid.RESULTS.GRANTED
) { ) {
this.setState({ haveLocationPermission: true }) this.setState({ haveLocationPermission: true })
navigator.geolocation.getCurrentPosition(this.handlePositionChange)
} }
if ( if (
@@ -96,7 +94,6 @@ export class Home extends React.Component {
} else { } else {
this.state.haveLocationPermission = true this.state.haveLocationPermission = true
this.state.haveCameraPermission = true this.state.haveCameraPermission = true
navigator.geolocation.getCurrentPosition(this.handlePositionChange)
} }
api api
@@ -145,11 +142,6 @@ export class Home extends React.Component {
this.setState({ messageModal: null }) this.setState({ messageModal: null })
} }
@autobind
handlePositionChange(positionInfo) {
this.positionInfo = positionInfo
}
@autobind @autobind
handleMarkerPress(e, sectionIndex) { handleMarkerPress(e, sectionIndex) {
if (this.sectionList) { if (this.sectionList) {
@@ -160,8 +152,8 @@ export class Home extends React.Component {
}) })
} }
if (this.positionInfo) { navigator.geolocation.getCurrentPosition((positionInfo) => {
const coords = this.positionInfo.coords const coords = positionInfo.coords
const workItem = this.state.sections[sectionIndex] const workItem = this.state.sections[sectionIndex]
const { latitude, longitude } = workItem.coordinate const { latitude, longitude } = workItem.coordinate
@@ -174,20 +166,18 @@ export class Home extends React.Component {
"K" "K"
).toFixed(2), ).toFixed(2),
}) })
} })
} }
@autobind @autobind
handleWorkItemsListPress() { handleWorkItemsListPress() {
if (this.positionInfo) { navigator.geolocation.getCurrentPosition((positionInfo) => {
const { coords } = this.positionInfo const { coords } = positionInfo
this.props.history.push( this.props.history.push(
`/workItemList?latLng=${coords.latitude},${coords.longitude}` `/workItemList?latLng=${coords.latitude},${coords.longitude}`
) )
} else { })
this.props.history.push("/workItemList")
}
} }
@autobind @autobind
@@ -215,15 +205,17 @@ export class Home extends React.Component {
@autobind @autobind
handleGlassesPress() { handleGlassesPress() {
const { sections: workItems } = this.state const { sections: workItems } = this.state
const {
latitude: latitude1, navigator.geolocation.getCurrentPosition((positionInfo) => {
longitude: longitude1, const { latitude: latitude1, longitude: longitude1 } = positionInfo.coords
} = this.positionInfo.coords
let closestWorkItem = config.alwaysShowWorkItemInAR ? workItems[0] : null let closestWorkItem = config.alwaysShowWorkItemInAR ? workItems[0] : null
let shortestDistance = config.minDistanceToItem let shortestDistance = config.minDistanceToItem
workItems.forEach((workItem) => { workItems.forEach((workItem) => {
const { latitude: latitude2, longitude: longitude2 } = workItem.coordinate const {
latitude: latitude2,
longitude: longitude2,
} = workItem.coordinate
const distance = const distance =
geoDistance(latitude1, longitude1, latitude2, longitude2, "K") * 1000 geoDistance(latitude1, longitude1, latitude2, longitude2, "K") * 1000
@@ -243,18 +235,19 @@ export class Home extends React.Component {
: "" : ""
}` }`
) )
})
} }
@autobind @autobind
handleMyLocationPress() { handleMyLocationPress() {
if (this.positionInfo) { navigator.geolocation.getCurrentPosition((positionInfo) => {
const coords = this.positionInfo.coords const coords = positionInfo.coords
this.map.animateToCoordinate({ this.map.animateToCoordinate({
latitude: coords.latitude, latitude: coords.latitude,
longitude: coords.longitude, longitude: coords.longitude,
}) })
} })
} }
@autobind @autobind
@@ -273,14 +266,6 @@ export class Home extends React.Component {
workItemDistance, workItemDistance,
} = this.state } = this.state
if (!this.watchId && haveLocationPermission) {
this.watchId = navigator.geolocation.watchPosition(
this.handlePositionChange,
null,
{ distanceFilter: 10 }
)
}
return ( return (
<View <View
style={{ style={{

View File

@@ -2,7 +2,8 @@ import React from "react"
import { Platform } from "react-native" import { Platform } from "react-native"
export const config = { export const config = {
localIPAddr: "192.168.1.175", //localIPAddr: "192.168.1.175",
localIPAddr: "192.168.1.14",
viroAPIKey: "06F37B6A-74DA-4A83-965A-7DE2209A5C46", viroAPIKey: "06F37B6A-74DA-4A83-965A-7DE2209A5C46",
googleGeocodeAPIKey: "AIzaSyCs4JVT6gysnY5dAJ7KjVJYeykLv_xz1GI", googleGeocodeAPIKey: "AIzaSyCs4JVT6gysnY5dAJ7KjVJYeykLv_xz1GI",
googleGeocodeURL: "https://maps.googleapis.com/maps/api/geocode/json", googleGeocodeURL: "https://maps.googleapis.com/maps/api/geocode/json",