How can I make a filter based in the distance with React-native

Viewed 334

I need to create a distance filter for my app, like just getting users from the user city, like in tinder distance radius, how can I do that ? here is the page code I have for now

getUsers = async() => {
    try{
        const response = await api.get('/auth/list')

        const { users } = response.data
        console.log({users})

        this.setState({ users })
    }catch(response){
        this.setState({ errorMessage: response.data.error })
        console.log(response.data.error)
    } 
  }


 async componentDidMount(){
  this.getUsers()
 }

 render() {
   return(
     <View>
       {!!this.state.errorMessage && <Text>{ this.state.errorMessage }</Text>}
       {this.state.users.filter(user => user.speciality === 'Dermatologista').map(user => (
          <View key={user._id} style={{marginTop: 15, alignItems: 'center'}}>
            <Text style={{fontWeight: 'bold', fontSize:20}}>{user.title}</Text>
            <Text>{user.speciality}</Text>
            <Button   title = 'View Profile'onPress ={() => this.props.navigation.navigate('Profile', {
               name: user._id
               
            })}/>
            </View>
        ))}
     </View>
   )
 }
}

how can I make a distance filter too ?, I already have the currentposititon and the other users positions

1 Answers

the answer is simple and you just need to create a regions array and do the following

this will get the user location

 navigator.geolocation.getCurrentPosition(
        ({coords: {latitude, longitude}}) => {
          this.setState({region: {latitude, longitude,latitudeDelta:0.0, 
 longitudeDelta:0.005,}})
        }, //sucesso
        () => {}, //erro
        {
          timeout: 2000,
          enableHighAccuracy: true,
          maximumAge: 1000,
        }
      )
    }
    
    render(){
      const users = this.state.users
      console.log(users)

and here is the filter

      return(
        <View style={styles.container}>
          <ScrollView>
          {!!this.state.errorMessage && <text>{this.state.errorMessage}</text>}
          {this.state.users.filter(user => user.speciality === 'Hospital' && 
           user.latitude > this.state.region.latitude - .2 &&
           user.latitude < this.state.region.latitude + .2 && user.longitude > 
            this.state.region.longitude -.2 &&
           user.longitude < this.state.region.longitude + .2
           ).map(user => (
              <View key={user._id} style={{marginTop: 15, alignItems: 
               'center'}}>
                <Text style={{fontWeight: 'bold', fontSize:20}}>{user.title} 
                </Text>
                <Text>{user.speciality}</Text>
                <Button   title = 'View Profile'onPress ={() => 
                this.props.navigation.navigate('Profile', {
                   name: user._id
                   
                })}/>
                </View>
            ))}
          </ScrollView>
         
    
        </View>
      )
    }
      
    
    }
Related