Как получить точную широту и долготу текущего местоположения в React Native - PullRequest
0 голосов
/ 29 мая 2020

Я хочу получить latitude, longitude моего текущего местоположения. Для этого я установил response-native-geolocation и реализовал его в соответствии с его документом. Но он дает неправильные latitude, longitude. То же самое касается response-native-geolocation-service .

Вот код, который я пробовал

import React from 'react';
import { StyleSheet, Text, View, Alert } from 'react-native';
import Geolocation from '@react-native-community/geolocation';

export default class GeolocationExample extends React.Component {
  state = {
    latitude: 'unknown',
    longitude: 'unknown',
  };

  componentDidMount() {

   Geolocation.getCurrentPosition(
      position => {
        const latitude = JSON.stringify(position.coords.latitude);
        const longitude = JSON.stringify(position.coords.longitude);
        this.setState({ latitude, longitude });
      },
      error => Alert.alert('Error', JSON.stringify(error)),
      { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
    );
  }

  render() {
    const { latitude, longitude } = this.state;
    console.log(latitude, longitude);

    return (
      <View>
        <Text>
          <Text style={styles.title}>latitude: </Text>
          {this.state.latitude}
        </Text>
        <Text>
          <Text style={styles.title}>longitude: </Text>
          {this.state.longitude}
        </Text>
      </View>
    );
  }
}

...