Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
1NH21CE037_Nagaraja_ N
Created September 3, 2024

Nature Access: An Audio Guide for Visually Impaired Outdoor

Empower visually impaired individuals to explore nature trails through an innovative app providing real-time audio descriptions of flora.

19
Nature Access: An Audio Guide for Visually Impaired Outdoor

Story

Read more

Code

Nature Access: An Audio Guide for Visually Impaired Outdoor Exploration

C/C++
import React, { useState, useEffect } from 'react';
import { Text, View, Button } from 'react-native';
import * as Location from 'expo-location';
import * as Speech from 'expo-speech';

const App = () => {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }

      let location = await Location.getCurrentPositionAsync({});
      setLocation(location);
    })();
  }, []);

  const checkProximity = () => {
    const userLat = location.coords.latitude;
    const userLon = location.coords.longitude;

    // Example point of interest
    const poiLat = 40.689247;
    const poiLon = -74.044502;

    const distance = getDistance(userLat, userLon, poiLat, poiLon);

    if (distance < 50) { // within 50 meters of POI
      Speech.speak('You are near the Statue of Liberty, a symbol of freedom and democracy.');
    }
  };

  const getDistance = (lat1, lon1, lat2, lon2) => {
    const R = 6371e3; // metres
    const φ1 = lat1 * Math.PI/180;
    const φ2 = lat2 * Math.PI/180;
    const Δφ = (lat2-lat1) * Math.PI/180;
    const Δλ = (lon2-lon1) * Math.PI/180;

    const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
              Math.cos(φ1) * Math.cos(φ2) *
              Math.sin(Δλ/2) * Math.sin(Δλ/2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    const distance = R * c; // in metres
    return distance;
  };

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Your current location: {JSON.stringify(location)}</Text>
      <Button title="Check Proximity" onPress={checkProximity} />
    </View>
  );
};

export default App;

Credits

1NH21CE037_Nagaraja_ N

1NH21CE037_Nagaraja_ N

1 project • 1 follower

Comments