distance between your location to destination android

Solutions on MaxInterview for distance between your location to destination android by the best coders in the world

showing results for - "distance between your location to destination android"
Alayah
16 May 2018
1import com.google.maps.android.SphericalUtil;
2
3 private FusedLocationProviderClient fusedLocationClient;
4    LatLng yourLocation;
5    LatLng Destination=new LatLng(15.389230635580095, 73.92281848312571);
6    Double distance;
7
8
9
10
11
12
13//inside oncreate
14
15
16 btn.setOnClickListener(new View.OnClickListener() {
17            @Override
18            public void onClick(View view) {
19
20          
21
22        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
23       
24            return;
25        }
26                fusedLocationClient.getLastLocation()
27                .addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
28                    @Override
29                    public void onSuccess(Location location) {
30                        // Got last known location. In some rare situations this can be null.
31                        if (location != null) {
32                           
33                            Double lat =location.getLatitude();
34                            Double longi = location.getLongitude();
35
36                            yourLocationPlz(lat,longi);
37
38                            
39                            distance = SphericalUtil.computeDistanceBetween(yourLocation,Destination);
40                            Toast.makeText(MainActivity.this, "Distance between Your Location and Destination is \n " + String.format("%.2f", distance / 1000) + "km", Toast.LENGTH_SHORT).show();
41                        }
42                    }
43                });
44
45            }
46        });
47
48
49
50
51
52
53
54    
55
56
57
58public void  yourLocationPlz(Double Latitude,Double Longitude){
59
60         yourLocation=new LatLng(Latitude,Longitude);
61
62
63}
64
65
66
67
68
69
70
71
72