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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:46:37+00:00 2026-05-30T14:46:37+00:00

I am having Current Location Latitude & Longitude with me. I am getting the

  • 0

I am having Current Location Latitude & Longitude with me. I am getting the list of ATM’s near by the user from the Google places API Web Service and storing my data in Arraylist like below.

Code Snippet

double currentLat = xxx;
double currentLang = xxx;

on button click i call the web service of google places.

public void buttonClicked(View v) {
    ParseXMLData xmlData = new ParseXMLData();
    url = baseUrl + "location=" + latitude + "," + longitude + "&" + "radius=" + search_Area_Radius + "&" + "name=" + nameofplace + "&" + "sensor=true" + "&" + "key=" + API_KEY;
    xmlData.execute("");
}

Storing the result inside arraylist….

    for (int i = 0; i < nodes.getLength(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element)nodes.item(i);
        map.put("Name", XMLFunctions.getValue(e, "name"));
        map.put("Vicinity", XMLFunctions.getValue(e, "vicinity"));
        map.put("Latitude", XMLFunctions.getValue(e, "lat"));
        map.put("Longitude", XMLFunctions.getValue(e, "lng"));

        loc = new Location("");
        loc.setLatitude(Double.parseDouble(XMLFunctions.getValue(e, "lat")));
        loc.setLongitude(Double.parseDouble(XMLFunctions.getValue(e, "lng")));

Getting the distance b/w 2 points frome here.

        distance = currentLoc.distanceTo(loc);
        map.put("Distance",String.valueOf(distance));
        mylist.add(map);
        loc = null;
        distance = 0.0;
    }

Implemented the Comparator and Sort the ArrayList

public class PositionComparator implements Comparator<HashMap<String, String>> {

    public PositionComparator() {
        // TODO Auto-generated constructor stub
    }


    public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
        /*System.out.println(o1.get("O1 Distance :- "));
        System.out.println(o2.get("O2 Distance :- "));
        System.out.println(o1.get("Distance").compareTo(o2.get("Distance")));
        */
        return o1.get("Distance").compareTo(o2.get("Distance"));
    }
}

Now what happen is the data which i have store inside the arraylist is sorted but the problem is compareTo method sort data based on the 1st character only. Suppose my data is like 2.12345 and 11.32545 then it will replace as 2-1 = 1.

How to solve this?

If anyone has any suggestion/tips for this please kindly help me in this.

Thanks

  • 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-05-30T14:46:38+00:00Added an answer on May 30, 2026 at 2:46 pm

    Here goes the thread that describes how you can calculate the distance between to gps coordinates. It features implementations in many languages, hopefully you will understand some of them. Then lets assume you have a function calcDistance that returns Double – the distance from the current point to hash map of your type using its longitude and latitude (btw is it requirement that you use such ugly kind of map? Can’t you create a bean class for the same purpose).

    Then what you need is to declare a method of comparing two positions specified by their positions:

    public class PositionComparator implements Comparator<HashMap<String, String>> {
        public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
            return calcDistance(o1).compareTo(calcDistance(o2));
        }
    }
    

    And finally the sorting is done like that:

    Collections.sort(mylist, new PositionComparator());
    

    However mind that if you implement the sorting that way the distance to each element will be calculated multiple times. I recommend you to add one more field in the map that stores the distance, so that you avoid reevaluating it.

    One last note: if you decide to switch the map with a class, you can easily make this class implement the Comparable interface and define the comparison there.

    EDIT Adding the requested example of the bean:

    public class PositionBean implements Comparable {
       private double latitude;
       private double longitude;
       private distanceFromCurrentPosition;
       private String vicinity;
       private String name;
       // ...getters and setters ...
    
       public PositionBean(String latitude, String longitude, String vicinity,
                String name) {
           this.latitude = Double.valueOf(latitude);
           this.longitude = Double.valueOf(longitude);
           this.vicinity= vicinity;
           this.name = name;
           this.distanceFromCurrentPosition = calcDistance();
       }
    
       public int compareTo(Object anotherPositionObj) throws ClassCastException {
           if (!(anotherPositionObj instanceof PositionBean)) {
               throw new ClassCastException("A PositionBean object expected.");
           }
           int anotherPosition = (PositionBean) anotherPositionObj;  
           return Double.compare(this.getDistanceFromCurrentPosition(),
                   anotherPosition.getDistanceFromCurrentPosition());    
       }
    
       private double calcDistance() {
           //...implement accordingly
       }
    }
    
    public static void main(String [] args) {
        List<PositionBean> positions = new ArrayList<PositionBean>();
        // ... Initialize the positions
        Collections.sort(positions);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I'm having trouble trying to find the user's longitude and location as
I am having a map application. When a person travels from current location to
I'm writing a web application using the google maps api v3, which displays places
I'm having a problem getting Google Maps to display when compiled and run on
I want to store latitude and longitude values of places in a mysql database
I am using MKReverseGeocoder to reversely geocode a latitude/longitude address. However I am having
I have a string of data latitude,longitude,location stored as an NSString. I want to
I am having trouble getting PropertyPlaceholderConfigurer to work in my current configuration. Given the
Seem to be having some issues storing the current date in a core data
I am having a bit of difficulty with this current code I have set

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.