Hi,In this tutorial we discuss about how to calculate distance between two locations in android by using Location class which is available in android.location.Location package.
lets start coding
Create a Project:
create layout file in res/layout/main.xml
1) main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/distancetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="25sp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
2)MainActivity.java
First take any two location coordinates in google map
next add the below code in your java code file
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView distancetext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
distancetext=(TextView)findViewById(R.id.distancetext);
Location locationA = new Location("point A");
locationA.setLatitude(Double.parseDouble("17.423397"));
locationA.setLongitude(Double.parseDouble("78.425674"));
Location locationB = new Location("point B");
locationB.setLatitude(Double.parseDouble("17.413393"));
locationB.setLongitude(Double.parseDouble("78.428303"));
float distance = locationA.distanceTo(locationB);
//Log.e("", "distance "+(distance / 1000));
// Log.e("", "distance "+new DecimalFormat("##.#").format(distance / 1000));
String finaldistance = new DecimalFormat("##.#").format(distance / 1000);
Log.e("", "distance "+finaldistance+ " "+"km");
distancetext.setText("distance "+finaldistance+ " "+"km");
}
}
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Finally,run your application
OUTPUT:
you will get distance in km. see below screen
lets start coding
Create a Project:
create layout file in res/layout/main.xml
1) main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/distancetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="25sp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
2)MainActivity.java
First take any two location coordinates in google map
next add the below code in your java code file
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
TextView distancetext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
distancetext=(TextView)findViewById(R.id.distancetext);
Location locationA = new Location("point A");
locationA.setLatitude(Double.parseDouble("17.423397"));
locationA.setLongitude(Double.parseDouble("78.425674"));
Location locationB = new Location("point B");
locationB.setLatitude(Double.parseDouble("17.413393"));
locationB.setLongitude(Double.parseDouble("78.428303"));
float distance = locationA.distanceTo(locationB);
//Log.e("", "distance "+(distance / 1000));
// Log.e("", "distance "+new DecimalFormat("##.#").format(distance / 1000));
String finaldistance = new DecimalFormat("##.#").format(distance / 1000);
Log.e("", "distance "+finaldistance+ " "+"km");
distancetext.setText("distance "+finaldistance+ " "+"km");
}
}
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4)Demo
OUTPUT:
you will get distance in km. see below screen