Calculate distance between two geo coordinates in android

watch_later Friday, July 29, 2016
comment 1 Comment
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>

4)Demo

 Finally,run your application


 OUTPUT:

you will get distance in km. see below screen




avatar

What Does Casino Debut Offer? - DRMCD
You 서산 출장안마 are not required to enter a 서울특별 출장안마 bonus code to enter a 전라남도 출장안마 free money 안산 출장샵 bonus on any of 정읍 출장안마 your games. All you have to do is make the right deposit, and the bonus amount

delete March 4, 2022 at 2:01 AM



sentiment_satisfied Emoticon