Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate distance between two lat+lng coordinates in React Native?

I've got an object containing the lat and lng of my companys facilities. I've also got an object containing the lat and lng of the user's current location. What I'd like to do is iterate through the object with the location data of the sites, and calculate the closest one to the user's location. I'd like to get the kilometers between the two as well as the time it would take to drive the distance.

I've been looking at Google's Geometry API but can not figure out how to use this in React Native, if it's possible.

My question is what I should do to best achieve this? Can I somehow use the Google Geometry API?

Edit: I don't have to do this in the app, preferably I'd like to do so, but if it's not possible I could simply make a fetch to my PHP API if there is a better solution for PHP.

like image 309
Johannes Nyman Avatar asked Apr 21 '17 08:04

Johannes Nyman


People also ask

How do you calculate the distance between two latitude longitude and react native?

type UseDistanceTypes = { from: { latitude: number; longitude: number }; to: { latitude: number; longitude: number }; }; const earthRadius = 6378137; const toRadius = (value: number): number => (value * Math. PI) / 180; export const useDistance = ({ from, to }: UseDistanceTypes): number => { const distance = Math.

How do you find the distance between two points in react native?

Precise distance calculation can be done using getPreciseDistance() function which calculates the distance between two geo coordinates. This method is more accurate then getDistance, especially for long distances but it is also slower. It is using the Vincenty inverse formula for ellipsoids.

How do you find the distance between two latitude longitude points?

from math import cos, asin, sqrt, pi def distance(lat1, lon1, lat2, lon2): p = pi/180 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2 return 12742 * asin(sqrt(a)) #2*R*asin... And for the sake of completeness: Haversine on Wikipedia.


2 Answers

The npm package geolib https://github.com/manuelbieh/geolib has several methods that can help you do this; getDistance, findNearest, and orderByDistance.

like image 170
reggie3 Avatar answered Sep 21 '22 15:09

reggie3


Try out this updated working code!

import {getDistance} from 'geolib';
import * as Location from "expo-location";
import * as Permissions from "expo-permissions";

export default class Locations extends Component
{
    state = {  
        destinationLat: 31.6200,
        destinationLong: 74.8765,
        distance:null,
        startLat:52.528308,
        startLong:1.3817765
    };
    async componentDidMount(){
        let location = await Location.getCurrentPositionAsync({
            enableHighAccuracy: true,
        });
        this.setState({
            startLat: location.coords.latitude,
            startLong: location.coords.longitude,
          });
        var dis = getDistance(
            {latitude: location.coords.latitude, longitude: location.coords.longitude},
            {latitude: this.state.destinationLat, longitude: this.state.destinationLong},
          );
          this.setState({
            distance: dis/1000,
          });
    };
    render(){
        return(
            <Text>{this.state.distance} KMs</Text>
        );
    }
}
like image 22
Rahul Avatar answered Sep 18 '22 15:09

Rahul