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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:43:27+00:00 2026-06-05T11:43:27+00:00

I have a list in jquery mobile. I have added code to resort the

  • 0

I have a list in jquery mobile. I have added code to resort the list based on the nearest state I am in. For some reason, my logic does not work. Any ideas why? Also the list is very sluggish when scrolling.

Check this link out and click Find Nearest Location Button on Top Left

http://www.jm.bugs3.com/gl/state.html

<script>
function findMe(){

    if (navigator.geolocation !=undefined){
        navigator.geolocation.watchPosition(onFound, handleError);
    }
}
function onFound(position){
    var userLat = position.coords.latitude;
    var userLong = position.coords.longitude;
    $('ul li').each(function (index){
        var locationLat = $(this).find('.lat').html();
        var locationLong = $(this).find('.long').html();
        var distance = getDistance(userLat, locationLat, userLong, locationLong);
        $(this).data("distance", distance);
    })

    reOrder();  
}



function handleError(error){
    alert ("Could not find location");
}

function reOrder(){
    $('ul li').sort(sortAlpha).appendTo('ul');  
}

function sortAlpha(a, b){
    return $(a).data('distance') > $(b).data('distance') ? 1 : -1;  //if True or false 1 or -1
};
//Calculate the shortest distance based on lat and long
function getDistance(lat1, lat2, lon1, lon2){
    var R = 6371; //KM
    var d = Math.acos(Math.sin(lat1) * Math.sin(lat2) +
            Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R
        return d    
};

// Here is the code for the list with lat and long coordinates

<div data-role="content">   
        <ul data-role="listview" data-inset="true">
            <li>
                <a href="#page2" data-transition="slide" >Alabama</a>
                <div class="lat" style="visibility:hidden">31.375626</div>
                <div class="long" style="visibility:hidden">-86.299592</div>
            </li>
            <li>
                <a href="#page3" data-transition="slide">Alaska</a>
                <div class="lat" style="visibility:hidden">60.216736</div>
                <div class="long" style="visibility:hidden">-149.882995</div>
            </li>
            <li>
                <a href="#page4" data-transition="slide">Arizona</a>
                <div class="lat" style="visibility:hidden">32.447659</div>
                <div class="long" style="visibility:hidden">-112.134568</div>
            </li>
            <li>
                <a href="#page5" data-transition="slide" >Arkansas</a>
                <div class="lat" style="visibility:hidden">33.678252</div>
                <div class="long" style="visibility:hidden">-92.340846</div>
            </li>
            <li>
                <a href="#page6" data-transition="slide" >Boston</a>
                <div class="lat" style="visibility:hidden">41.358431</div>
                <div class="long" style="visibility:hidden">-71.059773</div>
            </li>
  • 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-05T11:43:29+00:00Added an answer on June 5, 2026 at 11:43 am

    Hershey,

    “… based on the nearest state I am in” is a tad difficult to interpret for the following reasons :

    • At its simplest, “the nearest state I am in” is “the state I am in”
    • “The nearest geographic centre of a state” may not be that of “the state I am in” (eg. if I am at the western end of the Florida Pan Handle, then the “nearest state” is Alabama, not Florida)
    • I might not be in a state (eg. I am at sea).

    Notwithstanding this difficulty and trusting that your own code correctly reflects the proper meaning, and working from @ddotsenko’s answer as a startpoint, then the following improvements might be made :

    • A true sort operation can be avoided by loading <li> references into an array, rather than an object, indexed by distance (rounded). This will give a sparse array in the correct order.
    • The <li> elements can be reordered within their original <ul> avoiding the need for a new <ul> element at each sort.

    Taking these points onboard, a modified version of ddotsenko’s code would be as follows:

    function onFound(position) {
        var userLat = position.coords.latitude,
            userLong = position.coords.longitude,
            arr = [],
            $ul = $("ul");//a more specific selector would be good
        $ul.find('li').each(function (index) {
            var $this = $(this),
                lat = Number($this.data("latitude")),
                lng = Number($this.data('longitude')),
                dist = Math.round(getDistance(userLat, lat, userLong, lng));
            arr[dist] = this;
        });
        //At this point, arr is a sparse array of li elements indexed by distance, hence in distance order.
        for(var item in arr) {
            //Ref: "Iterating over sparse arrays", //http://hexmen.com/blog/2006/12/iterating-over-sparse-arrays/
            if (String(item >>> 0) == item && item >>> 0 != 0xffffffff) {
                $ul.append(item);
            }
        };
    }
    

    This should be appreciably faster.

    Regarding, sluggish scrolling, you may be better off with less elements in the DOM. The javasctipt in ddotsenko’s code indicates <li>s of the following format :

    <li data-latitude="31.375626" data-longitude="-86.299592">
        <a href="#page2" data-transition="slide" >Alabama</a>
    </li>
    

    which should be better than hidden divs to hold the data.

    Even better would be to have all the lat/long pairs hard-coded in javascript, thus avoiding slower retreival from the DOM, eg.:

    var stateLocations = {
        "Alabama":  {lat: 31.375626, lng: -86.299592},
        "Alaska":   {lat: 60.216736, lng: -149.882995},
        "Arizona":  {lat: 32.447659, lng: -112.134568},
        "Arkansas": {lat: 33.678252, lng: -92.340846},
        ...
    };
    

    Then look up accordingly.

    By the way, Boston is a city, not a state. None of the United States begins with “B”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamically added list on the home of my jQuery Mobile Page.
I have created a list in jquery mobile which directs to some page on
I have a list view in jQuery Mobile with delete buttons in them. <ul
I have a jquery mobile list. This list is populated with data. I want
I have written a cascading drop down list using JQuery. The code that I
I am using android 2.2, phonegap 1.3, and jquery-mobile 1.0 I have a list
I have been working in Jquery mobile and have been trying some samples and
I'm using jQuery Mobile to create a mobile (obviously) website. I have a list
I have a JSon array that displays in a JQuery Mobile list. It shows
I'm trying to bind some list item links in jquery mobile to a click

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.