Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:44:04+00:00 2026-06-05T06:44:04+00:00

I use this code to calculate to distance between two gps positions. Problem is

  • 0

I use this code to calculate to distance between two gps positions. Problem is when i return calculated value it returns undefined value. Please help me

function calcDistane(offerID,userLocation){
    var dist;
    var adapter = new LocationAdapter();

    adapter.selectByOfferId(offerID,function(res){                       

    navigator.geolocation.getCurrentPosition(function(position){

        var R = 6371;
        var userLocation= position.coords;

        dist= Math.acos(Math.sin(userLocation.latitude)*Math.sin(res.item(0).lt) + 
              Math.cos(userLocation.latitude)*Math.cos(res.item(0).lt) *
              Math.cos(userLocation.longitude-res.item(0).lg)) * R;
        });

    });  
    return dist;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:44:05+00:00Added an answer on June 5, 2026 at 6:44 am

    dist isn’t set yet when you return. The function that is setting dist is a callback. That is very likely to be called after you return from the outer (callback) function.

    The likely order of execution is

    1. adapter.selectByOfferId
    2. return dist (undefined)
    3. call anonymous function used as callback to adapter.selectByOfferId
    4. Call navigator.geolocation.getCurrentPosition and return from the callback from step 3
    5. When navigator.geolocation.getCurrentPosition returns then the callback of that call is called and dist is set. Which is after step 2

    You will need to pass a continuation rather than returning

    function calcDistane(offerID,userLocation,callback){
      var adapter = new LocationAdapter();
    
      adapter.selectByOfferId(offerID,function(res){                       
    
            navigator.geolocation.getCurrentPosition(function(position){
    
                   var R = 6371;
                   var userLocation= position.coords;
    
                   callback(Math.acos(Math.sin(userLocation.latitude)*Math.sin(res.item(0).lt) + 
                            Math.cos(userLocation.latitude)*Math.cos(res.item(0).lt) *
                            Math.cos(userLocation.longitude-res.item(0).lg)) * R);
    
           });
    
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which calculates the distance between two coordinates. The two functions
I'm trying to calculate the distance between two points. The two points I stored
Distance between two points: sqrt((x1-x2)^2 + (y1-y2)^2) Is there a way to do this
I use this code to calculate the sum : http://jsfiddle.net/QmTNZ/2/ I want to modify
I need to calculate precision and recall value in lucene and I use this
I try to calculate the distance between two points (given the latitude/longitude of those
This problem is a kind of closest pair between two disjoint set. Upperside picture
In the following code I use bootstrapping to calculate the C.I. and the p-value
I use this code to update data in database table. Can reuse same code
I use this code: http://blogswizards.com/plugin-development/sliding-boxes-and-captions-with-jquery On a simple gallery site I am building. Specifically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.