calculate distance from source to destination in android

Solutions on MaxInterview for calculate distance from source to destination in android by the best coders in the world

showing results for - "calculate distance from source to destination in android"
Emely
17 Jan 2018
1//import this
2import com.google.maps.android.SphericalUtil;
3
4public class Detailed extends AppCompatActivity {
5
6//define object for storing latitude and longitude coordinates
7LatLng YourLocationData;
8LatLng DestinationData;
9Double Distance;
10
11 @Override
12    protected void onCreate(Bundle savedInstanceState) {
13        super.onCreate(savedInstanceState);
14        setContentView(R.layout.activity_main);
15      
16      //button casting
17       Button btn=findViewById(R.id.button2);
18      
19      //setting cooridnates 
20      YourLocationData = new LatLng(15.40757526659319, 73.99194849921113);
21      DestinationData = new LatLng(15.366058435624439, 73.94238788088278);
22      
23      
24      //OnClick listener for functionality of button
25      btn.setOnClickListener(new View.OnClickListener() {
26            @Override
27            public void onClick(View view) {
28
29
30              //checking permission for location
31        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) {
32        
33            return;
34        }
35              
36              distance = SphericalUtil.computeDistanceBetween(YourLocationData,DestinationData);
37                            Toast.makeText(MainActivity.this, "Distance between Your Location and Destination is \n " + String.format("%.2f", distance / 1000) + "km", Toast.LENGTH_SHORT).show();
38              
39      });
40        
41      }
42                             
43    }
44
45
46
47
48
49
50
51
52
53