Added List Location Activity and Sort Data by Distance with Bubble Sort Algorithm

in #undefined5 years ago

Repository

https://github.com/sogatanco/nemu-bengkel

About Nemu-Bengkel

NEBENG (Nemu-Bengkel) is is an android application to find the nearest car repair place and motorcycle repair place. When your vehicle is damaged in transit, you don't know where it is closest to repairing it. Nebeng is the solution. Currently this application is designed for the Android operating system and will be developed for iOS operating systems.

What feature(s) did you add?

  • List Location Activity
    It is an activity that contains all car repair location and motorcycle repair location. All location shown on Recycle view that contains image, rating, category, comment count and your distance to location.

  • Sort Data by Distance with Bubble Sort Algorithm
    The location list are sorting by distance
    Location list is displayed according to the closest distance. The location closest to the user will be displayed at the top, then followed by the next closest location. This sorting uses the bubble sort algorithm.

Screen Shot 2019-06-27 at 17.35.08.png

How did you implement it/them?

All this features develop using Android Studio 3.3.2

  • Location List
    To display the list we use Recycle View and the data are from NEBENG rest API. To request the data we use the volley library on android
JsonObjectRequest getBengkel = new JsonObjectRequest(Request.Method.GET, URL, null,
            new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
      // here the code to add data to adapter you can see full ono GitHub
}, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("eror", error.toString());
            }
        });
requestQueue.add(getBengkel);

After getting data from Json request we add data to recycle view adapter here the snippet code :

    public void onBindViewHolder(@NonNull BengkelAdapter.ViewHolder viewHolder, int i) {
        bengkel=list.get(i);
        viewHolder.id=bengkel.getIdbengkel();
        viewHolder.mtextview.setText(bengkel.getNamaBengkel());
        Glide
                .with(context)
                .load(bengkel.gerUrlImage())
                .override(200,180)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewHolder.mgambar);
        viewHolder.ratingBar.setRating((float)jrating/bengkel.getUlasan());
        viewHolder.ulasan.setText(String.valueOf(bengkel.getUlasan())+" ulasan");
        viewHolder.jarak.setText(bengkel.getJarak());
    }
  • Sort Data by Distance with Bubble Sort Algorithm
    before sorting the data by distance we need to get user location and distance between user to location. To get user location we use LocationListener method
location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        onLocationChanged(location);
 if (location != null) {
            loc = new LatLng(location.getLatitude(), location.getLongitude());
        }

After getting the user longitude and latitude, Now we calculate the distance between user and each location we use this function

    public static double CalculationByDistance(LatLng StartP, LatLng EndP) {
        int Radius = 6371;// radius of earth in Km
        double lat1 = StartP.latitude;
        double lat2 = EndP.latitude;
        double lon1 = StartP.longitude;
        double lon2 = EndP.longitude;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult * 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));
        return km;
    }

After getting the distance, we sort it by distance using bubble sort algorithm. Bubble Sort algorithm is sequencing by exchanging data with data next to it continuously until in a certain iteration there are no changes. Here the code we use

   bubble sort algorithm
                            int n=rr.size();
                            for(int a=0; a < n; a++){
                                for(int b=1; b< (n -a); b++){
                                    if(CalculationByDistance(loc, new LatLng(rr.get(b-1).getDouble("bk_lat"),rr.get(b-1).getDouble("bk_long"))) >CalculationByDistance(loc, new LatLng(rr.get(b).getDouble("bk_lat"),rr.get(b).getDouble("bk_long")))){
                                        Collections.swap(rr,b-1,b);
                                    }
                                }
                            }

Commits on Github

https://github.com/sogatanco/nemu-bengkel/compare/master@%7B06-23-19%7D...master

Github Account

https://github.com/sogatanco

Sort:  

Thank you for your contribution.

You don't need to manually implement sorting algorithms these days in most cases, you can use the Arrays.sort or Collections.sort with a custom comparator. e.g. https://www.google.com/search?q=java+custom+sort+comparator+collections&oq=java+custom+sort+comparator+collections&aqs=chrome..69i57j0.6234j0j7&sourceid=chrome&ie=UTF-8

Bubble sorting is inefficient (only for teaching, not for practical use), if you really want to go down implementing your sorting algorithm, choose "selection sort" in simple sorting, and "quick-sort" or "merge-sort" if you want some more complex ones.

No unit tests.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @justyy! Keep up the good work!

Hi @sogata!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @sogata!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!