Skip to content

Instantly share code, notes, and snippets.

@shafayeatsumit
Created December 18, 2017 10:55
Show Gist options
  • Save shafayeatsumit/27b32875daa1a2b34bf3eeca4be136c2 to your computer and use it in GitHub Desktop.
Save shafayeatsumit/27b32875daa1a2b34bf3eeca4be136c2 to your computer and use it in GitHub Desktop.
Text speech for nearest locatoin
/**
* this code snippet will fetch user location data in every 300 meter
* fetch nearest attractions from aws lambda
* speak - location name and addresss (most nearest one)
*/
import Expo from 'expo';
import React, { Component } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import { Constants, Location, Permissions, Speech } from 'expo';
const dataUrl = "https://XXXXXXXXXXXXXXXXXXXXXXXX.amazonaws.com/dev/get-landmarks?"; //aws lambda api
// distanceInterval: 300 which will give users location in every 300 meters
const GEOLOCATION_OPTIONS = { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceInterval:300 };
console.disableYellowBox = true; // disable warning
export default class App extends Component {
constructor(props){
super(props);
this.state = {
location: { coords: {latitude: 0, longitude: 0}}
}
this.location = null;
}
componentWillMount() {
// watchPostionAsync will watch users postion and call locationChanged callback when new data comes
Location.watchPositionAsync(GEOLOCATION_OPTIONS, this.locationChanged);
Speech.speak("Hello Road Tripper! Hope You are having an amazing day.");
}
fetchLandmarks = (position) => {
/**
* fetch the nearest attractions using users current postion
* fetches attraction within 500 meter
*/
let url = dataUrl+"lat="+position.coords.latitude+"&lon="+position.coords.longitude+"&distance="+0.5
fetch(url)
.then((response)=>response.json())
.then((resJson)=>{
resJson = JSON.parse(resJson)
Speech.stop(); //stops speech if it is running already
if (resJson["landmark_data"] && resJson["landmark_data"].length >= 3){
// textToRead holds the nearest 3 attractions address and name string.
let textToRead = resJson["landmark_data"].slice(3).map((item) => item.name + "."+item.address)
/*** this.lastLocation holds the last text from of attraction
* if the last text doesn't match the current one then the speech
* starts again
*/
if (this.lastLocation !== textToRead[0]) {
Speech.speak(textToRead[0])
this.lastLocation = textToRead[0]
}
}else {
Speech.speak("no Data found.")
}
})
}
locationChanged = (location) => {
this.fetchLandmarks(location)
region = {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 0.1, // zoom level
longitudeDelta: 0.05, // zoom level
},
this.setState({location, region})
}
render() {
return (
<Expo.MapView
style={{ flex: 1 }}
showsUserLocation={true}
region={this.state.region}
/>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment