Revert to user location from map with better GPS accuracy

This commit is contained in:
John Lyon-Smith
2018-06-28 08:32:00 -07:00
parent d9fe36cdde
commit ea15132d59
6 changed files with 34 additions and 54 deletions

View File

@@ -1,10 +1,7 @@
import React from "react" import React from "react"
import { import {
StyleSheet,
Text, Text,
TextInput,
SectionList, SectionList,
Image,
View, View,
TouchableOpacity, TouchableOpacity,
TouchableHighlight, TouchableHighlight,
@@ -31,12 +28,8 @@ import KeyboardSpacer from "react-native-keyboard-spacer"
import hardhatPinImage from "./images/hardhat-pin.png" import hardhatPinImage from "./images/hardhat-pin.png"
import clipboardPinImage from "./images/clipboard-pin.png" import clipboardPinImage from "./images/clipboard-pin.png"
import questionPinImage from "./images/question-pin.png" import questionPinImage from "./images/question-pin.png"
import locationImage from "./images/location.png"
import moment from "moment" import moment from "moment"
const neverAskForLocationPermissionKeyName = "NeverAskForLocationPermission"
const neverAskForCameraKeyName = "NeverAskForCameraPermission"
export class Home extends React.Component { export class Home extends React.Component {
constructor(props) { constructor(props) {
super(props) super(props)
@@ -48,8 +41,8 @@ export class Home extends React.Component {
showWorkItems: true, showWorkItems: true,
haveCameraPermission: false, haveCameraPermission: false,
haveLocationPermission: false, haveLocationPermission: false,
currentPosition: null,
} }
this.currentPosition = null
this.isMapReady = false this.isMapReady = false
this.goToRegionWhenReady = false this.goToRegionWhenReady = false
this.region = null this.region = null
@@ -97,6 +90,7 @@ export class Home extends React.Component {
} }
) )
} else { } else {
navigator.geolocation.requestAuthorization()
this.state.haveLocationPermission = true this.state.haveLocationPermission = true
this.state.haveCameraPermission = true this.state.haveCameraPermission = true
} }
@@ -176,8 +170,8 @@ export class Home extends React.Component {
} }
handleWorkItemsListPress() { handleWorkItemsListPress() {
if (this.state.currentPosition) { if (this.currentPosition) {
const { coords } = this.state.currentPosition const { coords } = this.currentPosition
this.props.history.push( this.props.history.push(
`/workItemList?latLng=${coords.latitude},${coords.longitude}` `/workItemList?latLng=${coords.latitude},${coords.longitude}`
@@ -210,13 +204,13 @@ export class Home extends React.Component {
} }
handleGlassesPress() { handleGlassesPress() {
const { sections: workItems, currentPosition } = this.state const { sections: workItems } = this.state
if (currentPosition) { if (this.currentPosition) {
const { const {
latitude: latitude1, latitude: latitude1,
longitude: longitude1, longitude: longitude1,
} = currentPosition.coords } = this.currentPosition.coords
let closestWorkItem = config.alwaysShowWorkItemInAR ? workItems[0] : null let closestWorkItem = config.alwaysShowWorkItemInAR ? workItems[0] : null
let shortestDistance = config.minDistanceToItem let shortestDistance = config.minDistanceToItem
@@ -248,10 +242,8 @@ export class Home extends React.Component {
} }
handleMyLocationPress() { handleMyLocationPress() {
const { currentPosition } = this.state if (this.currentPosition && this.isMapReady) {
const { coords } = this.currentPosition
if (currentPosition && this.isMapReady) {
const { coords } = currentPosition
this.mapView.animateToCoordinate({ this.mapView.animateToCoordinate({
latitude: coords.latitude, latitude: coords.latitude,
@@ -281,14 +273,12 @@ export class Home extends React.Component {
} }
handlePositionUpdate(position) { handlePositionUpdate(position) {
this.setState({ currentPosition: position }) this.currentPosition = position
} }
getCoordinateDistance(coordinate) { getCoordinateDistance(coordinate) {
const { currentPosition } = this.state if (this.currentPosition) {
const { coords } = this.currentPosition
if (currentPosition) {
const { coords } = currentPosition
const { latitude, longitude } = coordinate const { latitude, longitude } = coordinate
return ( return (
@@ -313,7 +303,6 @@ export class Home extends React.Component {
waitModal, waitModal,
haveCameraPermission, haveCameraPermission,
haveLocationPermission, haveLocationPermission,
currentPosition,
} = this.state } = this.state
return ( return (
@@ -340,7 +329,7 @@ export class Home extends React.Component {
showWorkItems && { height: "40%" }, showWorkItems && { height: "40%" },
!showWorkItems && { flexGrow: 1 }, !showWorkItems && { flexGrow: 1 },
]} ]}
showsUserLocation={false} showsUserLocation
showsBuildings={false} showsBuildings={false}
showsTraffic={false} showsTraffic={false}
showsIndoors={false} showsIndoors={false}
@@ -376,14 +365,6 @@ export class Home extends React.Component {
</Callout> </Callout>
</Marker> </Marker>
))} ))}
{currentPosition && (
<Marker
key={sections.length}
coordinate={currentPosition.coords}
anchor={{ x: 0.5, y: 0.5 }}
image={locationImage}
/>
)}
</MapView> </MapView>
<View <View
style={{ style={{

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

View File

@@ -1,6 +1,6 @@
export const config = { export const config = {
localIPAddr: "192.168.1.175", // localIPAddr: "192.168.1.175",
//localIPAddr: "192.168.1.14", 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",
@@ -12,8 +12,8 @@ export const config = {
homeRegionDelta: 0.005, homeRegionDelta: 0.005,
geocodeDelayMilliseconds: 500, geocodeDelayMilliseconds: 500,
//defaultUser: "john@lyon-smith.org", defaultUser: "john@lyon-smith.org",
defaultUser: "", //defaultUser: "",
// This region is downtown Toronto // This region is downtown Toronto
initialRegion: { initialRegion: {

View File

@@ -15,36 +15,35 @@ export class Geolocation extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
reactAutoBind(this) this.watchId = -1
if (props.watch) {
this.intervalId = setInterval(this.updateLocation, config.geoSampleDelay)
} else {
this.intervalId = -1
}
} }
componentDidMount() { componentDidMount() {
this.updateLocation()
}
componentWillUnmount() {
if (this.intervalId !== -1) {
clearInterval(this.intervalId)
this.intervalId = -1
}
}
updateLocation() {
navigator.geolocation.getCurrentPosition( navigator.geolocation.getCurrentPosition(
(position) => { (position) => {
this.props.onUpdate(position) this.props.onUpdate(position)
if (this.props.watch) {
;(this.watchId = navigator.geolocation.watchPosition((position) =>
this.props.onUpdate(position)
)),
null,
{ distanceFilter: 5, maximumAge: 0, enableHighAccuracy: true }
}
}, },
null, null,
{ distanceFilter: 5, maximumAge: 0, enableHighAccuracy: true } { distanceFilter: 5, maximumAge: 0, enableHighAccuracy: true }
) )
} }
componentWillUnmount() {
if (this.watchId !== -1) {
navigator.geolocation.clearWatch(this.watchId)
navigator.geolocation.stopObserving()
this.watchId = -1
}
}
render() { render() {
return null return null
} }