Android Program to Calculate Distance between Two Latitude and Longitude Points

Here is source code of the Program to Calculate Distance between two points using Latitude & Longitude in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

package com.example.changeiamge;
 
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.renderscript.Type;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
 
public class MainActivity extends Activity implements OnCheckedChangeListener {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        EditText lat_dest = (EditText) findViewById(R.id.dest_latitude);
        EditText lon_dest = (EditText) findViewById(R.id.dest_lon);
 
        Button but1 = (Button) findViewById(R.id.button1);
        but1.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
 
                               Location mylocation = new Location("");
                                  Location dest_location = new Location("");
                               String lat = lat_dest.getText().toString();
                       String lon = lon_dest.getText().toString();
                               dest_location.setLatitude(Double.parseDouble(lat));
                       dest_location.setLongitude(Double.parseDouble(lon));
                               Double my_loc = 0.00;
                               mylocation.setLatitude(my_loc);
                       mylocation.setLongitude(my_loc);
                               Double distance = mylocation.distanceTo(dest_location);//in meters
                               Toast.makeText(this, "Distance"+Double.toString(distance),
                        Toast.LENGTH_LONG).show();
            }
        });
 
    }
}

Screenshot_2013-11-16-20-37-02

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.