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

View File

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