关于从地理位置发送到反应原生火力的纬度和纵向问题



问候大家。 我在向反应原生的火堆发送坐标时遇到了问题。 我可以向 firestore 发送日期时间,但坐标没有从地理位置发送到 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: position.coords.longitude in firestore 仍然保持 lat 和 long 在 0。 错误说

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

刚刚通过使地理定位中的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,
},
);

最新更新