android calculates the distance according to the longitude and latitude of Double type data, and then realizes the sorting function according to the distance


In the project, the national gas station data is used to load and calculate the distance according to the longitude and latitude, and then the distance is sorted from small to large
The main reason is that the data type is Double. You have to encapsulate the data here
It's not hard to realize. Let's talk about our own implementation methods and ideas

design sketch

Let's first look at the unsorted renderings

Carefully look at the distance in the above figure is that the distance without sorting is all disorder. Our purpose is to complete sorting from small to large.

Double type data sorting

 Collections.sort(mList, new Comparator<State>() {
            public int compare(State o1, State o2) {
               //If the data type here is double, an error will be reported
               //Error prompt: cannot invoke compareTo(double) on the primitive type double
             return o1.compareTo(o2);                 

The solution of double can't be sorted directly

compareTo Only for implementation Comparable Object type of
   // The first modification method:
        Collections.sort(mList, new Comparator<State>() {
            public int compare(State o1, State o2) { 
                return new Double(o1).compareTo(new Double(o2));

    // The second solution is:
    double distance;//Definition of distance

   //Amend to read: 
   Double distance;  
   //Change distance to double's wrapper class double type

Sorting code implementation

According to the data we get, we process the data, that is, we calculate the latitude and longitude of each Bean

 if (homeModelBaseBean.getCode() == SUCCESS) {
 			//Get the data
            List<HomeModel> data = homeModelBaseBean.getData();
      		//Non empty judgment
            if (data != null && data.size() > 0) {
                for (int i = 0; i < data.size(); i++) {
                	//The implementation of compare by sorting interface
                    Collections.sort(data, new Comparator<HomeModel>() {
                        public int compare(HomeModel o1, HomeModel o2) {
                        	//Here we get the current longitude and latitude of the service station and calculate the current distance of each service station through Utils
                        	//Then according to the distance to achieve the order from near to far
                            double dis = GetDistanceUtils.getDistance(latLng.longitude,
                                    latLng.latitude, o1.getGasAddressLongitude(),
                            double dis2 = GetDistanceUtils.getDistance(latLng.longitude, 
                                    o2.getGasAddressLongitude(), o2.getGasAddressLatitude());
                             //Print log to see data sorting
                            Log.e(TAG, "The calculated distance is:" + dis);
                            //The first modification method, new Double type, is used here
                            //Because this distance is calculated and is not the type of background return definition
                            return new Double(dis).compareTo(dis2);
        } else {


Sort implementation (from near to far)

Posted on Thu, 04 Jun 2020 11:39:54 -0400 by MFKR