Giter Site home page Giter Site logo

Comments (4)

justinmespel avatar justinmespel commented on June 12, 2024 1

I wasn't getting very accurate distances using this function either. I use this to calculate the distance in KM, then times the result by 1000

calculateKmDistance(loc1, loc2): number {

    const R = 6371.0710; // Radius of the Earth in Km
    const r1 = loc1.latitude * (Math.PI / 180); // Convert degrees to radians
    const r2 = loc2.latitude * (Math.PI / 180); // Convert degrees to radians
    const d1 = r2 - r1; // Radian difference (latitudes)
    const d2 = (loc2.longitude - loc1.longitude) * (Math.PI / 180); // Radian difference (longitudes)

    return 2 * R * Math.asin(Math.sqrt(Math.sin(d1 / 2) *
        Math.sin(d1 / 2) + Math.cos(r1) * Math.cos(r2) *
        Math.sin(d2 / 2) * Math.sin(d2 / 2)));
}

from nativescript-geolocation.

akarsh014 avatar akarsh014 commented on June 12, 2024

I have faced this issue as well, i get 0 when i calculate the distance.

from nativescript-geolocation.

alexonozor avatar alexonozor commented on June 12, 2024

Do you still need to times the result by 1000 to get it in KM?

I wasn't getting very accurate distances using this function either. I use this to calculate the distance in KM, then times the result by 1000

calculateKmDistance(loc1, loc2): number {

    const R = 6371.0710; // Radius of the Earth in Km
    const r1 = loc1.latitude * (Math.PI / 180); // Convert degrees to radians
    const r2 = loc2.latitude * (Math.PI / 180); // Convert degrees to radians
    const d1 = r2 - r1; // Radian difference (latitudes)
    const d2 = (loc2.longitude - loc1.longitude) * (Math.PI / 180); // Radian difference (longitudes)

    return 2 * R * Math.asin(Math.sqrt(Math.sin(d1 / 2) *
        Math.sin(d1 / 2) + Math.cos(r1) * Math.cos(r2) *
        Math.sin(d2 / 2) * Math.sin(d2 / 2)));
}

from nativescript-geolocation.

justinmespel avatar justinmespel commented on June 12, 2024

@alexonozor I don't think so, it should return the result in KM. I think / 1000 to get it in meters.

from nativescript-geolocation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.