【问题标题】:Problem about send latitude and longtitude from geolocation to react-native-firebase on react native关于在 react native 上将经纬度从地理位置发送到 react-native-firebase 的问题
【发布时间】:2020-05-16 07:14:08
【问题描述】:

大家好。 我在 react-native 中将坐标发送到 firestore 时遇到问题。 我可以将日期时间发送到 firestore,但坐标没有从 Geolocation 发送到 firebase 添加集合功能。 代码在这里。

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>
    );
  }
}


我尝试使用 lat:position.coords.latitude, long: 在 Firestore 中的 position.coords.longitude 仍然保持 lat 和 long 为 0。 错误说

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

【问题讨论】:

    标签: android react-native geolocation react-native-firebase


    【解决方案1】:

    刚刚从朋友那里得到了一个指南,通过在地理位置中制作 geoDat 之类的变量来保持纬度并发送到 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,
          },
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 2021-01-23
      • 2019-05-03
      相关资源
      最近更新 更多