Add in Geolocation component

This commit is contained in:
John Lyon-Smith
2018-05-16 12:11:54 -07:00
parent 07f74fbf7d
commit 23d4a95bd6
5 changed files with 84 additions and 35 deletions

View File

@@ -12,7 +12,7 @@ import {
Platform, Platform,
} from "react-native" } from "react-native"
import MapView, { Marker, Callout } from "react-native-maps" import MapView, { Marker, Callout } from "react-native-maps"
import { Icon, Header } from "../ui" import { Icon, Header, Geolocation } from "../ui"
import { MessageModal } from "../Modal" import { MessageModal } from "../Modal"
import { api } from "../API" import { api } from "../API"
import autobind from "autobind-decorator" import autobind from "autobind-decorator"
@@ -44,7 +44,8 @@ export class Home extends React.Component {
showWorkItems: true, showWorkItems: true,
region: config.initialRegion, region: config.initialRegion,
haveCameraPermission: false, haveCameraPermission: false,
workItemDistance: -1, haveLocationPermission: false,
currentPosition: null,
} }
if (Platform.OS !== "ios") { if (Platform.OS !== "ios") {
@@ -142,33 +143,17 @@ export class Home extends React.Component {
viewOffset: 45, viewOffset: 45,
}) })
} }
navigator.geolocation.getCurrentPosition((positionInfo) => {
const coords = positionInfo.coords
const workItem = this.state.sections[sectionIndex]
const { latitude, longitude } = workItem.coordinate
this.setState({
workItemDistance: geoDistance(
coords.latitude,
coords.longitude,
latitude,
longitude,
"K"
).toFixed(2),
})
})
} }
@autobind @autobind
handleWorkItemsListPress() { handleWorkItemsListPress() {
navigator.geolocation.getCurrentPosition((positionInfo) => { if (this.currentPosition) {
const { coords } = positionInfo const { coords } = this.currentPosition
this.props.history.push( this.props.history.push(
`/workItemList?latLng=${coords.latitude},${coords.longitude}` `/workItemList?latLng=${coords.latitude},${coords.longitude}`
) )
}) }
} }
@autobind @autobind
@@ -197,8 +182,11 @@ export class Home extends React.Component {
handleGlassesPress() { handleGlassesPress() {
const { sections: workItems } = this.state const { sections: workItems } = this.state
navigator.geolocation.getCurrentPosition((positionInfo) => { if (this.currentPosition) {
const { latitude: latitude1, longitude: longitude1 } = positionInfo.coords const {
latitude: latitude1,
longitude: longitude1,
} = 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
@@ -226,19 +214,19 @@ export class Home extends React.Component {
: "" : ""
}` }`
) )
}) }
} }
@autobind @autobind
handleMyLocationPress() { handleMyLocationPress() {
navigator.geolocation.getCurrentPosition((positionInfo) => { if (this.currentPosition) {
const coords = positionInfo.coords const coords = this.currentPosition.coords
this.map.animateToCoordinate({ this.map.animateToCoordinate({
latitude: coords.latitude, latitude: coords.latitude,
longitude: coords.longitude, longitude: coords.longitude,
}) })
}) }
} }
@autobind @autobind
@@ -257,6 +245,31 @@ export class Home extends React.Component {
} }
} }
@autobind
handlePositionUpdate(position) {
this.currentPosition = position
}
@autobind
getCoordinateDistance(coordinate) {
if (this.currentPosition) {
const coords = this.currentPosition.coords
const { latitude, longitude } = coordinate
return (
geoDistance(
coords.latitude,
coords.longitude,
latitude,
longitude,
"K"
).toFixed(2) + " km"
)
} else {
return "?"
}
}
render() { render() {
const { const {
sections, sections,
@@ -265,7 +278,6 @@ export class Home extends React.Component {
messageModal, messageModal,
haveCameraPermission, haveCameraPermission,
haveLocationPermission, haveLocationPermission,
workItemDistance,
} = this.state } = this.state
return ( return (
@@ -319,10 +331,8 @@ export class Home extends React.Component {
": " + ": " +
workItemTypeText[workItem.workItemType] + workItemTypeText[workItem.workItemType] +
" (" + " (" +
(workItemDistance > 0 this.getCoordinateDistance(workItem.coordinate) +
? workItemDistance.toString() ")"}
: "?") +
" km)"}
</Text> </Text>
<Text>{dotify(workItem.address)}</Text> <Text>{dotify(workItem.address)}</Text>
</View> </View>
@@ -466,6 +476,9 @@ export class Home extends React.Component {
/> />
</TouchableOpacity> </TouchableOpacity>
</View> </View>
{haveLocationPermission && (
<Geolocation onUpdate={this.handlePositionUpdate} />
)}
<MessageModal <MessageModal
open={!!messageModal} open={!!messageModal}
icon={messageModal ? messageModal.icon : ""} icon={messageModal ? messageModal.icon : ""}

View File

@@ -344,7 +344,6 @@ export class WorkItem extends React.Component {
showsBuildings={false} showsBuildings={false}
showsTraffic={false} showsTraffic={false}
showsIndoors={false} showsIndoors={false}
cacheEnabled
initialRegion={config.initialRegion} initialRegion={config.initialRegion}
onRegionChange={this.handleRegionChange} onRegionChange={this.handleRegionChange}
onMapReady={this.handleOnMapReady} onMapReady={this.handleOnMapReady}

View File

@@ -2,8 +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", //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",

View File

@@ -0,0 +1,36 @@
import React, { Component } from "react"
import { Image, View } from "react-native"
import PropTypes from "prop-types"
import autobind from "autobind-decorator"
export class Geolocation extends Component {
static propTypes = {
onUpdate: PropTypes.func.isRequired,
}
constructor(props) {
super(props)
this.watchId = -1
}
componentDidMount() {
navigator.geolocation.getCurrentPosition((position) => {
this.props.onUpdate(position)
this.watchId = navigator.geolocation.watchPosition((position) =>
this.props.onUpdate(position)
)
})
}
componentWillUnmount() {
if (this.watchId !== -1) {
navigator.geolocation.clearWatch(this.watchId)
navigator.geolocation.stopObserving()
this.watchId = -1
}
}
render() {
return null
}
}

View File

@@ -3,6 +3,7 @@ export { Header } from "./Header"
export { OptionStrip } from "./OptionStrip" export { OptionStrip } from "./OptionStrip"
export { BubbleLoader } from "./BubbleLoader" export { BubbleLoader } from "./BubbleLoader"
export { ProgressBar } from "./ProgressBar" export { ProgressBar } from "./ProgressBar"
export { Geolocation } from "./Geolocation"
export { BoundSwitch } from "./BoundSwitch" export { BoundSwitch } from "./BoundSwitch"
export { BoundInput } from "./BoundInput" export { BoundInput } from "./BoundInput"
export { FormStaticInput } from "./FormStaticInput" export { FormStaticInput } from "./FormStaticInput"