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 9068515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:12:39+00:00 2026-06-16T17:12:39+00:00

I have created this script with the help of the Haversine formula, the problem

  • 0

I have created this script with the help of the Haversine formula, the problem is that it keeps directing me to the first location on the array, no matter how many time I swap them about. Any ideas?

var locations = new Array(
  Array("Brighton", 50.82253, -0.137163),
  Array("Chichester", 50.83761, -0.774936),
  Array("Portsmouth", 50.8166667, -1.0833333),
  Array("Worthing", 50.81787, -0.372882)
);

function deg2rad(degrees){
    radians = degrees * (Math.PI/180);
    return radians;
}

function haversine(lat1,lon1,lat2,lon2) {
    deltaLat = lat2 - lat1;
    deltaLon = lon2 - lon1;
    earthRadius = 3959; // In miles (6371 in kilometers)
    alpha = deltaLat/2;
    beta = deltaLon/2;
    a = Math.sin(deg2rad(alpha)) * Math.sin(deg2rad(alpha)) 
      + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) 
      * Math.sin(deg2rad(beta)) * Math.sin(deg2rad(beta));
    c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    distance = earthRadius * c;
    return distance.toFixed(2);
}

function locationSuccess(position) {

    var places = new Array();
    var userLatitude = position.coords.latitude;
    var userLongitude = position.coords.longitude;

    for(var i=0;i<locations.length;i++) {
        places.push(haversine(
          userLatitude,
          userLongitude,
          locations[i][1],
          locations[i][2]
        ));
    }

    var sorted = places.sort(); // Sort places from low to high 

    /*
    alert(places); // Listed distances unordered
    alert(sorted); // Listed distances in order
    alert(sorted[0]); // Nearest city distance
    */

}

function locationFailed() {
    // Change me to a better error message
    alert("Ooops, we couldn't find you!"); 
}

// Get user's physical location
navigator.geolocation.getCurrentPosition(locationSuccess, locationFailed);
  • 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-16T17:12:41+00:00Added an answer on June 16, 2026 at 5:12 pm

    You’re not sorting the array of locations, only the array of distances.

    You should drop the distance into the locations array (i.e. as the fourth member of each element):

    for(var i = 0; i < locations.length; ++i) {
        var l = locations[i];
        l[3] = haversine(userLatitude, userLongitude, l[1], l[2]);
    }
    

    and then use a “comparator” function that looks at that distance field:

    locations.sort(function(a, b) {
        return (a[3] < b[3]) ? -1 : ((a[3] > b[3]) ? 1 : 0);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script task that executes a dynamically created SQL restore statement. This
Quite new to this but i have created a SQL script that groups by
I have created (with help from other scripts found online) a slideshow script that
I have created this Fiddle with this code: This is my problem I want
I created this account registration activation script of my own, I have checked it
This is my first time to post my query on stackoverflow. I have created
I have created a google script that should have sent me an email, instead
I have a form that I have created using Google App Script. And it
I have created a script that when you run it, it sets the 'password
I have programmed a script-- First I have created a <div>....</div> which is hidden

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.