Quantcast
Channel: calculating distance in React Native - Stack Overflow
Viewing all articles
Browse latest Browse all 3

calculating distance in React Native

$
0
0

I want to get the distance from my current location to destination. I do have the Longitude and latitude of the destination in my JSON file. How can I get the longitude and latitude of my current location so that I can calculate the distance between two points? I tried the code below in my android emulator, but I keep getting the time out issues.

    import React, { Component } from 'react';
import { View, Text } from 'react-native';

class GeolocationExample extends Component {
  constructor(props) {
    super(props);

    this.state = {
      latitude: null,
      longitude: null,
      error: null,
    };
  }

  componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        });
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 },
    );
  }

  render() {
    return (
      <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Latitude: {this.state.latitude}</Text>
        <Text>Longitude: {this.state.longitude}</Text>
        {this.state.error ? <Text>Error: {this.state.error}</Text> : null}
      </View>
    );
  }
}

export default GeolocationExample;

I just want to get the distance from the current location to destination in miles. How can I achieve that? Below is the screen along with the error enter image description herethat I am getting :


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>