Quantcast
Channel: React Native calculating the distance between two coordinates using GeoLib - Stack Overflow
Viewing all articles
Browse latest Browse all 3

React Native calculating the distance between two coordinates using GeoLib

$
0
0

I have two issues. One is I can see the Longitude and Latitude of my current location if I start the application using command react native run-android. If I reload the screen, I get a timeout error saying "Location Request timed out" and I don't see Longitude and Latitude. Below is the image

enter image description here

Another issue I am having is when I tried to calculate the distance between two coordinates of Longitude and Latitude then I get path error. Below is my code and the screen shot of the error:

import React, { Component } from 'react';
import { View, Text, TouchableOpacity, Linking } from 'react-native';
import geolib from "geolib";

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: true, timeout: 20000, maximumAge: 1000 },
    );
  }

  render() {

    let geolib = require('geo-lib');


    let result =geolib.Distance({
     p1: { lat: this.state.latitude, lon: this.state.longitude },
     p2: { lat: 33.613355, lon: -117.373261 }
 });


    return (
      <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Latitude: {this.state.latitude}</Text>
        <Text>Longitude: {this.state.longitude}</Text>
        <Text>Distance between two points:result </Text>
        {this.state.error ? <Text>Error: {this.state.error}</Text> : null}


      </View>
    );
  }
}

export default GeolocationExample;

enter image description here

I am not sure why this error is coming up.

Any help will be greatly appreciated.


Viewing all articles
Browse latest Browse all 3

Trending Articles



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