How do i get a list of nearby users in an react na

2020-04-16 06:52发布

问题:

I'm working on an app based on Maps, with react native and Firebase as a backend, And I'm Stuck with Nearby users Feature, I'm using react native maps for Geolocation, and react native firebase.

Now In my app, I have two types of users ( End User, Provider ) and when registered I'm saved Data about them contain their Location ( Lat, Long ), at the moment I'm working with End User App, and I save some faked Data about providers ( Location ), and I don't have an idea about how to get all of the provider nearby of my location,

Here's my DB [Providers]

Here's My Code To Get the current location to users, and I've tried some Queries to get the nearby providers but it's Fall :(, I can easily get all provider but bot the nearby so ..

import React, { Component } from 'react';
import MapView, { Marker } from 'react-native-maps';
import { View, Text, StyleSheet, Dimensions, PermissionsAndroid } from 'react-native';

let { width, height } = Dimensions.get('window');

const LATITUDE = 50.78825;
const LONGITUDE = 51.4324;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = 0.0421;

class Map extends Component {
    constructor(props) {
        super(props);
        this.state = {
            error: null,
            width: width,
            marginBottom: 1,
            region: {
                latitude: LATITUDE,
                longitude: LONGITUDE,
                latitudeDelta: LATITUDE_DELTA,
                longitudeDelta: LONGITUDE_DELTA,
            }
        };

    }
    requestLocationPermission = async () => {
        const LocationPermission = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
            // , {
            //     'message': 'This App needs to Access your location, OK?'
            // }
        )
        if (LocationPermission === PermissionsAndroid.RESULTS.GRANTED) {
            navigator.geolocation.getCurrentPosition(
                //Will give you the current location
                position => {
                    const longitude = position.coords.longitude;
                    const latitude = position.coords.latitude;

                    this.setState({
                        region: {
                            latitude,
                            longitude,
                            latitudeDelta: LATITUDE_DELTA,
                            longitudeDelta: LONGITUDE_DELTA,
                        }
                    })
                },
                error => alert(error.message),
                { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
            );
            this.watchID = navigator.geolocation.watchPosition(position => {
                console.log(position);
                const longitude = position.coords.longitude;
                const latitude = position.coords.latitude;

                this.setState({
                    region: {
                        latitude,
                        longitude,
                        latitudeDelta: LATITUDE_DELTA,
                        longitudeDelta: LONGITUDE_DELTA,
                    }
                });
            });

        }
    }
    async componentDidMount() {
        await this.requestLocationPermission()
    };
    render() {
        const { region } = this.state;
        return (
            <View style={styles.container}>
                <MapView
                    style={[styles.map, { width: this.state.width }]}
                    style={StyleSheet.absoluteFill}
                    onMapReady={() => console.log(this.state.region)}
                    showsUserLocation
                    region={region}
                    loadingEnabled={true}
                    // style={StyleSheet.absoluteFill}
                    textStyle={{ color: '#bc8b00' }}
                    containerStyle={{ backgroundColor: 'white', borderColor: '#BC8B00' }}
                >
                    <Marker
                        coordinate={this.state.region}
                        title="Hello Title"
                        description="description..."
                    />
                </MapView>
                {/* <Text>{this.state.region.latitude}</Text> */}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        flexDirection: 'row',
        justifyContent: 'space-between',
        padding: 30,
        flex: 1,
        alignItems: 'center'
    },
    map: {
        position: 'absolute',
        zIndex: -1,
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
    },
});

export default Map;

If anyone interested to help me here is my Repo