Search code examples
androidreact-nativegeolocationreact-native-firebase

Problem about send latitude and longtitude from geolocation to react-native-firebase on react native


Greeting everyone. I had problem about send a coordinate to firestore in react-native. I can send a datetime to firestore but the coordinate didn't send from Geolocation to firebase add collection function. Here the code.

import React from 'react';
import {View, Text, PermissionsAndroid, Alert, Platform} from 'react-native';
import Geolocation from 'react-native-geolocation-service';
import MapView, {PROVIDER_GOOGLE, Marker, Polyline} from 'react-native-maps';
import {mapStyle} from '../../constants/mapStyle';
import firebase from 'react-native-firebase';
import {geocollection } from 'geofirestore';
export default class Map extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      latitude: 0,
      longitude: 0,
      coordinates: [],
    };
  }
async componentDidMount() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          'title': 'Tracking App',
          'message': 'Tracking App'
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("You can use the location")
        alert("You can use the location");
      } else {
        console.log("location permission denied")
        alert("location permission denied");
      }
    } catch (err) {
      console.warn(err)
   Geolocation.getCurrentPosition(
      position => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          coordinates: this.state.coordinates.concat({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
          }),
        });
      },
      error => {
        Alert.alert(error.message.toString());
      },
      {
        showLocationDialog: true,
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 0,
      },
    );
    Geolocation.watchPosition(
      position => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          coordinates: this.state.coordinates.concat({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
          }),

        });
      },
      error => {
        console.log(error);
      },
      {
        showLocationDialog: true,
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 0,
        distanceFilter: 0,
      },
    );

    firebase.firestore()
    .collection('Tracking')
    .add({
    lat:this.state.latitude,
    long: this.state.longitude,
    date:firebase.firestore.FieldValue.serverTimestamp()
    })
 }
  render() {
    return (
      <View style={{flex: 1}}>
        <MapView
          provider={PROVIDER_GOOGLE}
          customMapStyle={mapStyle}
          style={{flex: 1}}
          region={{
            latitude: this.state.latitude,
            longitude: this.state.longitude,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}>
          <Marker
            coordinate={{
              latitude: this.state.latitude,
              longitude: this.state.longitude,
            }}></Marker>
          <Polyline
            coordinates={this.state.coordinates}
            strokeColor="#bf8221"
            strokeColors={[
              '#bf8221',
              '#ffe066',
              '#ffe066',
              '#ffe066',
              '#ffe066',
            ]}
            strokeWidth={3}
          />
        </MapView>
      </View>
    );
  }
}


I tried to use with lat:position.coords.latitude, long: position.coords.longitude in firestore still keep lat and long in 0. The error said

Possible Unhandled Promise Rejection (id: 0):
TypeError: undefined is not a function (near '...}).the(function (data) {...')

Solution

  • Just got a guide from friend by make variable like geoDat in geolocation to keep lat long and send to firestore.

      Geolocation.getCurrentPosition(
          position => {
            this.setState({
              latitude: position.coords.latitude,
              longitude: position.coords.longitude,
              coordinates: this.state.coordinates.concat({
                latitude: position.coords.latitude,
                longitude: position.coords.longitude,
              }),
            });
            geoDat.lat = position.coords.latitude
            geoDat.lng = position.coords.longitude
          },
          error => {
            Alert.alert(error.message.toString());
          },
          {
            showLocationDialog: true,
            enableHighAccuracy: true,
            timeout: 20000,
            maximumAge: 0,
          },
        );
        Geolocation.watchPosition(
          position => {
            this.setState({
              latitude: position.coords.latitude,
              longitude: position.coords.longitude,
              coordinates: this.state.coordinates.concat({
                latitude: position.coords.latitude,
                longitude: position.coords.longitude,
              }),
    
            });
            geoDat.lat = position.coords.latitude
            geoDat.lng = position.coords.longitude
            console.log(geoDat);
            firebase.firestore()
              .collection('Tracking')
              .add({
                lat: geoDat.lat,
                long:geoDat.lng,
                date: firebase.firestore.FieldValue.serverTimestamp()
              })
          },
          error => {
            console.log(error);
          },
          {
            showLocationDialog: true,
            enableHighAccuracy: true,
            timeout: 20000,
            maximumAge: 0,
            distanceFilter: 0,
          },
        );