Added List Location Activity and Sort Data by Distance with Bubble Sort Algorithm
utopian-io·@sogata·
0.000 HBDAdded List Location Activity and Sort Data by Distance with Bubble Sort Algorithm
### 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. <center></center> ### 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
👍 wahyudi98, dongkrak, steempulsa, darut, leumo, bsi, drsensor, clivingston005, mungprik, leir, mightypanda, primeradue, alfajrisyariff, amosbastian, ulockblock, rufans, helo, jaff8, fego, adamada, bestofph, kendallron, pinas, buckydurddle, jinger, rajamalikulfajar, mcfarhat, memeitbaby, indayclara, atika, baroen96, properfraction, justyy, moneybaby, steem-ua, newsrx, sargoon, utopian-io, tombstone, jga, cyprianj, cryptouno, dicetime, delegate4upvotes,